/** * @Author: helin3 * @Date: 2024-03-25 13:47:19 * @LastEditors: helin3 * @LastEditTime: 2024-03-25 16:16:18 * @Description: 增删改查模拟接口定义 * * 模拟接口文件路径遵循规范:`/mock/[模块名]/[功能名].mock.js` * 更详细的文档请参考:https://github.com/pengzhanbo/vite-plugin-mock-dev-server */ import { defineMock, backend, normalResponse, paginationReponse } from '@/../mock/shared/utils.js'; import { crudDataList } from './data/crud.data.js'; /** * 模拟API定义集合 */ export default defineMock([ /** * 获取crud列表模拟接口 * @method POST * @url /api/demo/crud/list * @body 返回分页形式的crud列表数据 */ { url: `${backend.demoService}/api/crud/list`, method: 'POST', enabled: true, // 是否启用接口模拟 body: () => paginationReponse(crudDataList.value, crudDataList.value.length), }, /** * 新增crud数据模拟接口 * @method POST * @url /api/demo/crud/save * @body 接收并保存传入的crud数据,成功时返回正常响应,失败时返回错误信息 */ { url: `${backend.demoService}/api/crud/save`, method: 'POST', body: ({ body }) => { const { id } = body; let success = false; if (id) { crudDataList.value.push(body); success = true; } return success ? normalResponse() : normalResponse(null, -1, '操作失败'); }, }, /** * 更新crud数据模拟接口 * @method POST * @url /api/demo/crud/update * @body 根据id查找并更新指定的crud数据,若找到则更新,否则新增 */ { url: `${backend.demoService}/api/crud/update`, method: 'POST', body: ({ body }) => { const temp = body; const index = crudDataList.value.findIndex((post) => post.id === temp.id); if (index !== -1) { crudDataList.value.splice(index, 1, temp); } else { crudDataList.value.push(temp); } return normalResponse(); }, }, /** * 删除crud数据模拟接口 * @method POST * @url /api/demo/crud/delete * @body 根据id删除指定的crud数据,删除成功时返回正常响应,失败时返回错误信息 */ { url: `${backend.demoService}/api/crud/delete`, method: 'POST', body: ({ body }) => { const { id } = body; let success = false; const index = crudDataList.value.findIndex((post) => post.id === id); if (index !== -1) { crudDataList.value.splice(index, 1); success = true; } return success ? normalResponse() : normalResponse(null, -1, '操作失败'); }, }, ]);