blog.js
2.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import blogModel from "../../models/blog";
module.exports = {
async list(ctx, next) {
console.log(
"----------------获取博客列表 client_api/blog/list-----------------------"
);
let {
keyword = null,
isQuery = null,
pageindex = 1,
pagesize = 9,
sortBy = null,
} = ctx.request.query;
// 条件参数
let conditions = { isVisible: true };
if (keyword) {
let reg = new RegExp(keyword, "i");
// 区分搜索框、标签场景
let searchObj = isQuery
? [{ title: { $regex: reg } }, { desc: { $regex: reg } }]
: [{ type: { $regex: reg } }];
conditions["$or"] = [...searchObj];
}
// 排序参数
let sortParams = {};
if (sortBy) {
sortParams[sortBy] = -1;
sortParams["releaseTime"] = -1;
}
let options = {
limit: pagesize * 1,
skip: (pageindex - 1) * pagesize,
sort: sortParams,
};
try {
let data = await ctx.find(blogModel, conditions, null, options);
return ctx.send(data);
} catch (e) {
console.log(e);
return ctx.sendError(e);
}
},
async info(ctx, next) {
console.log(
"----------------获取博客信息 client_api/blog/info-----------------------"
);
let _id = ctx.request.query._id;
try {
let data = await ctx.findOne(blogModel, { _id });
return ctx.send(data);
} catch (e) {
return ctx.sendError(e);
}
},
async updateLikes(ctx, next) {
console.log(
"----------------点赞文章 client_api/blog/updateLikes------------------"
);
let paramsData = ctx.request.body;
let num = JSON.parse(paramsData.isLike) ? -1 : 1;
let options = { $inc: { likes: num } };
try {
let data = await ctx.update(blogModel, { _id: paramsData._id }, options);
ctx.send();
} catch (e) {
ctx.sendError(e);
}
},
async updatePV(ctx, next) {
console.log(
"----------------文章浏览量 client_api/blog/updatePV------------------"
);
let paramsData = ctx.request.body;
let options = { $inc: { pv: 1 } };
try {
let data = await ctx.update(blogModel, { _id: paramsData._id }, options);
ctx.send();
} catch (e) {
ctx.sendError(e);
}
},
};