ncloud.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. async save(){
  23. let method = "POST"
  24. let url = "http://1.94.237.145:1339/parse/classes/" + this.className
  25. // 更新
  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. "accept": "*/*",
  34. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  35. "content-type": "text/plain;charset=UTF-8",
  36. "x-parse-application-id": "ylj",
  37. "Referer": "http://127.0.0.1:4040/",
  38. "Referrer-Policy": "strict-origin-when-cross-origin"
  39. },
  40. "body": body,
  41. "method": method,
  42. "mode": "cors",
  43. "credentials": "omit"
  44. });
  45. let result = await response?.json();
  46. if(result?.error){
  47. console.error(result?.error)
  48. }
  49. if(result?.objectId){this.id = result?.objectId}
  50. return this
  51. }
  52. async destory(){
  53. if(!this.id) return
  54. let response = await fetch("http://1.94.237.145:1339/parse/classes/test/"+this.id, {
  55. "headers": {
  56. "accept": "*/*",
  57. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  58. "x-parse-application-id": "ylj",
  59. "Referer": "http://127.0.0.1:4040/",
  60. "Referrer-Policy": "strict-origin-when-cross-origin"
  61. },
  62. "body": null,
  63. "method": "DELETE",
  64. "mode": "cors",
  65. "credentials": "omit"
  66. });
  67. let result = await response?.json();
  68. if(result){
  69. this.id = null
  70. }
  71. return true
  72. }
  73. }
  74. class CloudQuery{
  75. className
  76. constructor(className){
  77. this.className = className
  78. }
  79. whereOptions = {}
  80. greaterThan(key,value){
  81. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  82. this.whereOptions[key]["$gt"] = value
  83. }
  84. greaterThanAndEqualTo(key,value){
  85. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  86. this.whereOptions[key]["$gte"] = value
  87. }
  88. lessThan(key,value){
  89. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  90. this.whereOptions[key]["$lt"] = value
  91. }
  92. lessThanAndEqualTo(key,value){
  93. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  94. this.whereOptions[key]["$lte"] = value
  95. }
  96. equalTo(key,value){
  97. this.whereOptions[key] = value
  98. }
  99. async get(id){
  100. let url = "http://1.94.237.145:1339/parse/classes/"+this.className+"/"+id+"?"
  101. let response = await fetch(url, {
  102. "headers": {
  103. "accept": "*/*",
  104. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  105. "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
  106. "x-parse-application-id": "ylj",
  107. "Referer": "http://127.0.0.1:4040/",
  108. "Referrer-Policy": "strict-origin-when-cross-origin"
  109. },
  110. "body": null,
  111. "method": "GET",
  112. "mode": "cors",
  113. "credentials": "omit"
  114. });
  115. let json = await response?.json();
  116. return json || {}
  117. }
  118. async find(){
  119. let url = "http://1.94.237.145:1339/parse/classes/"+this.className+"?"
  120. if(Object.keys(this.whereOptions)?.length){
  121. let whereStr = JSON.stringify(this.whereOptions)
  122. url += `where=${whereStr}`
  123. }
  124. let response = await fetch(url, {
  125. "headers": {
  126. "accept": "*/*",
  127. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  128. "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
  129. "x-parse-application-id": "ylj",
  130. "Referer": "http://127.0.0.1:4040/",
  131. "Referrer-Policy": "strict-origin-when-cross-origin"
  132. },
  133. "body": null,
  134. "method": "GET",
  135. "mode": "cors",
  136. "credentials": "omit"
  137. });
  138. let json = await response?.json();
  139. return json?.results || []
  140. }
  141. async first(){
  142. let url = "http://1.94.237.145:1339/parse/classes/"+this.className+"?"
  143. if(Object.keys(this.whereOptions)?.length){
  144. let whereStr = JSON.stringify(this.whereOptions)
  145. url += `where=${whereStr}`
  146. }
  147. let response = await fetch(url, {
  148. "headers": {
  149. "accept": "*/*",
  150. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  151. "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
  152. "x-parse-application-id": "ylj",
  153. "Referer": "http://127.0.0.1:4040/",
  154. "Referrer-Policy": "strict-origin-when-cross-origin"
  155. },
  156. "body": null,
  157. "method": "GET",
  158. "mode": "cors",
  159. "credentials": "omit"
  160. });
  161. let json = await response?.json();
  162. let exists = json?.results?.[0] || null
  163. if(exists){
  164. let existsObject = new CloudObject(this.className)
  165. existsObject.set(exists)
  166. existsObject.id = exists.objectId
  167. existsObject.createdAt = exists.createdAt
  168. existsObject.updatedAt = exists.updatedAt
  169. return existsObject
  170. }
  171. }
  172. }
  173. module.exports.CloudObject = CloudObject
  174. module.exports.CloudQuery = CloudQuery