feat:对接了strapi 接口

This commit is contained in:
2025-05-03 19:51:37 +08:00
parent 3e2aa46ea1
commit f31c30116a
17 changed files with 562 additions and 369 deletions
-173
View File
@@ -1,173 +0,0 @@
import cuid from "cuid";
// import getDb from "../dbServer.mjs";
import _ from "lodash";
import { getDb } from "funjia-server/lib/funjia-server.mjs";
const getEmployee = (router) => {
router.post('/api/employee/list', async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const db = await getDb();
const body = ctx.request.body;
const data = db.data?.employee?.filter((item) => {
let isMatch = true;
for (let field in body) {
if (["page", "pageSize"].includes(field)) {
continue;
}
if (typeof (item[field]) == "string") {
isMatch = item[field]?.indexOf?.(body[field]) > -1;
} else if (typeof (item[field]) == "number") {
isMatch = item[field] == body[field];
}
if (!isMatch)
break;
}
return isMatch;
});
const {
page,
pageSize
} = body;
let batchData = data;
if (page && pageSize) {
batchData = data.slice((page - 1) * pageSize, page * pageSize);
}
ctx.body = {
code: 0,
data: batchData || [],
total: data.length
};
});
router.get('/api/employee', async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const db = await getDb();
const data = db.data.employee;
const query = ctx.request.query;
console.log("参数:", query, ctx.request.body, ctx.query);
let detail = {};
if (query.id && data) {
detail = data.find((item) => {
return item.id == query.id;
});
}
ctx.body = {
code: 0,
data: detail || {}
};
});
router.post('/api/employee/add', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.employee)
db.data.employee = [];
const {
employee
} = db.data;
const data = ctx.request.body;
console.log("add:", data);
data.id = cuid();
data.createTime = new Date();
employee.push(data)
// Write db.data content to db.json
await db.write()
ctx.body = {
code: 0,
};
});
router.post('/api/employee/upd', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.employee)
db.data = {
employee: []
}
const {
employee
} = db.data;
const data = ctx.request.body;
const id = data.id;
console.log("数据Id", id);
if (id && employee) {
const currentDetail = employee.find(item => item.id == id);
if (currentDetail) {
_.merge(currentDetail, data);
}
// Write db.data content to db.json
await db.write()
}
ctx.body = {
code: 0,
};
});
router.post('/api/employee/delete', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.employee)
db.data = {
employee: []
}
let {
employee
} = db.data;
const data = ctx.request.body;
const ids = data.ids;
if (ids?.length > 0 && employee) {
let id = null;
for (let i = 0; i < ids.length; i++) {
id = ids[i];
const idx = employee.findIndex(item => item.id == id);
if (idx != -1) {
employee.splice(idx, 1);
}
}
// Write db.data content to db.json
await db.write()
}
ctx.body = {
code: 0,
};
});
router.post('/api/employee/login', async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const db = await getDb();
const data = db.data.employee;
const params = ctx.request.body;
let detail = {};
if (data) {
detail = data.find((item) => {
return item.userName == params.userName && item.pwd == params.pwd;
});
}
const code = detail ? 0 : 1;
ctx.body = {
code,
data: detail || {}
};
});
}
export default getEmployee;