Эх сурвалжийг харах

feat: new server case code

RyaneMax 8 сар өмнө
parent
commit
9071399edc

+ 42 - 0
server/case/promise.js

@@ -0,0 +1,42 @@
+
+async function main(){
+    // setTimeout(() => {
+    //     console.log(1)
+    // }, 500);
+    // setTimeout(() => {
+    //     console.log(2)
+    // }, 200);
+    // setTimeout(() => {
+    //     console.log(3)
+    // }, 100);
+    // setTimeout(() => {
+    //     console.log(4)
+    // }, 1000);
+    // return
+
+    // waitSeconds(500,()=>{console.log(1)}) // 500
+    // waitSeconds(200,()=>{console.log(2)}) // 700
+    // waitSeconds(100,()=>{console.log(3)}) // 800
+    // waitSeconds(1000,()=>{console.log(4)}) // 1800
+    // return
+    
+    let res1 = await waitSeconds(500,()=>{console.log(1)}) // 500
+    console.log(res1)
+    let res2 = await waitSeconds(200,()=>{console.log(2)}) // 700
+    console.log(res2)
+    let res3 = await waitSeconds(100,()=>{console.log(3)}) // 800
+    console.log(res3)
+    let res4 = await waitSeconds(1000,()=>{console.log(4)}) // 1800
+    console.log(res4)
+}
+
+function waitSeconds(duration,handle) {
+    return new Promise((resolve, reject) => {
+        setTimeout(() => {
+            handle()
+            resolve(`等待了${duration}ms`);
+        }, duration);
+    });
+}
+
+main()

+ 40 - 0
server/fmode-parse-test.js

@@ -0,0 +1,40 @@
+const { ParseObject } = require("./fmode-parse");
+
+async function main(){
+    // testFind()
+    // testSave()
+    testUpdate()
+}
+
+async function testUpdate(){
+    // 根据ID获取羊驼
+    let yangtuo = new ParseObject("Pet");
+    yangtuo = await yangtuo.get("XOeof1ppOS")
+    console.log(yangtuo)
+    // 根据有ID的对象,保存新属性
+    yangtuo.set({
+        price:300
+    })
+    yangtuo.save()
+
+}
+async function testFind(){
+        // 查询测试
+        let Pet = new ParseObject("Pet")
+        let list = await Pet.findAll()
+        console.log(list)
+    
+}
+async function testSave(){
+       // 创建测试
+       let yangtuo = new ParseObject("Pet")
+       yangtuo.set({
+           name:"小羊驼",
+           type:"羊驼",
+           price:30000
+       })
+       yangtuo = await yangtuo.save();
+       console.log(yangtuo) // id XOeof1ppOS
+       console.log(yangtuo.toJSON())
+}
+main()

+ 110 - 0
server/fmode-parse.js

@@ -0,0 +1,110 @@
+class ParseObject{
+    className
+    id
+    serverURL = "http://web2023.fmode.cn:9999/parse"
+    data = {}
+    toJSON(){
+        return this.data
+    }
+    constructor(className){
+        this.className = className
+    }
+    async get(id){
+        let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
+            "headers": {
+                "x-parse-application-id": "dev"
+              },
+            "body": null,
+            "method": "GET",
+            "mode": "cors",
+            "credentials": "omit"
+          });
+          if(response?.status=="200"){
+            let json = await response.json()
+            this.id = json?.objectId;
+            delete json.objectId
+            delete json.createdAt
+            delete json.updatedAt
+            this.data = json;
+            return this
+          }else{
+            return []
+          }
+    }
+    async findAll(){
+        let response = await fetch(this.serverURL+"/classes/"+this.className, {
+            "headers": {
+                "x-parse-application-id": "dev"
+              },
+            "body": null,
+            "method": "GET",
+            "mode": "cors",
+            "credentials": "omit"
+          });
+          // console.log(response)
+          // return []
+          if(response?.status=="200"){
+            let json = await response.json()
+            // console.log(json)
+            return json?.results || []
+          }else{
+            return []
+          }
+    }
+    set(data){
+        this.data = data
+    }
+    async save(){
+        let body = JSON.stringify(this.data)
+        let url = this.serverURL+"/classes/"+this.className // 创建URL
+        let method = "POST"
+        if(this.id){
+            url = url + "/" + this.id // 更新URL
+            method = "PUT"
+        }
+        console.log(url,method,body)
+        let response = await fetch(url, {
+            "headers": {
+                // "content-type": "text/plain;charset=UTF-8",
+                "x-parse-application-id": "dev"
+            },
+            "body": body,
+            "method": method,
+            "mode": "cors",
+            "credentials": "omit"
+        });
+        console.log(body)
+        let text = await response?.text();
+        console.log(text)
+        let json = await response?.json();
+        if(json?.objectId){
+            console.log(json)
+            this.id = json?.objectId
+            return this
+        }else{
+            return null
+        }
+    }
+    async delete(){
+        let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
+            "headers": {
+                "x-parse-application-id": "dev"
+              },
+            "body": null,
+            "method": "DELETE",
+            "mode": "cors",
+            "credentials": "omit"
+          });
+          if(response?.status=="200"){
+            let json = await response.json()
+            return json
+          }else{
+            return []
+          }
+    }
+}
+module.exports.ParseObject = ParseObject
+
+class ParseQuery{
+    
+}

