message.js
2.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
import messageModel from '../../models/message';
module.exports = {
async add(ctx, next) {
console.log('----------------添加留言 message/add-----------------------');
let paramsData = ctx.request.body;
if (!paramsData.nickname) {
paramsData.nickname = '匿名网友';
}
try {
await ctx.add(messageModel, paramsData);
ctx.send(paramsData);
} catch (e) {
ctx.sendError(e);
}
},
async list(ctx, next) {
console.log(
'----------------获取评论列表 client_api/message/list-----------------------'
);
let { pageindex = 1, pagesize = 10 } = ctx.request.query;
// 排序参数
let sortParams = {
createTime: -1,
};
let options = {
limit: pagesize * 1,
skip: (pageindex - 1) * pagesize,
sort: sortParams,
};
try {
let data = await ctx.find(messageModel, null, null, options);
return ctx.send(data);
} catch (e) {
console.log(e);
return ctx.sendError(e);
}
},
async replyCount(ctx, next) {
console.log(
'----------------获取回复列表 client_api/message/replyCount-----------------------'
);
let conditions = [
{ $project: { replyCount: { $size: '$replyList' } } },
{ $group: { _id: null, replyCount: { $sum: '$replyCount' } } },
];
try {
let data = await ctx.aggregate(messageModel, conditions);
return ctx.send(data);
} catch (e) {
console.log(e);
return ctx.sendError(e);
}
},
async updateLikes(ctx, next) {
console.log(
'----------------点赞留言 client_api/message/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(
messageModel,
{ _id: paramsData._id },
options
);
ctx.send();
} catch (e) {
ctx.sendError(e);
}
},
async updateReplys(ctx, next) {
console.log(
'----------------回复留言 client_api/message/updateReplys------------------'
);
let paramsData = ctx.request.body;
if (!paramsData.replyUser) {
paramsData.replyUser = '匿名网友';
}
let options = {
$push: { replyList: { $each: [paramsData], $position: 0 } },
};
try {
let data = await ctx.update(
messageModel,
{ _id: paramsData._id },
options
);
ctx.send();
} catch (e) {
ctx.sendError(e);
}
},
};