ncloud.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. class CloudObject{//创建
  2. id
  3. className
  4. data
  5. constructor(className){
  6. this.className=className
  7. this.data={}
  8. }
  9. set(json){
  10. object.keys(json).forEach(key=>{
  11. if(["objectId","id","createAt","updatedAt","ACL"].indexOf(key)>-1){
  12. return
  13. }
  14. this.data[key]=json[key]
  15. })
  16. }
  17. get(key){
  18. return this.data[key] || null
  19. }
  20. async save(){
  21. let method = this.id ? "PUT" : "POST"; // 如果有id则为PUT,否则为POST
  22. let url="http://dev.fmode.cn:1337/parse/classes/"+this.className
  23. if(this.id){
  24. url=url+"/"+this.id
  25. }
  26. let body = JSON.stringify(this.data);
  27. let response = await fetch(url, {
  28. "headers": {
  29. "content-type": "application/json;charset=UTF-8",
  30. "x-parse-application-id": "dev"
  31. },
  32. "body": body,
  33. "method": "POST",
  34. "mode": "cors",
  35. "credentials": "omit"
  36. });
  37. let result = await response?.json();
  38. if(result?.objectId){
  39. this.id=result?.objectId
  40. }
  41. return this
  42. }
  43. async destory(){
  44. let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className + "/";
  45. if(!this.id) return
  46. let response = await fetch(url+this.id, {
  47. "headers": {
  48. "x-parse-application-id": "dev"
  49. },
  50. "body": null,
  51. "method": "DELETE",
  52. "mode": "cors",
  53. "credentials": "omit"
  54. });
  55. let result=response?.json();
  56. if(result){
  57. this.id=null
  58. }
  59. return true
  60. }
  61. }
  62. class CloudQuery{//查询
  63. className
  64. constructor(className){
  65. this.className=className
  66. }
  67. async get(id){
  68. let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?", {
  69. "headers": {
  70. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  71. "x-parse-application-id": "dev"
  72. },
  73. "body": null,
  74. "method": "GET",
  75. "mode": "cors",
  76. "credentials": "omit"
  77. });
  78. let json = await response?.json();
  79. return json || {}
  80. }
  81. async find(){
  82. let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"?", {
  83. "headers": {
  84. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  85. "x-parse-application-id": "dev"
  86. },
  87. "body": null,
  88. "method": "GET",
  89. "mode": "cors",
  90. "credentials": "omit"
  91. });
  92. let json = await response?.json();
  93. return json?.results || []
  94. }
  95. async first(){
  96. let results = await this.find();
  97. return results.length > 0 ? results[0] : null;
  98. }
  99. }