ncloud.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. class CloudObject{
  2. id // 编号
  3. className // 名称
  4. data ={} // 属性、内容
  5. constructor(className){
  6. this.className = className
  7. }
  8. toPointer(){
  9. return {"__type":"Pointer","className":this.className,"objectId":this.id}
  10. }
  11. set(json){
  12. Object.keys(json).forEach(key=>{
  13. if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
  14. return
  15. }
  16. this.data[key] = json[key]
  17. })
  18. }
  19. get(key){
  20. return this.data[key] || null
  21. }
  22. // 更新
  23. async save(){
  24. let method = "POST"
  25. let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className; //默认
  26. if(this.id){
  27. url += "/"+this.id
  28. method = "PUT"
  29. }
  30. let body = JSON.stringify(this.data)
  31. let response = await fetch(url, {
  32. "headers": {
  33. "content-type": "application/json;charset=UTF-8",
  34. "x-parse-application-id": "dev"
  35. },
  36. "body": body,
  37. "method": method,
  38. "mode": "cors",
  39. "credentials": "omit"
  40. });
  41. let result = await response?.json();
  42. if(result?.objectId){this.id = result?.objectId}
  43. return this
  44. }
  45. // 删除
  46. async destory(){
  47. if(!this.id) return
  48. let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.id, {
  49. "headers": {
  50. "x-parse-application-id": "dev"
  51. },
  52. "body": null,
  53. "method": "DELETE",
  54. "mode": "cors",
  55. "credentials": "omit"
  56. });
  57. let result = await response?.json();
  58. if(result){
  59. this.id = null
  60. }
  61. return true
  62. }
  63. }
  64. // 查询
  65. class CloudQuery{
  66. className
  67. constructor(className){
  68. this.className = className
  69. }
  70. whereOptions = {}
  71. equalTo(key,value){
  72. this.whereOptions[key] = value
  73. }
  74. greaterThan(key,value){
  75. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  76. this.whereOptions[key]["$gt"] = value
  77. }
  78. greaterThanAndEqualTo(key,value){
  79. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  80. this.whereOptions[key]["$gte"] = value
  81. }
  82. lessThan(key,value){
  83. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  84. this.whereOptions[key]["$lt"] = value
  85. }
  86. lessThanAndEqualTo(key,value){
  87. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  88. }
  89. async get(id){ // 通过id查询
  90. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"
  91. let response = await fetch(url, {
  92. "headers": {
  93. "if-none-match": "W/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
  94. "x-parse-application-id": "dev"
  95. },
  96. "body": null,
  97. "method": "GET",
  98. "mode": "cors",
  99. "credentials": "omit"
  100. });
  101. let json = await response?.json();
  102. return json || {}
  103. }
  104. async find(){ // 直接查询
  105. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  106. if(Object.keys(this.whereOptions)?.length){
  107. let whereStr = JSON.stringify(this.whereOptions)
  108. url += `where=${whereStr}`
  109. }
  110. let response = await fetch(url, {
  111. "headers": {
  112. "if-none-match": "W/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
  113. "x-parse-application-id": "dev"
  114. },
  115. "body": null,
  116. "method": "GET",
  117. "mode": "cors",
  118. "credentials": "omit"
  119. });
  120. let json = await response?.json();
  121. return json?.results || []
  122. }
  123. async first(){ // 只查询第一项
  124. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  125. if(Object.keys(this.whereOptions)?.length){
  126. let whereStr = JSON.stringify(this.whereOptions)
  127. url += `where=${whereStr}`
  128. }
  129. let response = await fetch(url, {
  130. "headers": {
  131. "if-none-match": "W/\"22c-NGsQZp5SnqjXTAX+NXjLAv6LaLA\"",
  132. "x-parse-application-id": "dev"
  133. },
  134. "body": null,
  135. "method": "GET",
  136. "mode": "cors",
  137. "credentials": "omit"
  138. });
  139. let json = await response?.json();
  140. let exists = json?.results?.[0] || null
  141. if(exists){
  142. let existsObject = new CloudObject(this.className)
  143. existsObject.set(exists)
  144. existsObject.id = exists.objectId
  145. existsObject.createdAt = exists.createdAt
  146. existsObject.updateAt = exists.updateAt
  147. return existsObject
  148. }
  149. }
  150. }
  151. module.exports.CloudObject = CloudObject
  152. module.exports.CloudQuery = CloudQuery