Răsfoiți Sursa

Update files: src/api/deviceApi.js

D8D AI 1 an în urmă
părinte
comite
d8135060a3
1 a modificat fișierele cu 40 adăugiri și 31 ștergeri
  1. 40 31
      src/api/deviceApi.js

+ 40 - 31
src/api/deviceApi.js

@@ -2,6 +2,32 @@ import axios from 'axios';
 
 const API_BASE_URL = 'http://your-device-api-url.com'; // 替换为实际的设备 API URL
 
+// 模拟数据存储
+let mockData = [
+  {
+    personId: '1',
+    name: '张三',
+    gender: '男',
+    age: 30,
+    birthDate: '1994-01-01',
+    maritalStatus: '已婚',
+    isBaptized: true,
+    baptismDate: '2010-05-15',
+    contact: '13800138000'
+  },
+  {
+    personId: '2',
+    name: '李四',
+    gender: '女',
+    age: 25,
+    birthDate: '1999-03-15',
+    maritalStatus: '未婚',
+    isBaptized: false,
+    baptismDate: null,
+    contact: '13900139000'
+  }
+];
+
 export const deviceApi = {
   getAllPersons: async () => {
     try {
@@ -10,30 +36,7 @@ export const deviceApi = {
       return new Promise((resolve) => {
         setTimeout(() => {
           console.log('模拟 API 返回数据');
-          resolve([
-            {
-              personId: '1',
-              name: '张三',
-              gender: '男',
-              age: 30,
-              birthDate: '1994-01-01',
-              maritalStatus: '已婚',
-              isBaptized: true,
-              baptismDate: '2010-05-15',
-              contact: '13800138000'
-            },
-            {
-              personId: '2',
-              name: '李四',
-              gender: '女',
-              age: 25,
-              birthDate: '1999-03-15',
-              maritalStatus: '未婚',
-              isBaptized: false,
-              baptismDate: null,
-              contact: '13900139000'
-            }
-          ]);
+          resolve([...mockData]); // 返回模拟数据的深拷贝
         }, 1000);
       });
     } catch (error) {
@@ -48,10 +51,12 @@ export const deviceApi = {
       // 模拟添加操作
       return new Promise((resolve) => {
         setTimeout(() => {
-          resolve({
+          const newPerson = {
             ...personInfo,
-            personId: Date.now().toString(), // 模拟生成ID
-          });
+            personId: (mockData.length + 1).toString(),
+          };
+          mockData.push(newPerson);
+          resolve(newPerson);
         }, 500);
       });
     } catch (error) {
@@ -66,10 +71,13 @@ export const deviceApi = {
       // 模拟更新操作
       return new Promise((resolve) => {
         setTimeout(() => {
-          resolve({
-            ...personInfo,
-            personId,
-          });
+          const index = mockData.findIndex(p => p.personId === personId);
+          if (index !== -1) {
+            mockData[index] = { ...personInfo, personId };
+            resolve(mockData[index]);
+          } else {
+            throw new Error('未找到指定会友');
+          }
         }, 500);
       });
     } catch (error) {
@@ -84,6 +92,7 @@ export const deviceApi = {
       // 模拟删除操作
       return new Promise((resolve) => {
         setTimeout(() => {
+          mockData = mockData.filter(p => p.personId !== personId);
           resolve({ success: true });
         }, 500);
       });