import { http, HttpResponse, delay } from 'msw'; import { mockDictTypes, mockDictDatas } from '../data'; import type { MockDictType, MockDictData } from '../data'; // Mutable in-memory stores let dictTypes = [...mockDictTypes]; let dictTypeNextId = Math.max(...dictTypes.map((d) => d.id)) + 1; let dictDatas = [...mockDictDatas]; let dictDataNextId = Math.max(...dictDatas.map((d) => d.id)) + 1; function paginate(list: T[], pageNo: number, pageSize: number) { const start = (pageNo - 1) * pageSize; return { list: list.slice(start, start + pageSize), total: list.length, }; } export const dictHandlers = [ // ==================== Dict Type ==================== // Dict Type - Page query http.get('/admin-api/system/dict-type/page', async ({ request }) => { await delay(300); const url = new URL(request.url); const pageNo = Number(url.searchParams.get('pageNo') || 1); const pageSize = Number(url.searchParams.get('pageSize') || 10); const name = url.searchParams.get('name') || ''; const type = url.searchParams.get('type') || ''; const status = url.searchParams.get('status'); let filtered = [...dictTypes]; if (name) { filtered = filtered.filter((d) => d.name.includes(name)); } if (type) { filtered = filtered.filter((d) => d.type.includes(type)); } if (status !== null && status !== '') { filtered = filtered.filter((d) => d.status === Number(status)); } return HttpResponse.json({ code: 0, data: paginate(filtered, pageNo, pageSize), message: 'ok', }); }), // Dict Type - Get by ID http.get('/admin-api/system/dict-type/get', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); const dictType = dictTypes.find((d) => d.id === id); if (!dictType) { return HttpResponse.json( { code: 1, data: null, message: '字典类型不存在' }, ); } return HttpResponse.json({ code: 0, data: dictType, message: 'ok', }); }), // Dict Type - Create http.post('/admin-api/system/dict-type/create', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const typeExists = dictTypes.find((d) => d.type === body.type); if (typeExists) { return HttpResponse.json( { code: 1, data: null, message: '字典类型已存在' }, ); } const newDictType: MockDictType = { id: dictTypeNextId++, name: body.name as string, type: body.type as string, status: (body.status as number) ?? 0, remark: (body.remark as string) || '', createTime: new Date().toISOString().replace('T', ' ').slice(0, 19), }; dictTypes.push(newDictType); return HttpResponse.json({ code: 0, data: newDictType.id, message: 'ok' }); }), // Dict Type - Update http.put('/admin-api/system/dict-type/update', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const index = dictTypes.findIndex((d) => d.id === body.id); if (index === -1) { return HttpResponse.json( { code: 1, data: null, message: '字典类型不存在' }, ); } // Check type uniqueness (excluding self) if (body.type) { const dup = dictTypes.find((d) => d.type === body.type && d.id !== body.id); if (dup) { return HttpResponse.json( { code: 1, data: null, message: '字典类型已存在' }, ); } } dictTypes[index] = { ...dictTypes[index], ...(body.name !== undefined && { name: body.name as string }), ...(body.type !== undefined && { type: body.type as string }), ...(body.status !== undefined && { status: body.status as number }), ...(body.remark !== undefined && { remark: body.remark as string }), }; return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), // Dict Type - Delete http.delete('/admin-api/system/dict-type/delete', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); const dictType = dictTypes.find((d) => d.id === id); if (!dictType) { return HttpResponse.json( { code: 1, data: null, message: '字典类型不存在' }, ); } // Check if there are dict data entries for this type const hasData = dictDatas.some((d) => d.dictType === dictType.type); if (hasData) { return HttpResponse.json( { code: 1, data: null, message: '该字典类型下存在字典数据,不允许删除' }, ); } dictTypes = dictTypes.filter((d) => d.id !== id); return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), // Dict Type - Simple list http.get('/admin-api/system/dict-type/simple-list', async () => { await delay(200); const simpleList = dictTypes.map((d) => ({ id: d.id, name: d.name, type: d.type, })); return HttpResponse.json({ code: 0, data: simpleList, message: 'ok', }); }), // ==================== Dict Data ==================== // Dict Data - Page query (filtered by dictType) http.get('/admin-api/system/dict-data/page', async ({ request }) => { await delay(300); const url = new URL(request.url); const pageNo = Number(url.searchParams.get('pageNo') || 1); const pageSize = Number(url.searchParams.get('pageSize') || 10); const dictType = url.searchParams.get('dictType') || ''; const label = url.searchParams.get('label') || ''; const status = url.searchParams.get('status'); let filtered = [...dictDatas]; if (dictType) { filtered = filtered.filter((d) => d.dictType === dictType); } if (label) { filtered = filtered.filter((d) => d.label.includes(label)); } if (status !== null && status !== '') { filtered = filtered.filter((d) => d.status === Number(status)); } return HttpResponse.json({ code: 0, data: paginate(filtered, pageNo, pageSize), message: 'ok', }); }), // Dict Data - Get by ID http.get('/admin-api/system/dict-data/get', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); const dictData = dictDatas.find((d) => d.id === id); if (!dictData) { return HttpResponse.json( { code: 1, data: null, message: '字典数据不存在' }, ); } return HttpResponse.json({ code: 0, data: dictData, message: 'ok', }); }), // Dict Data - Create http.post('/admin-api/system/dict-data/create', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const newDictData: MockDictData = { id: dictDataNextId++, sort: (body.sort as number) ?? 0, label: body.label as string, value: body.value as string, dictType: body.dictType as string, status: (body.status as number) ?? 0, colorType: (body.colorType as string) || '', cssClass: (body.cssClass as string) || '', remark: (body.remark as string) || '', createTime: new Date().toISOString().replace('T', ' ').slice(0, 19), }; dictDatas.push(newDictData); return HttpResponse.json({ code: 0, data: newDictData.id, message: 'ok' }); }), // Dict Data - Update http.put('/admin-api/system/dict-data/update', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const index = dictDatas.findIndex((d) => d.id === body.id); if (index === -1) { return HttpResponse.json( { code: 1, data: null, message: '字典数据不存在' }, ); } dictDatas[index] = { ...dictDatas[index], ...(body.sort !== undefined && { sort: body.sort as number }), ...(body.label !== undefined && { label: body.label as string }), ...(body.value !== undefined && { value: body.value as string }), ...(body.dictType !== undefined && { dictType: body.dictType as string }), ...(body.status !== undefined && { status: body.status as number }), ...(body.colorType !== undefined && { colorType: body.colorType as string }), ...(body.cssClass !== undefined && { cssClass: body.cssClass as string }), ...(body.remark !== undefined && { remark: body.remark as string }), }; return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), // Dict Data - Delete http.delete('/admin-api/system/dict-data/delete', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); dictDatas = dictDatas.filter((d) => d.id !== id); return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), // Dict Data - Simple list (all data, used by dict store) http.get('/admin-api/system/dict-data/simple-list', async () => { await delay(200); const simpleList = dictDatas.map((d) => ({ id: d.id, label: d.label, value: d.value, dictType: d.dictType, status: d.status, colorType: d.colorType, cssClass: d.cssClass, })); return HttpResponse.json({ code: 0, data: simpleList, message: 'ok', }); }), ];