ncloud.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. greaterThan(key,value){
  20. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  21. this.whereOptions[key]["$gt"] = value
  22. }
  23. greaterThanAndEqualTo(key,value){
  24. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  25. this.whereOptions[key]["$gte"] = value
  26. }
  27. lessThan(key,value){
  28. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  29. this.whereOptions[key]["$lt"] = value
  30. }
  31. lessThanAndEqualTo(key,value){
  32. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  33. this.whereOptions[key]["$lte"] = value
  34. }
  35. // 更新
  36. async save(){
  37. let method = "POST"
  38. let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className //默认
  39. if(this.id){
  40. url = "/"+this.id
  41. method = "PUT"
  42. }
  43. let body = JSON.stringify(this.data)
  44. let response = await fetch(url, {
  45. "headers": {
  46. "content-type": "application/json;charset=UTF-8",
  47. "x-parse-application-id": "dev"
  48. },
  49. "body": body,
  50. "method": "POST",
  51. "mode": "cors",
  52. "credentials": "omit"
  53. });
  54. let result = await response?.json();
  55. if(result?.objectId){this.id = result?.objectId}
  56. return this
  57. }
  58. // 删除
  59. async destory(){
  60. if(!this.id) return
  61. let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.id, {
  62. "headers": {
  63. "x-parse-application-id": "dev"
  64. },
  65. "body": null,
  66. "method": "DELETE",
  67. "mode": "cors",
  68. "credentials": "omit"
  69. });
  70. let result = await response?.json();
  71. if(result){
  72. this.id = null
  73. }
  74. return true
  75. }
  76. }
  77. // 查询
  78. class CloudQuery{
  79. className
  80. constructor(className){
  81. this.className = className
  82. }
  83. whereOptions = {}
  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. }
  124. module.exports.CloudObject = CloudObject
  125. module.exports.CloudQuery = CloudQuery