feat: 优化模板代码,增加服务端模板
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user