label.js
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import labelModel from "../../models/label";
module.exports = {
async list(ctx, next) {
console.log(
"----------------获取标签列表 label/list-----------------------"
);
let { keyword, pageindex = 1, pagesize = 50 } = ctx.request.query;
try {
let reg = new RegExp(keyword, "i");
let data = await ctx.findPage(
labelModel,
{
$or: [{ label: { $regex: reg } }, { bgColor: { $regex: reg } }],
},
null,
{ limit: pagesize * 1, skip: (pageindex - 1) * pagesize }
);
ctx.send(data);
} catch (e) {
console.log(e);
ctx.sendError(e);
}
},
async add(ctx, next) {
console.log("----------------添加标签 label/add-----------------------");
let paramsData = ctx.request.body;
try {
let data = await ctx.findOne(labelModel, { label: paramsData.label });
if (data) {
ctx.sendError("数据已经存在, 请重新添加!");
} else {
let result = await ctx.add(labelModel, paramsData);
ctx.send(result);
}
} catch (e) {
ctx.sendError(e);
}
},
async update(ctx, next) {
console.log("----------------更新标签 label/update-----------------------");
let paramsData = ctx.request.body;
try {
let data = await ctx.update(
labelModel,
{ _id: paramsData._id },
paramsData
);
ctx.send(data);
} catch (e) {
if (e === "暂无数据") {
ctx.sendError(e);
}
}
},
async del(ctx, next) {
console.log("----------------删除标签 label/del-----------------------");
let id = ctx.request.query.id;
try {
let data = await ctx.remove(labelModel, { _id: id });
ctx.send(data);
} catch (e) {
ctx.sendError(e);
}
},
};