Files
funjia-admin-template-vue/server/routers/anki/notice.mjs
T

154 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cuid from "cuid";
// import getDb from "../dbServer.mjs";
import {getDb} from "funjia-server/lib/funjia-server.mjs";
import _ from "lodash";
const getNotice = (router) => {
router.post('/api/Notice/list', async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const db = await getDb();
const body = ctx.request.body;
const data = db.data?.Notice?.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.sort((a, b) => {
return a.sort - b.sort;
}) || [],
total: data.length
};
});
router.get('/api/Notice', async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const db = await getDb();
const data = db.data.Notice;
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/Notice/add', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.Notice)
db.data.Notice = [];
const {
Notice
} = db.data;
const data = ctx.request.body;
console.log("add:", data);
data.id = cuid();
data.createTime = new Date();
Notice.push(data)
// Write db.data content to db.json
await db.write()
ctx.body = {
code: 0,
};
});
router.post('/api/Notice/upd', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.Notice)
db.data = {
Notice: []
}
const {
Notice
} = db.data;
const data = ctx.request.body;
const id = data.id;
console.log("数据Id", id);
if (id && Notice) {
const currentDetail = Notice.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/Notice/delete', async (ctx) => {
ctx.type = 'application/json';
const db = await getDb();
// You can also use this syntax if you prefer
if (!db.data?.Notice)
db.data = {
Notice: []
}
let {
Notice
} = db.data;
const data = ctx.request.body;
const ids = data.ids;
if (ids?.length > 0 && Notice) {
let id = null;
for (let i = 0; i < ids.length; i++) {
id = ids[i];
const idx = Notice.findIndex(item => item.id == id);
if (idx != -1) {
Notice.splice(idx, 1);
}
}
// Write db.data content to db.json
await db.write()
}
ctx.body = {
code: 0,
};
});
}
export default getNotice;