cloud-object.js 3.6 KB

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