| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- const mockApi = {
- members: [
- { _id: '1', name: '张三', attendance: [] },
- { _id: '2', name: '李四', attendance: [] },
- ],
- devices: [
- { _id: '1', name: '设备1', deviceId: 'DEV001', status: 'online' },
- { _id: '2', name: '设备2', deviceId: 'DEV002', status: 'offline' },
- ],
- };
- const mockApiHandlers = {
- '/api/members': async () => mockApi.members,
- '/api/devices': async () => mockApi.devices,
- '/api/members': async (method, body) => {
- if (method === 'POST') {
- const newMember = { _id: String(mockApi.members.length + 1), name: body.name, attendance: [] };
- mockApi.members.push(newMember);
- return newMember;
- }
- return mockApi.members;
- },
- '/api/attendance': async (method, body) => {
- if (method === 'POST') {
- const { memberId, date } = body;
- const member = mockApi.members.find(m => m._id === memberId);
- if (member) {
- member.attendance.push(date);
- return member;
- }
- throw new Error('会员不存在');
- }
- },
- };
- async function callMockApi(url, method = 'GET', body = null) {
- const handler = mockApiHandlers[url.split('?')[0]];
- if (handler) {
- return await handler(method, body);
- }
- throw new Error('Not found');
- }
- window.mockApi = {
- fetch: async (url, options = {}) => {
- try {
- const result = await callMockApi(url, options.method, options.body ? JSON.parse(options.body) : null);
- return {
- ok: true,
- json: async () => result,
- };
- } catch (error) {
- return {
- ok: false,
- status: 404,
- json: async () => ({ message: error.message }),
- };
- }
- }
- };
|