feat: first commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
static
|
||||||
|
config.yml
|
||||||
|
node_modules
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
用于搭建服务端的模板
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/test
|
||||||
|
```
|
||||||
|
可检测接口服务是否正常
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/Employee/login
|
||||||
|
```
|
||||||
|
登录接口
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/[name]/list
|
||||||
|
```
|
||||||
|
数据列表接口
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/[name]/add
|
||||||
|
```
|
||||||
|
添加数据接口
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/[name]/upd
|
||||||
|
```
|
||||||
|
修改数据接口
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/[name]/delete
|
||||||
|
```
|
||||||
|
删除数据接口
|
||||||
|
|
||||||
|
```
|
||||||
|
/api/[name]?id=[id]
|
||||||
|
```
|
||||||
|
数据详情接口
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"backupHistory": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"test": [
|
||||||
|
{
|
||||||
|
"message": "服务正常"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"dataLog": [
|
||||||
|
{
|
||||||
|
"dataId": "cm6pwsc3v0000g0rbdezlccd2",
|
||||||
|
"type": "add",
|
||||||
|
"tableName": "test",
|
||||||
|
"data": {
|
||||||
|
"id": "cm6pwsc3v0000g0rbdezlccd2",
|
||||||
|
"message": "xxx",
|
||||||
|
"sort": 1,
|
||||||
|
"userId": "cl3fqx6zb000034ny90rd333s",
|
||||||
|
"createTime": "2025-02-04T03:17:08.299Z"
|
||||||
|
},
|
||||||
|
"status": 1,
|
||||||
|
"createTime": "2025-02-04T03:17:08.300Z",
|
||||||
|
"id": "cm6pwsc3w0001g0rb3p03bro0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"dataId": "cm6pwst9z0000hsrbd1bx3b84",
|
||||||
|
"type": "add",
|
||||||
|
"tableName": "test",
|
||||||
|
"data": {
|
||||||
|
"id": "cm6pwst9z0000hsrbd1bx3b84",
|
||||||
|
"message": "xxx",
|
||||||
|
"sort": 1,
|
||||||
|
"userId": "cl3fqx6zb000034ny90rd333s",
|
||||||
|
"createTime": "2025-02-04T03:17:30.552Z"
|
||||||
|
},
|
||||||
|
"status": 1,
|
||||||
|
"createTime": "2025-02-04T03:17:30.553Z",
|
||||||
|
"id": "cm6pwsta10001hsrb8st86tid"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"img": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import { CreateServer } from "funjia-server";
|
||||||
|
|
||||||
|
CreateServer(4874);
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "funjia-server-template",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"@koa/multer": "^3.0.0",
|
||||||
|
"@types/yaml": "^1.9.7",
|
||||||
|
"cuid": "^2.1.8",
|
||||||
|
"dayjs": "^1.11.3",
|
||||||
|
"koa": "^2.13.4",
|
||||||
|
"koa-body": "^4.2.0",
|
||||||
|
"koa-mount": "^4.0.0",
|
||||||
|
"koa-router": "^10.1.1",
|
||||||
|
"koa-static": "^5.0.0",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"lowdb": "^3.0.0",
|
||||||
|
"multer": "^1.4.5-lts.1",
|
||||||
|
"node-cron": "^3.0.1",
|
||||||
|
"uuid": "^8.3.2",
|
||||||
|
"yaml": "^2.1.1",
|
||||||
|
"funjia-server":"git+http://git.funjia.top/funjia/funjia-server.git#v1.0.2"
|
||||||
|
},
|
||||||
|
"description": ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import _ from "lodash";
|
||||||
|
// import RouterFactor from "../RouterFactory.mjs";
|
||||||
|
// import DBHelper from "../DBHelper.mjs";
|
||||||
|
import {RouterFactor,DBHelper} from "funjia-server/lib/funjia-server.mjs";
|
||||||
|
|
||||||
|
const getBackupHistory = (router) => {
|
||||||
|
const rF = new RouterFactor({
|
||||||
|
router,
|
||||||
|
db: DBHelper.backupDB,
|
||||||
|
name: "backupHistory"
|
||||||
|
});
|
||||||
|
|
||||||
|
rF.createGetList();
|
||||||
|
|
||||||
|
rF.createGetDetail();
|
||||||
|
|
||||||
|
rF.createAdd();
|
||||||
|
|
||||||
|
rF.createUpdate();
|
||||||
|
|
||||||
|
rF.createDelete();
|
||||||
|
|
||||||
|
// router.post('/api/backupHistory/add', async (ctx) => {
|
||||||
|
// ctx.type = 'application/json';
|
||||||
|
// const db = await getBackupDb();
|
||||||
|
|
||||||
|
// // You can also use this syntax if you prefer
|
||||||
|
// if (!db.data.backupHistory)
|
||||||
|
// db.data.backupHistory = [];
|
||||||
|
// const {
|
||||||
|
// backupHistory
|
||||||
|
// } = db.data;
|
||||||
|
// const data = ctx.request.body;
|
||||||
|
// console.log("add:", data);
|
||||||
|
// data.id = cuid();
|
||||||
|
// data.createTime = new Date();
|
||||||
|
// backupHistory.push(data)
|
||||||
|
// // Write db.data content to db.json
|
||||||
|
// await db.write()
|
||||||
|
|
||||||
|
// ctx.body = {
|
||||||
|
// code: 0,
|
||||||
|
// };
|
||||||
|
// });
|
||||||
|
|
||||||
|
// router.post('/api/backupHistory/upd', async (ctx) => {
|
||||||
|
// ctx.type = 'application/json';
|
||||||
|
// const db = await getBackupDb();
|
||||||
|
|
||||||
|
// // You can also use this syntax if you prefer
|
||||||
|
// if (!db.data.backupHistory)
|
||||||
|
// db.data = {
|
||||||
|
// backupHistory: []
|
||||||
|
// }
|
||||||
|
// const {
|
||||||
|
// backupHistory
|
||||||
|
// } = db.data;
|
||||||
|
// const data = ctx.request.body;
|
||||||
|
// const id = data.id;
|
||||||
|
// console.log("数据Id:", id);
|
||||||
|
// if (id && backupHistory) {
|
||||||
|
// const currentDetail = backupHistory.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/backupHistory/delete', async (ctx) => {
|
||||||
|
// ctx.type = 'application/json';
|
||||||
|
// const db = await getBackupDb();
|
||||||
|
|
||||||
|
// // You can also use this syntax if you prefer
|
||||||
|
// if (!db.data.backupHistory)
|
||||||
|
// db.data = {
|
||||||
|
// backupHistory: []
|
||||||
|
// }
|
||||||
|
// let {
|
||||||
|
// backupHistory
|
||||||
|
// } = db.data;
|
||||||
|
// const data = ctx.request.body;
|
||||||
|
// const ids = data.ids;
|
||||||
|
// if (ids.length > 0 && backupHistory) {
|
||||||
|
// let id = null;
|
||||||
|
// for (let i = 0; i < ids.length; i++) {
|
||||||
|
// id = ids[i];
|
||||||
|
// const idx = backupHistory.findIndex(item => item.id == id);
|
||||||
|
// if (idx != -1) {
|
||||||
|
// backupHistory.splice(idx, 1);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// // Write db.data content to db.json
|
||||||
|
// await db.write()
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ctx.body = {
|
||||||
|
// code: 0,
|
||||||
|
// };
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getBackupHistory;
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
import cuid from "cuid";
|
||||||
|
// import getDb from "../dbServer.mjs";
|
||||||
|
import {getDb} from "funjia-server/lib/funjia-server.mjs";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
const getDynamic = (router) => {
|
||||||
|
|
||||||
|
router.post('/api/dynamic/list/:tableName', async (ctx) => {
|
||||||
|
// 设置头类型, 如果不设置,会直接下载该页面
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const db = await getDb();
|
||||||
|
const body = ctx.request.body;
|
||||||
|
const data = db.data[ctx.params.tableName]?.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/dynamic/:tableName', async (ctx) => {
|
||||||
|
// 设置头类型, 如果不设置,会直接下载该页面
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const db = await getDb();
|
||||||
|
const data = db.data[ctx.params.tableName];
|
||||||
|
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/dynamic/add/:tableName', async (ctx) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const tableName=ctx.params.tableName;
|
||||||
|
|
||||||
|
const db = await getDb();
|
||||||
|
|
||||||
|
// You can also use this syntax if you prefer
|
||||||
|
if (!db.data[tableName])
|
||||||
|
db.data[tableName] = [];
|
||||||
|
|
||||||
|
const dynamic = db.data[tableName];
|
||||||
|
const data = ctx.request.body;
|
||||||
|
console.log("add:", data);
|
||||||
|
data.id = cuid();
|
||||||
|
data.createTime = new Date();
|
||||||
|
dynamic.push(data)
|
||||||
|
// Write db.data content to db.json
|
||||||
|
await db.write()
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
code: 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/api/dynamic/upd/:tableName', async (ctx) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const tableName=ctx.params.tableName;
|
||||||
|
|
||||||
|
const db = await getDb();
|
||||||
|
|
||||||
|
// You can also use this syntax if you prefer
|
||||||
|
if (!db.data[tableName])
|
||||||
|
db.data = {
|
||||||
|
dynamic: []
|
||||||
|
}
|
||||||
|
|
||||||
|
const dynamic = db.data[tableName];
|
||||||
|
|
||||||
|
const data = ctx.request.body;
|
||||||
|
const id = data.id;
|
||||||
|
console.log("数据Id:", id);
|
||||||
|
if (id && dynamic) {
|
||||||
|
const currentDetail = dynamic.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/dynamic/delete/:tableName', async (ctx) => {
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const tableName=ctx.params.tableName;
|
||||||
|
|
||||||
|
const db = await getDb();
|
||||||
|
|
||||||
|
// You can also use this syntax if you prefer
|
||||||
|
if (!db.data[tableName])
|
||||||
|
db.data = {
|
||||||
|
[tableName]: []
|
||||||
|
}
|
||||||
|
|
||||||
|
const dynamic = db.data[tableName];
|
||||||
|
|
||||||
|
const data = ctx.request.body;
|
||||||
|
const ids = data.ids;
|
||||||
|
if (ids?.length > 0 && dynamic) {
|
||||||
|
let id = null;
|
||||||
|
for (let i = 0; i < ids.length; i++) {
|
||||||
|
id = ids[i];
|
||||||
|
const idx = dynamic.findIndex(item => item.id == id);
|
||||||
|
if (idx != -1) {
|
||||||
|
dynamic.splice(idx, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Write db.data content to db.json
|
||||||
|
await db.write()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
code: 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getDynamic;
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
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;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import cuid from "cuid";
|
||||||
|
// import getDb from "../dbServer.mjs";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
import { getDb } from "funjia-server/lib/funjia-server.mjs";
|
||||||
|
const getRouterSet= (router) => {
|
||||||
|
router.get('/api/test', async (ctx) => {
|
||||||
|
// 设置头类型, 如果不设置,会直接下载该页面
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const db = await getDb();
|
||||||
|
const data = db.data.test;
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
code:0,
|
||||||
|
data
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getRouterSet;
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
import cuid from "cuid";
|
||||||
|
// import getDb, {
|
||||||
|
// getResourceDb
|
||||||
|
// } from "../dbServer.mjs";
|
||||||
|
import {getDb,getResourceDb} from "funjia-server/lib/funjia-server.mjs";
|
||||||
|
import _ from "lodash";
|
||||||
|
import multer from '@koa/multer';
|
||||||
|
|
||||||
|
//配置
|
||||||
|
var storage = multer.diskStorage({
|
||||||
|
//文件保存路径
|
||||||
|
destination: function (req, file, cb) {
|
||||||
|
cb(null, 'static') //注意路径必须存最后面在不用加斜杠
|
||||||
|
},
|
||||||
|
//修改文件名称
|
||||||
|
filename: function (req, file, cb) {
|
||||||
|
var fileFormat = (file.originalname).split(".");
|
||||||
|
const filename = cuid() + "." + fileFormat[fileFormat.length - 1];
|
||||||
|
req.filename = filename;
|
||||||
|
cb(null, filename);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
//加载配置
|
||||||
|
var upload = multer({
|
||||||
|
storage: storage
|
||||||
|
})
|
||||||
|
|
||||||
|
// const upload = multer({ dest: 'static/' })
|
||||||
|
|
||||||
|
const getUpload = (router) => {
|
||||||
|
router.post('/api/leaseRecords/list', async (ctx) => {
|
||||||
|
// 设置头类型, 如果不设置,会直接下载该页面
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const db = await getDb();
|
||||||
|
const body = ctx.request.body;
|
||||||
|
const data = db.data.leaseRecords.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/img', async (ctx) => {
|
||||||
|
// 设置头类型, 如果不设置,会直接下载该页面
|
||||||
|
ctx.type = 'application/json';
|
||||||
|
const db = await getDb();
|
||||||
|
const data = db.data.leaseRecords;
|
||||||
|
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 || {}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加图片记录
|
||||||
|
* @param {*} path
|
||||||
|
*/
|
||||||
|
const addImg = async (filename, path) => {
|
||||||
|
const data = {};
|
||||||
|
try {
|
||||||
|
const db = await getResourceDb();
|
||||||
|
|
||||||
|
// You can also use this syntax if you prefer
|
||||||
|
if (!db.data.img)
|
||||||
|
db.data.img = [];
|
||||||
|
|
||||||
|
const {
|
||||||
|
img
|
||||||
|
} = db.data;
|
||||||
|
|
||||||
|
data.id = cuid();
|
||||||
|
data.filename = filename;
|
||||||
|
data.path = path;
|
||||||
|
data.createTime = new Date();
|
||||||
|
img.push(data)
|
||||||
|
// Write db.data content to db.json
|
||||||
|
await db.write()
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return data.id;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加单个文件上传的路由
|
||||||
|
router.post(
|
||||||
|
'/api/upload/img',
|
||||||
|
upload.single('file'),
|
||||||
|
async (ctx) => {
|
||||||
|
const {
|
||||||
|
filename,
|
||||||
|
path
|
||||||
|
} = ctx.file;
|
||||||
|
const res = await addImg(filename, path);
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
code: res ? 0 : 1,
|
||||||
|
data: {
|
||||||
|
id: res,
|
||||||
|
url: path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加单个文件上传的路由
|
||||||
|
router.post(
|
||||||
|
'/api/upload/file',
|
||||||
|
upload.single('file'),
|
||||||
|
async (ctx) => {
|
||||||
|
const {
|
||||||
|
filename,
|
||||||
|
path
|
||||||
|
} = ctx.file;
|
||||||
|
// const res = await addImg(filename, path);
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
code: 0,
|
||||||
|
data: {
|
||||||
|
url: "/resource/static/"+filename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getUpload;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
fetch("http://localhost:4874/api/test/add", {
|
||||||
|
"headers": {
|
||||||
|
"accept": "application/json, text/plain, */*",
|
||||||
|
"accept-language": "en,zh-CN;q=0.9,zh;q=0.8",
|
||||||
|
"authorization": "",
|
||||||
|
"cache-control": "no-cache",
|
||||||
|
"content-type": "application/json",
|
||||||
|
"sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"",
|
||||||
|
"sec-ch-ua-mobile": "?0",
|
||||||
|
"sec-ch-ua-platform": "\"Windows\"",
|
||||||
|
"sec-fetch-dest": "empty",
|
||||||
|
"sec-fetch-mode": "cors",
|
||||||
|
"sec-fetch-site": "same-origin",
|
||||||
|
"cookie": "UserInfo={%22id%22:%22cl3fqx6zb000034ny90rd333s%22%2C%22useName%22:%22xxk%22%2C%22pwd%22:%22123456%22%2C%22phone%22:%221342919898%22%2C%22vehiclePlateNumber%22:%22sfsfsdf%22%2C%22userInfoPic%22:%22/%E5%BE%90_1.png%22%2C%22createTime%22:%222022-05-21T10:46:44.377Z%22%2C%22name%22:%22%E5%BE%90%E5%90%91%E5%A5%8E%22%2C%22userName%22:%22xxk%22%2C%22source_type%22:%22IPC%22%2C%22forever%22:true%2C%22noticeTotal%22:%2235%22}",
|
||||||
|
"Referer": "http://localhost:4874/",
|
||||||
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
||||||
|
},
|
||||||
|
"body": "{\"id\":0,\"message\":\"xxx\",\"sort\":1,\"userId\":\"cl3fqx6zb000034ny90rd333s\"}",
|
||||||
|
"method": "POST"
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user