Przeglądaj źródła

feat: ncloud local serverURL

fmode 17 godzin temu
rodzic
commit
d5f659229c
2 zmienionych plików z 80 dodań i 16 usunięć
  1. 58 0
      docs-prod/shop-schema.md
  2. 22 16
      src/lib/ncloud.ts

+ 58 - 0
docs-prod/shop-schema.md

@@ -0,0 +1,58 @@
+
+
+```plantuml
+@startuml
+' 设置类图方向为从左到右
+left to right direction
+
+' 定义用户表实体
+class User {
+  + user_id : INT [PK]
+  username : VARCHAR
+}
+
+' 定义商品表实体
+class Goods {
+  + goods_id : INT [PK]
+  goods_name : VARCHAR
+  unit : VARCHAR
+  unit_price : DECIMAL
+}
+
+' 定义订单主表实体
+class Order {
+  + order_id : VARCHAR [PK]
+  order_date : DATETIME
+  user_id : INT [FK]
+}
+
+' 定义订单明细表实体
+class OrderDetail {
+  + detail_id : INT [PK]
+  order_id : VARCHAR [FK]
+  goods_id : INT [FK]
+  quantity : INT
+  unit_price : DECIMAL
+}
+
+' 定义关联关系
+User "1" --> "0..*" Order : 创建
+Order "1" --> "1..*" OrderDetail : 包含
+Goods "1" --> "0..*" OrderDetail : 被购买
+
+' 添加范式说明注释
+note top of Goods
+  第三范式实现:
+  goods_name/unit 独立存储在商品表
+  通过 goods_id 外键关联消除冗余
+end note
+
+note top of OrderDetail
+  第三范式实现:
+  存储下单时的 unit_price(快照)
+  总价 = quantity * unit_price
+  不存储冗余的 goods_name/unit
+end note
+
+@enduml
+```

+ 22 - 16
src/lib/ncloud.ts

@@ -1,4 +1,10 @@
 // CloudObject.ts
+
+let serverURL = `https://dev.fmode.cn/parse`;
+if(location.protocol=="http:"){
+   serverURL = `http://dev.fmode.cn:1337/parse`;
+}
+
 export class CloudObject {
     className: string;
     id: string | null = null;
@@ -29,7 +35,7 @@ export class CloudObject {
 
     async save() {
         let method = "POST";
-        let url = `https://dev.fmode.cn/parse/classes/${this.className}`;
+        let url = serverURL + `/classes/${this.className}`;
 
         // 更新
         if (this.id) {
@@ -61,7 +67,7 @@ export class CloudObject {
 
     async destroy() {
         if (!this.id) return;
-        const response = await fetch(`https://dev.fmode.cn/parse/classes/${this.className}/${this.id}`, {
+        const response = await fetch(serverURL + `/classes/${this.className}/${this.id}`, {
             headers: {
                 "x-parse-application-id": "dev"
             },
@@ -117,7 +123,7 @@ export class CloudQuery {
     }
 
     async get(id: string) {
-        const url = `https://dev.fmode.cn/parse/classes/${this.className}/${id}?`;
+        const url = serverURL + `/classes/${this.className}/${id}?`;
 
         const response = await fetch(url, {
             headers: {
@@ -139,7 +145,7 @@ export class CloudQuery {
     }
 
     async find():Promise<Array<CloudObject>> {
-        let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
+        let url = serverURL + `/classes/${this.className}?`;
 
         let queryStr = ``
         Object.keys(this.queryParams).forEach(key=>{
@@ -154,7 +160,7 @@ export class CloudQuery {
             }
         })
         // if (Object.keys(this.queryParams["where"]).length) {
-            
+
         // }
 
         const response = await fetch(url, {
@@ -176,7 +182,7 @@ export class CloudQuery {
 
 
     async first() {
-        let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
+        let url = serverURL + `/classes/${this.className}?`;
 
         if (Object.keys(this.queryParams["where"]).length) {
             const whereStr = JSON.stringify(this.queryParams["where"]);
@@ -236,7 +242,7 @@ export class CloudUser extends CloudObject {
             return null;
         }
         return this;
-        // const response = await fetch(`https://dev.fmode.cn/parse/users/me`, {
+        // const response = await fetch(serverURL + `/users/me`, {
         //     headers: {
         //         "x-parse-application-id": "dev",
         //         "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
@@ -254,7 +260,7 @@ export class CloudUser extends CloudObject {
 
     /** 登录 */
     async login(username: string, password: string):Promise<CloudUser|null> {
-        const response = await fetch(`https://dev.fmode.cn/parse/login`, {
+        const response = await fetch(serverURL + `/login`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "Content-Type": "application/json"
@@ -268,7 +274,7 @@ export class CloudUser extends CloudObject {
             console.error(result?.error);
             return null;
         }
-        
+
         // 设置用户信息
         this.id = result?.objectId;
         this.sessionToken = result?.sessionToken;
@@ -286,7 +292,7 @@ export class CloudUser extends CloudObject {
             return;
         }
 
-        const response = await fetch(`https://dev.fmode.cn/parse/logout`, {
+        const response = await fetch(serverURL + `/logout`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "x-parse-session-token": this.sessionToken
@@ -324,7 +330,7 @@ export class CloudUser extends CloudObject {
             ...additionalData // 合并额外的用户数据
         };
 
-        const response = await fetch(`https://dev.fmode.cn/parse/users`, {
+        const response = await fetch(serverURL + `/users`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "Content-Type": "application/json"
@@ -351,14 +357,14 @@ export class CloudUser extends CloudObject {
 
     override async save() {
         let method = "POST";
-        let url = `https://dev.fmode.cn/parse/users`;
-    
+        let url = serverURL + `/users`;
+
         // 更新用户信息
         if (this.id) {
             url += `/${this.id}`;
             method = "PUT";
         }
-    
+
         let data:any = JSON.parse(JSON.stringify(this.data))
         delete data.createdAt
         delete data.updatedAt
@@ -377,7 +383,7 @@ export class CloudUser extends CloudObject {
             mode: "cors",
             credentials: "omit"
         });
-    
+
         const result = await response?.json();
         if (result?.error) {
             console.error(result?.error);
@@ -417,4 +423,4 @@ export class CloudApi{
         let json = await response.json();
         return json
     }
-}
+}