-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
249 lines (230 loc) · 6.36 KB
/
service.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
const jwt = require('./auth.js')
const sendMailObj = require('./mail.js')
const dbOperations = require('./db.js')
// 获取用户信息
const getUserInfo = async (req, res) => {
const data = await dbOperations.findUserInfo(req._id)
const { token, loginTime, ...otherData } = data
const laterData = await dbOperations.findUserLater(req._id)
const todoData = await dbOperations.findUserTodoKeys(req._id)
let laterCount = 0
let todoCount = 0
if (laterData?.length) {
laterCount = laterData.filter(i => i.status === 0)?.length || 0
}
if (todoData?.length) {
todoCount = todoData.filter(i => i.status === 0)?.length || 0
}
return {
userinfo: otherData,
laterCount,
todoCount,
}
}
//获取验证码
const sendMailCode = async (req, res) => {
const captchaCode = await sendMailObj.sendEmail(req.query.email)
return {
verifyCode: captchaCode,
}
}
// 同步用户本地收藏、稍后再看、记事本
const updateLocal = async (req, userId) => {
const { laterData, todoData, collectUrls } = req.body
// 稍后再看
if (laterData?.length) {
const options = []
laterData.forEach(ele => {
const createObj = {
userId,
...ele,
}
options.push(createObj)
})
dbOperations.insertLaterMany(options)
}
if (collectUrls?.length) {
const userInfo = await dbOperations.findUserInfo(userId)
const combine = Array.from(new Set([...(userInfo?.collectUrls || []), ...collectUrls]))
dbOperations.updateUser(userId, { $set: { collectUrls: combine } })
}
if (todoData?.length) {
const options = []
todoData.forEach(ele => {
const createObj = {
userId,
...ele,
}
options.push(createObj)
})
dbOperations.insertTodoMany(options)
}
}
// 登录
const createUser = async (req, res) => {
const { mail, code, laterData } = req.body
let userId = ''
const userInfo = {
mail: mail,
loginTime: Date.now(),
token: '',
}
let token = jwt.sign(req.body) // 生成token
const result = await dbOperations.findUserByMail(mail)
if (result?._id) {
userId = result._id
updateLocal(req, userId)
await dbOperations.updateUser(userId, { $set: { token } })
} else {
const verifyRes = await sendMailObj.verifyCode(mail, code)
if (verifyRes.error === 1) {
throw new Error(verifyRes.msg)
}
userInfo.token = token
const result = await dbOperations.insertUser(userInfo)
userId = result.insertedId.toHexString()
updateLocal(req, result.insertedId)
}
return {
token,
userId,
}
}
// 创建标签组
const createUrlTag = async (req, res) => {
const { _id, body } = req
let doc = {
userId: _id,
createTime: Date.now(),
...body,
}
const groups = await dbOperations.findUserTags(_id)
const hasAleady = groups.find(i => i.name === body.name)
if (hasAleady) {
throw new Error('标签名重复')
} else {
await dbOperations.inserUserTag(doc)
// mongodb.insertOne('url_tag', doc, () => {
const result = [...groups, doc].map(i => {
const { userId, _id, ...params } = i
return { ...params }
})
return result
}
}
// 查询用户标签
const getUrlTag = async (req, res) => {
const groups = await dbOperations.findUserTags(req._id)
const result = groups.map(i => {
const { userId, ...params } = i
return { ...params }
})
return result
}
// 处理收藏网址功能
const addFavorUrl = async (req, res) => {
const { _id } = req
const { url } = req.query
const result = await dbOperations.findUserInfo(_id)
const { collectUrls = [] } = result
const index = collectUrls.findIndex(i => i.url === url)
const hasfavor = index > -1
if (hasfavor) {
collectUrls.splice(index, 1)
} else {
collectUrls.push(req.query)
}
await dbOperations.updateUser(_id, { $set: { collectUrls } })
return {
msg: hasfavor ? '取消收藏' : '收藏成功',
data: collectUrls,
}
}
// 稍后再看
const setLater = async (req, res) => {
const { _id, body } = req
const laterResults = (await dbOperations.findUserLater(_id)) || []
const createObj = {
userId: _id,
createTime: Date.now(),
status: 0,
...body,
}
await dbOperations.insertLater(createObj)
return [...laterResults, createObj]
}
const getLater = async (req, res) => {
const laterResults = (await dbOperations.findUserLater(req._id)) || []
return laterResults
}
// 更新稍后再看
const updateLater = async (req, res) => {
const laterResults = (await dbOperations.findUserLater(req._id)) || []
const { _id } = req.body
laterResults.forEach(async i => {
if (i._id.toString() === _id) {
const status = Number(!i?.status)
await dbOperations.updateLater(i._id, { $set: { status } })
const updateData = await dbOperations.findUserLater(req._id)
return updateData
}
})
}
const deleteLater = async (req, res) => {
const { _id } = req.query
await dbOperations.deleteLater(_id)
const updateData = await dbOperations.findUserLater(req._id)
return updateData
}
// 关键词记事本
const setTodoKeys = async (req, res) => {
const { _id, body } = req
const todoKeysResults = (await dbOperations.findUserTodoKeys(_id)) || []
const createObj = {
userId: _id,
createTime: Date.now(),
status: 0,
...body,
}
console.error('createobj', createObj)
await dbOperations.insertTodoKeys(createObj)
return [...todoKeysResults, createObj]
}
const getTodoKeys = async (req, res) => {
const todoKeysResults = (await dbOperations.findUserTodoKeys(req._id)) || []
return todoKeysResults
}
const deleteTodoKeys = async (req, res) => {
const { _id } = req.query
await dbOperations.deleteTodoKeys(_id)
const updateData = await dbOperations.findUserTodoKeys(req._id)
return updateData
}
const updateTodoKeys = async (req, res) => {
const todoKeysResults = (await dbOperations.findUserTodoKeys(req._id)) || []
const { _id } = req.body
todoKeysResults.forEach(async i => {
if (i._id.toString() === _id) {
const status = Number(!i?.status)
await dbOperations.updateTodoKeys(i._id, { $set: { status } })
const updateData = await dbOperations.findUserTodoKeys(req._id)
return updateData
}
})
}
module.exports = {
getUserInfo,
sendMailCode,
getUrlTag,
createUrlTag,
createUser,
setLater,
getLater,
addFavorUrl,
updateLater,
deleteLater,
setTodoKeys,
getTodoKeys,
deleteTodoKeys,
updateTodoKeys,
}