-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
349 lines (328 loc) · 10.9 KB
/
router.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const express = require('express')
// 调用express.router方法
const router = express.Router()
// 引入数据库model
const { users, navs } = require('./models/users')
const { programs } = require('./models/program')
// const history = require('./models/history')
// 引入文件核心模块
const fs = require('fs')
const path = require('path')
const multer = require('multer')
const upload = multer({ dest: './public/image/avatar' })
// 引入md5加密包
const md5 = require('js-md5')
const Jwt = require('jsonwebtoken');//模块引入
const secret = 'manager'; //定义密钥
// 保存邮箱验证码
let mailcode = {}
// 引入发送邮箱验证码的方法
const { sendCode } = require('./util')
// 删除文件的方法
function deleteFile(delPath, direct) {
delPath = direct ? delPath : path.join(__dirname, delPath)
try {
/**
* @des 判断文件或文件夹是否存在
*/
if (fs.existsSync(delPath)) {
fs.unlinkSync(delPath);
} else {
console.log('inexistence path:', delPath);
}
} catch (error) {
console.log('del error', error);
}
}
// deleteFile("/public/image/program/e82ba64544a1aec3480de4dd849f803905b7dbaa1eafa1e33ab485e8a0d7aece.jpg")
// users.updateOne({'username':'Asunat'},{'avatar':'/public/image/avatar/avatar.jpg'},function(err,doc){
// if(err){
// console.log(err)
// }
// return console.log(doc)
// })
// 处理home界面导航数据请求
router.get('/home/navs', async (req, res) => {
try {
const data = await navs.find()
return res.status(200).json({
code: 1,
user: req.session.user,
data: data
})
} catch (e) {
return res.status(500).json({
code: 500,
})
}
})
// 处理登录请求
router.post('/login', async (req, res) => {
// 接受客户端发送发送过来的post数据
try {
const body = req.body
console.log(body)
// 邮箱登录验证
if (body.email) {
if (!body.code) return res.status(200).json({
error_code: -1,
msg: '验证码不能为空!',
})
if (parseInt(body.code) !== mailcode.mcode) return res.status(200).json({
error_code: -1,
msg: '验证码错误!',
})
// 将查询到的数据保存到session中
const data = await users.findOne({ "email": body.email })
// 设置验证token
const token = Jwt.sign(Object.assign({}, data.username), secret, {
expiresIn: 60 * 60 * 2 // 过期时间
})
// console.log(req.session.user)
return res.status(200).json({
error_code: 1,
msg: '登录成功',
token
})
}
body.password = md5(md5(body.password))
// console.log(body.password)
// 从数据库中查询数据
if (!(await users.findOne({
"$or": [{ "username": body.username },
{ "email": body.username }]
}))) {
return res.status(200).json({
error_code: -1,
msg: '用户名/邮箱错误或未注册'
})
}
const result = await users.findOne({
"password": body.password, "$or": [{ "username": body.username },
{ "email": body.username }]
})
if (JSON.stringify(result) === '{}' || !result) {
return res.status(200).json({
error_code: 0,
msg: '密码错误'
})
}
// 将查询到的数据保存到session中
const data = await users.findOne({
"$or": [{ "username": body.username },
{ "email": body.username }]
})
req.session.islogin = true
req.session.user = data
// 设置验证token
const token = Jwt.sign(Object.assign({}, data.username), secret, {
expiresIn: 60 * 60 * 2 // 过期时间
})
// console.log(req.session.user)
return res.status(200).json({
error_code: 1,
msg: '登录成功',
token
})
} catch (e) {
console.log(e)
}
})
// 处理登录邮箱验证码请求
router.post('/login/mail', async (req, res) => {
const email = req.body.email
console.log(email)
if (email.length === 0) return res.status(200).json({
code: -1,
msg: '邮箱不能为空!'
})
const result = await users.findOne({ "email": email })
if (!result) {
return res.status(200).json({
code: -1,
msg: "该邮箱未注册!",
})
}
const code = parseInt(Math.random() * 1000000)
const data = await sendCode(code, email)
console.log(data)
// 判断邮箱验证码,是否发送成功
if (data.code !== 200) return res.status(200).json({
code: 500,
msg: data.msg,
})
mailcode.mcode = code
return res.status(200).json({
code: 1,
msg: data.msg,
data: mailcode
})
})
// 发送邮箱验证码
router.post('/sendmCode', async (req, res) => {
const mail = req.body.email
try {
if (!mail) return res.status(200).json({
code: -1,
msg: "邮箱不能为空!",
})
const result = await users.findOne({ "email": mail })
if (result) {
return res.status(200).json({
code: -1,
msg: "该邮箱已注册!",
})
}
const code = parseInt(Math.random() * 1000000)
const data = await sendCode(code, mail)
// 判断邮箱验证码,是否发送成功
if (data.code !== 200) return res.status(200).json({
code: 500,
msg: data.msg,
})
mailcode.mcode = code
return res.status(200).json({
code: 1,
msg: data.msg,
data: mailcode
})
} catch (e) {
console.log(e)
}
})
// 处理注册请求
router.post('/registered', (req, res) => {
const body = req.body
// 使用$or查询用户名或者邮箱
users.findOne({
"$or": [{ "username": body.username },
{ "email": body.email }]
})
.then(result => {
// console.log(result)
// 如果可以查询到结果,则证明用户存在
if (result) {
return res.status(200).json({
error_code: 0,
msg: '用户名或者邮箱已存在'
})
} else {
if (!body.mailcode) {
return res.status(200).json({
error_code: 5,
msg: '验证码不能为空!'
})
} else if (parseInt(body.mailcode) !== mailcode.mcode) {
return res.status(200).json({
error_code: 500,
msg: '验证码错误!'
})
}
// 否则证明用户不存在,就往数据库里添加这条数据
body.password = md5(md5(body.password))
delete body.mailcode
new users(body).save((err, data) => {
if (err) {
return res.status(500).json({
error_code: 500,
msg: '服务器繁忙,请稍后再试'
})
} else if (data) {
// 数据存入成功
return res.status(200).json({
error_code: 1,
msg: '用户注册成功'
})
}
})
}
})
.catch(err => {
// 返回错误
return res.status(500).json({
error_code: 500,
msg: '服务器繁忙。请稍后再试'
})
})
})
// 记录搜索历史记录的请求
router.put('/home/shistory', async (req, res) => {
const body = req.body
const value = req.body.value
const username = req.body.username
if (!body.value) return res.status(200).json({
code: 0,
msg: 'value为空!'
})
const val = await users.updateOne({
'username': username,
"history.value": { $ne: value }
}, {
$push: {
'history': { 'value': value }
}
})
if (!val.nModified) return res.status(200).json({
code: 5,
msg: '已存在'
})
return res.status(200).json({
code: 1,
msg: '添加记录成功!'
})
})
// 处理删除用户搜索记录的请求
router.delete('/home/shistory', async (req, res) => {
const id = req.query.id
const username = req.query.username
try {
const data = await users.updateOne({ 'username': username }, { "$pull": { "history": { "_id": id } } })
// console.log(data)
if (data.nModified) {
return res.status(200).json({
code: 1,
data: data,
msg: '删除成功',
})
}
return res.status(200).json({
code: 0,
msg: '删除失败!'
})
} catch (e) {
console.log(e)
}
})
// 上传用户头像
router.post('/home/upload/avatar', upload.single('avatar'), async (req, res) => {
let oldfilename = req.file.destination + "/" + req.file.filename
let newfilename = req.file.destination + '/' + req.file.filename + req.file.originalname
try {
let username = req.body.username
const img = await users.findOne({ 'username': username })
// 更新前删除对应图片
if (img.avatar !== '/public/image/avatar-default.png') {
deleteFile(img.avatar)
}
// 更改头像
fs.rename(oldfilename, newfilename, async (err) => {
if (err) return res.status(200).json({
code: 500,
msg: '服务器出错!!!'
})
let curavatar = newfilename.slice(1)
const data = await users.updateOne({ 'username': username }, { 'avatar': curavatar })
if (!data.nModified) return res.status(200).json({
code: 0,
msg: '更新失败',
})
return res.status(200).json({
code: 1,
msg: '更新头像成功'
})
})
} catch (e) {
console.log(e)
}
})
module.exports = router