forked from wangtunan/vue-mooc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnotice.js
250 lines (244 loc) · 5.56 KB
/
notice.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import Router from 'koa-router'
import Notice from '../models/notice.js'
import UserNotice from '../models/userNotice.js'
import checkUser from '../middleware/auth.js'
import { SIZE, ERR_OK } from '../config.js'
import { getGuid } from '../../src/utils/utils.js'
const router = new Router({
prefix: '/notice'
})
// 消息分页列表接口
router.get('/list', checkUser, async (ctx) => {
const { page = 1, code } = ctx.query
const userid = ctx.session.user_id
let where = {}
if (code) {
where.code = code
}
try {
const query = Notice.find(where)
const total = await Notice.find(where).countDocuments()
const result = await query.skip( (page - 1) * SIZE ).limit(SIZE).lean()
const userNoticeResult = await UserNotice.find({
isDelete: false,
userid: userid
})
// 判断主表有没有数据
if (total === 0) {
ctx.body = {
code: 0,
msg: '查询消息列表数据失败',
data: {
list: [],
total: 0
}
}
return false
}
result.forEach(notice => {
notice['isRead'] = userNoticeResult.filter(item => {
return item.messageid === notice.id && item.userid === userid && item.isRead
}).length > 0
})
ctx.body = {
code: ERR_OK,
msg: '查询消息列表数据成功',
data: {
list: result,
total: total
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '查询消息列表数据失败',
data: {
list: [],
total: 0
}
}
}
})
// 单个消息已读
router.post('/read', checkUser, async (ctx) => {
const { id } = ctx.request.body
const userid = ctx.session.user_id
// 判断是否传入id
if (!id) {
ctx.body = {
code: -1,
msg: "请传入消息主键ID"
}
return false
}
// 先判断是否已经在用户消息表中,存在update,不存在create
try {
const checkResult = await UserNotice.findOne({
userid: userid,
messageid: id
})
if (!checkResult) {
const createResult = await UserNotice.create({
id: getGuid(),
messageid: id,
userid: userid,
isRead: true,
isDelete: false
})
if (createResult) {
ctx.body = {
code: ERR_OK,
msg: "消息已读成功"
}
} else {
ctx.body = {
code: -1,
msg: "消息已读失败"
}
}
} else {
const updateResult = await UserNotice.where({
messageid: id,
userid: userid,
}).updateOne({
isRead: true
})
if (updateResult) {
ctx.body = {
code: ERR_OK,
msg: "消息已读成功"
}
} else {
ctx.body = {
code: -1,
msg: "消息已读失败"
}
}
}
} catch(e) {
ctx.body = {
code: -1,
msg: e.message || "消息删除失败",
userid: userid
}
}
})
// 消息全部已读
router.post('/read/all', checkUser, async (ctx) => {
const { ids } = ctx.request.body
const userid = ctx.session.user_id
try {
const insertArr = []
ids.forEach(id => {
insertArr.push({
id: getGuid(),
messageid: id,
userid: userid,
isRead: true,
isDelete: false
})
})
const insertResult = await UserNotice.insertMany(insertArr)
if (insertResult) {
ctx.body = {
code: ERR_OK,
msg: '全部消息已读成功'
}
} else {
ctx.body = {
code: -1,
msg: '全部消息已读失败'
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '全部消息已读失败'
}
}
})
// 单个删除消息
router.post('/delete', checkUser, async (ctx) => {
const { id } = ctx.request.body
const userid = ctx.session.user_id
if (!id) {
ctx.body = {
code: -1,
msg: "请传入消息主键ID"
}
return false
}
// 先判断是否已经在用户消息表中,存在update,不存在create
try {
const checkResult = await UserNotice.findOne({
userid: userid,
messageid: id
})
if (!checkResult) {
const createResult = await UserNotice.create({
id: getGuid(),
messageid: id,
userid: userid,
isRead: false,
isDelete: true
})
if (createResult) {
ctx.body = {
code: ERR_OK,
msg: "消息删除成功"
}
} else {
ctx.body = {
code: -1,
msg: "消息删除失败"
}
}
} else {
const updateResult = await UserNotice.where({
messageid: id,
userid: userid,
}).updateOne({
isDelete: true
})
if (updateResult) {
ctx.body = {
code: ERR_OK,
msg: "消息删除成功"
}
} else {
ctx.body = {
code: -1,
msg: "消息删除失败"
}
}
}
} catch(e) {
ctx.body = {
code: -1,
msg: e.message || "消息删除失败"
}
}
})
// 是否存在未读消息
router.get('/read/not', checkUser, async (ctx) => {
try {
const noticeCount = await Notice.find().countDocuments()
const userCount = await UserNotice.find({
isDelete: false,
userid: ctx.session.user_id
}).countDocuments()
ctx.body = {
code: ERR_OK,
msg: '获取成功',
notice: noticeCount,
user: userCount,
data: noticeCount > userCount
}
} catch(e) {
ctx.body = {
code: -1,
msg: e.message || '获取数据失败'
}
}
})
export default router