+ 112 - 0
server/rest.js

@@ -0,0 +1,112 @@
+
+// Pet GET 获取全部宠物
+async function getPet(){
+  let response = await fetch("http://web2023.fmode.cn:9999/parse/classes/Pet?", {
+    "headers": {
+      "accept": "*/*",
+      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
+      "if-none-match": "W/\"19f-gvNjdCzTNrgDv/sckM9+BRABBVY\"",
+      "x-parse-application-id": "dev"
+    },
+    "referrer": "http://127.0.0.1:4040/",
+    "referrerPolicy": "strict-origin-when-cross-origin",
+    "body": null,
+    "method": "GET",
+    "mode": "cors",
+    "credentials": "omit"
+  });
+  // console.log(response)
+  // return []
+  if(response?.status=="200"){
+    let json = await response.json()
+    // console.log(json)
+    return json?.results || []
+  }else{
+    return []
+  }
+}
+
+async function main(){
+  // let petList = await getPet()
+  // console.log(petList)
+  // let list = await ClassesGet("Pet")
+  // console.log(list)
+
+  let list = await ClassesGet("Student")
+  console.log(list)
+}
+main()
+
+// 通过函数封装,实现多个表的查询
+async function ClassesGet(className){
+  let response = await fetch("http://web2023.fmode.cn:9999/parse/classes/"+className, {
+    "headers": {
+      "accept": "*/*",
+      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
+      "if-none-match": "W/\"19f-gvNjdCzTNrgDv/sckM9+BRABBVY\"",
+      "x-parse-application-id": "dev"
+    },
+    "referrer": "http://127.0.0.1:4040/",
+    "referrerPolicy": "strict-origin-when-cross-origin",
+    "body": null,
+    "method": "GET",
+    "mode": "cors",
+    "credentials": "omit"
+  });
+  // console.log(response)
+  // return []
+  if(response?.status=="200"){
+    let json = await response.json()
+    // console.log(json)
+    return json?.results || []
+  }else{
+    return []
+  }
+}
+
+// // Pet POST 创建宠物
+// fetch("http://web2023.fmode.cn:9999/parse/classes/Pet", {
+//   "headers": {
+//     "accept": "*/*",
+//     "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
+//     "content-type": "text/plain;charset=UTF-8",
+//     "x-parse-application-id": "dev"
+//   },
+//   "referrer": "http://127.0.0.1:4040/",
+//   "referrerPolicy": "strict-origin-when-cross-origin",
+//   "body": '{"name":"小强","type":"蟑螂","price":0.2}',
+//   "method": "POST",
+//   "mode": "cors",
+//   "credentials": "omit"
+// });
+
+// // Pet PUT 修改
+// fetch("http://web2023.fmode.cn:9999/parse/classes/Pet/bl34OtuBYm", {
+//   "headers": {
+//     "accept": "*/*",
+//     "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
+//     "content-type": "text/plain;charset=UTF-8",
+//     "x-parse-application-id": "dev"
+//   },
+//   "referrer": "http://127.0.0.1:4040/",
+//   "referrerPolicy": "strict-origin-when-cross-origin",
+//   "body": "{\"price\":0.09}",
+//   "method": "PUT",
+//   "mode": "cors",
+//   "credentials": "omit"
+// });
+
+// // Pet DELETE 删除
+// fetch("http://web2023.fmode.cn:9999/parse/classes/Pet/bl34OtuBYm", {
+//   "headers": {
+//     "accept": "*/*",
+//     "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
+//     "x-parse-application-id": "dev"
+//   },
+//   "referrer": "http://127.0.0.1:4040/",
+//   "referrerPolicy": "strict-origin-when-cross-origin",
+//   "body": null,
+//   "method": "DELETE",
+//   "mode": "cors",
+//   "credentials": "omit"
+// });