fmode-parse copy.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class ParseObject{
  2. className
  3. id
  4. serverURL = "https://web2023.fmode.cn/parse"
  5. data = {}
  6. toJSON(){
  7. return this.data
  8. }
  9. constructor(className){
  10. this.className = className
  11. }
  12. async get(id){
  13. let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
  14. "headers": {
  15. "x-parse-application-id": "dev"
  16. },
  17. "body": null,
  18. "method": "GET",
  19. "mode": "cors",
  20. "credentials": "omit"
  21. });
  22. if(response?.status=="200"){
  23. let json = await response.json()
  24. this.id = json?.objectId;
  25. delete json.objectId
  26. delete json.createdAt
  27. delete json.updatedAt
  28. this.data = json;
  29. return this
  30. }else{
  31. return []
  32. }
  33. }
  34. async findAll(){
  35. let response = await fetch(this.serverURL+"/classes/"+this.className, {
  36. "headers": {
  37. "x-parse-application-id": "dev"
  38. },
  39. "body": null,
  40. "method": "GET",
  41. "mode": "cors",
  42. "credentials": "omit"
  43. });
  44. console.log(response)
  45. return []
  46. if(response?.status=="200"){
  47. let json = await response.json()
  48. // console.log(json)
  49. return json?.results || []
  50. }else{
  51. return []
  52. }
  53. }
  54. set(data){
  55. this.data = data
  56. }
  57. async save(){
  58. let body = JSON.stringify(this.data)
  59. let url = this.serverURL+"/classes/"+this.className // 创建URL
  60. let method = "POST"
  61. if(this.id){
  62. url = url + "/" + this.id // 更新URL
  63. method = "PUT"
  64. }
  65. console.log(url,method,body)
  66. let response = await fetch(url, {
  67. "headers": {
  68. // "content-type": "text/plain;charset=UTF-8",
  69. "x-parse-application-id": "dev"
  70. },
  71. "body": body,
  72. "method": method,
  73. "mode": "cors",
  74. "credentials": "omit"
  75. });
  76. console.log(body)
  77. let text = await response?.text();
  78. console.log(text)
  79. let json = await response?.json();
  80. if(json?.objectId){
  81. console.log(json)
  82. this.id = json?.objectId
  83. return this
  84. }else{
  85. return null
  86. }
  87. }
  88. async delete(){
  89. let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
  90. "headers": {
  91. "x-parse-application-id": "dev"
  92. },
  93. "body": null,
  94. "method": "DELETE",
  95. "mode": "cors",
  96. "credentials": "omit"
  97. });
  98. if(response?.status=="200"){
  99. let json = await response.json()
  100. return json
  101. }else{
  102. return []
  103. }
  104. }
  105. }
  106. module.exports.ParseObject = ParseObject
  107. class ParseQuery{
  108. }