-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommon.js
113 lines (82 loc) · 3.39 KB
/
common.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
/**
* @Author:acexy@thankjava.com
* 2018/6/13
* @Description:登录相关处理
*/
const Router = require('koa-router');
const cookiesName = require('../const/cookiesName');
const baseController = require('./baseController');
const util = require('../lib/util');
const asyncRedisClient = require('../lib/asyncRedis').client;
const redisKey = require('../const/redisKey');
const userModel = require('../models/user');
const baseConfig = require('../config/basic');
module.exports = new Router(
).post('login', async (ctx) => {
// 登录
let params = ctx.request.body;
if (!params || Object.keys(params).length == 0) {
return baseController.response400(ctx);
}
let username = params.username;
let password = params.password;
if (!username || !password) {
return baseController.response400(ctx, '帐号或密码为空');
}
let user = await userModel.selectByUsername(username);
if (!user) {
return baseController.responseWithCode(ctx, baseController.CODE.INVALID_ACCOUNT, '账号不存在');
}
if (util.md5(user.username + password) != user.password) {
return baseController.responseWithCode(ctx, baseController.CODE.PASSWORD_ERROR, '密码错误');
}
await doLogin(ctx, user);
baseController.response(ctx);
}).post('registe', async ctx => {
let params = ctx.request.body;
if (!params) return baseController.response400(ctx);
if (!params.username || !params.password || !params.nickname) {
return baseController.response400(ctx);
}
if (!baseController.REG.USERNAME.test(params.username)) return baseController.response400(ctx, '用户名不合法');
if (!baseController.REG.PASSWORD.test(params.password)) return baseController.response400(ctx, '密码不合法');
let user = await userModel.selectByUsername(params.username);
if (user) return baseController.responseWithCode(ctx, baseController.CODE.EXISTING_USER, '用户账号已存在');
user = await userModel.save({
username: params.username,
password: util.md5(params.username + params.password),
nickname: params.nickname,
sex: params.sex ? params.sex : '-1',
sign: params.sign,
headImg: params.sex == '0' ? baseConfig.headImgGirl : (params.sex == '1' ? baseConfig.headImgBoy : baseConfig.headImgUnknown)
});
await doLogin(ctx, user);
baseController.response(ctx);
}).routes();
const doLogin = async (ctx, user) => {
let nowTime = Date.now();
let token = util.md5(util.uuid() + nowTime + user.username);
let userInfo = {
id: user._id.toString(),
username: user.username,
nickname: user.nickname,
headImg: user.headImg,
sign: user.sign,
sex: user.sex
};
let userInfoJsonStr = JSON.stringify(userInfo);
let authEx = baseController.CONSTS.AUTH_COOKIE_EXPIRES_DAY * 24 * 60 * 60;
// 写入redis验证数据
await asyncRedisClient.setAsync(redisKey.AUTH_TOKEN(token), userInfoJsonStr, 'EX', authEx);
// 登录成功
let cookieOpt = {
httpOnly: true,
path: '/',
domain: baseConfig.domain
};
cookieOpt.maxAge = authEx * 1000;
// 创建cookies会话凭证信息
baseController.setCookie(ctx, cookiesName.COOKIE_NAME_TOKEN, token, cookieOpt);
cookieOpt.httpOnly = false;
baseController.setCookie(ctx, cookiesName.COOKIE_NAME_UID, userInfo.id, cookieOpt);
};