ncloud.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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://dev.fmode.cn:1337/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. // "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?.error){
  43. // console.error(result?.error)
  44. // }
  45. // if(result?.objectId){this.id = result?.objectId}
  46. // return this
  47. // }
  48. // async destory(){
  49. // if(!this.id) return
  50. // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Doctor/"+this.id, {
  51. // "headers": {
  52. // "x-parse-application-id": "dev"
  53. // },
  54. // "body": null,
  55. // "method": "DELETE",
  56. // "mode": "cors",
  57. // "credentials": "omit"
  58. // });
  59. // let result = await response?.json();
  60. // if(result){
  61. // this.id = null
  62. // }
  63. // return true
  64. // }
  65. // }
  66. // class CloudQuery{
  67. // className
  68. // constructor(className){
  69. // this.className = className
  70. // }
  71. // whereOptions = {}
  72. // greaterThan(key,value){
  73. // if(!this.whereOptions[key]) this.whereOptions[key] = {}
  74. // this.whereOptions[key]["$gt"] = value
  75. // }
  76. // greaterThanAndEqualTo(key,value){
  77. // if(!this.whereOptions[key]) this.whereOptions[key] = {}
  78. // this.whereOptions[key]["$gte"] = value
  79. // }
  80. // lessThan(key,value){
  81. // if(!this.whereOptions[key]) this.whereOptions[key] = {}
  82. // this.whereOptions[key]["$lt"] = value
  83. // }
  84. // lessThanAndEqualTo(key,value){
  85. // if(!this.whereOptions[key]) this.whereOptions[key] = {}
  86. // this.whereOptions[key]["$lte"] = value
  87. // }
  88. // equalTo(key,value){
  89. // this.whereOptions[key] = value
  90. // }
  91. // async get(id){
  92. // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"
  93. // let response = await fetch(url, {
  94. // "headers": {
  95. // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  96. // "x-parse-application-id": "dev"
  97. // },
  98. // "body": null,
  99. // "method": "GET",
  100. // "mode": "cors",
  101. // "credentials": "omit"
  102. // });
  103. // let json = await response?.json();
  104. // return json || {}
  105. // }
  106. // async find(){
  107. // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  108. // if(Object.keys(this.whereOptions)?.length){
  109. // let whereStr = JSON.stringify(this.whereOptions)
  110. // url += `where=${whereStr}`
  111. // }
  112. // let response = await fetch(url, {
  113. // "headers": {
  114. // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  115. // "x-parse-application-id": "dev"
  116. // },
  117. // "body": null,
  118. // "method": "GET",
  119. // "mode": "cors",
  120. // "credentials": "omit"
  121. // });
  122. // let json = await response?.json();
  123. // return json?.results || []
  124. // }
  125. // async first(){
  126. // let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  127. // if(Object.keys(this.whereOptions)?.length){
  128. // let whereStr = JSON.stringify(this.whereOptions)
  129. // url += `where=${whereStr}`
  130. // }
  131. // let response = await fetch(url, {
  132. // "headers": {
  133. // "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  134. // "x-parse-application-id": "dev"
  135. // },
  136. // "body": null,
  137. // "method": "GET",
  138. // "mode": "cors",
  139. // "credentials": "omit"
  140. // });
  141. // let json = await response?.json();
  142. // let exists = json?.results?.[0] || null
  143. // if(exists){
  144. // let existsObject = new CloudObject(this.className)
  145. // existsObject.set(exists)
  146. // existsObject.id = exists.objectId
  147. // existsObject.createdAt = exists.createdAt
  148. // existsObject.updatedAt = exists.updatedAt
  149. // return existsObject
  150. // }
  151. // }
  152. // }
  153. // module.exports.CloudObject = CloudObject
  154. // module.exports.CloudQuery = CloudQuery
  155. class CloudObject{
  156. id
  157. ClassName
  158. data={}
  159. constructor(ClassName)
  160. {
  161. this.ClassName=ClassName
  162. }
  163. toPointer(){
  164. return {"_type":"Pointer","ClassName":this.ClassName,"objectId":this.id}
  165. }
  166. // 界面给data传数据
  167. set(json){
  168. Object.keys(json).forEach(key=>{
  169. if(["objectId","id","createdAt","updateAt","ACL"].indexOf(key)>-1)
  170. {
  171. return
  172. }
  173. this.data[key]=json[key]
  174. })
  175. }
  176. // data返回数据给界面
  177. get(){
  178. return this.data[key] || null
  179. }
  180. // data数据传给数据库,
  181. // 增数据 数据库生成id,给id赋值,返回整个对象
  182. // 改数据 修改非id数据,返回整个对象
  183. async save(){
  184. let url="http://dev.fmode.cn:1337/parse/classes/" +this.ClassName
  185. let method="POST"
  186. if(this.id)
  187. {
  188. method="PUT"
  189. url+="/"+this.id
  190. }
  191. let body=JSON.stringify(this.data)
  192. let response=await fetch(url, {
  193. "headers": {
  194. "content-type": "application/json;charset=UTF-8",
  195. "x-parse-application-id": "dev"
  196. },
  197. "body": body,
  198. "method": method,
  199. "mode": "cors",
  200. "credentials": "omit"
  201. });
  202. let json= await response?.json();
  203. if(json?.objectId){this.id=json?.objectId}
  204. return this
  205. }
  206. // 通过数据库删除,再删除id里可能存在的数据
  207. async destroy() {
  208. if(!this.id)return
  209. let response=await fetch("http://dev.fmode.cn:1337/parse/classes/Post/"+this.id, {
  210. "headers": {
  211. "x-parse-application-id": "dev"
  212. },
  213. "body": null,
  214. "method": "DELETE",
  215. "mode": "cors",
  216. "credentials": "omit"
  217. });
  218. let json=await response?.json()
  219. if(json)this.id=null
  220. return true;
  221. }
  222. }
  223. class CloudQuery
  224. {
  225. ClassName
  226. constructor(ClassName)
  227. {
  228. this.ClassName=ClassName
  229. }
  230. whereOptions={}
  231. equalTo(key,value)
  232. {
  233. this.whereOptions[key]=value
  234. }
  235. greaterThan(key,value)
  236. {
  237. if(!this.whereOptions[key])this.whereOptions[key]={}
  238. this.whereOptions[key]["$gt"]=value
  239. }
  240. greaterThanAndEqualTo(key,value)
  241. {
  242. if(!this.whereOptions[key])this.whereOptions[key]={}
  243. this.whereOptions[key]["$gte"]=value
  244. }
  245. lessThan(key,value)
  246. {
  247. if(!this.whereOptions[key])this.whereOptions[key]={}
  248. this.whereOptions[key]["$lt"]=value
  249. }
  250. lessThanAndEqualTo(key,value)
  251. {
  252. if(!this.whereOptions[key])this.whereOptions[key]={}
  253. this.whereOptions[key]["$lte"]=value
  254. }
  255. // 查询一个表中的特定id数据
  256. async get(id)
  257. {
  258. let response=await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"/"+id+"?", {
  259. "headers": {
  260. "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
  261. "x-parse-application-id": "dev"
  262. },
  263. "body": null,
  264. "method": "GET",
  265. "mode": "cors",
  266. "credentials": "omit"
  267. });
  268. let json=await response?.json();
  269. // console.log(json);
  270. return json || {}
  271. }
  272. // 查询一个表中的所有数据,高级查询
  273. async find(){
  274. let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"
  275. if(Object.keys(this.whereOptions).length)
  276. {
  277. let whereStr=JSON.stringify(this.whereOptions)
  278. url+=`where=${whereStr}`
  279. }
  280. let response=await fetch(url, {
  281. "headers": {
  282. "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
  283. "x-parse-application-id": "dev"
  284. },
  285. "body": null,
  286. "method": "GET",
  287. "mode": "cors",
  288. "credentials": "omit"
  289. });
  290. let json=await response?.json();
  291. // console.log(json);
  292. return json?.results || []
  293. }
  294. async first(){
  295. let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"
  296. if(Object.keys(this.whereOptions).length)
  297. {
  298. let whereStr=JSON.stringify(this.whereOptions)
  299. url+=`where=${whereStr}`
  300. }
  301. let response=await fetch(url, {
  302. "headers": {
  303. "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
  304. "x-parse-application-id": "dev"
  305. },
  306. "body": null,
  307. "method": "GET",
  308. "mode": "cors",
  309. "credentials": "omit"
  310. });
  311. let json=await response?.json();
  312. // console.log(json);
  313. let exists=json?.results?.[0] || null
  314. if(exists)
  315. {
  316. let existsObject=new CloudObject(this.ClassName)
  317. existsObject.set(exists)
  318. existsObject.id=exists.objectId
  319. existsObject.createdAt=exists.createdAt
  320. existsObject.updatedAt=exists.updatedAt
  321. return existsObject
  322. }
  323. }
  324. }
  325. module.exports.CloudObject=CloudObject
  326. module.exports.CloudQuery=CloudQuery