authAction.js
4.82 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
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
/**
* Created by lintry on 2017-5-8.
*/
"use strict";
module.exports = function (user_type, dbo) {
if (user_type === void 0) {
throw new Error('请明确指定登录平台');
}
//api公共模块
const _ = require('lodash'),
Promise = require('bluebird'),
po = global.po,
config = global.config,
Result = require('kml-express-stage-lib').Result,
logger = global.loggers.system,
ENUM = config.ENUM,
cache_config = config.cache,
crypto_utils = require('kml-crypto-utils'),
redisDb = require('../init/redis-promisify');
const ExpressPassport = require('kml-common-module').ExpressPassport,
express_passport = new ExpressPassport({
server_id: global.config.system.project_name,
ttl: cache_config.EXPRESS_TTL,
redis_client: redisDb
});
/**
* 登录系统
* @param req
* @param res
* @param db
* @return {*}
* 仅供调试用
*/
this.sysloginPost = async function (req, res, db) {
const params = req.body;
if (!params || !params.userCode || !params.userPassword) return Result.Error('参数错误');
const sendData = {
userCode: params.userCode,
userPassword: params.userPassword+'@'+params.userCode
};
try {
const
User = po.import(db, 'user'),
userWhereObj = {
user_code: sendData.userCode,
status: ENUM.TYPE.ENABLE
};
const
userInfoArray = _.difference(Object.keys(User.fieldRawAttributesMap), ENUM.DEFAULT_PARAMS_ARRAY);
const userInfo = await User.findOne({where: userWhereObj, attributes: userInfoArray});
if (!userInfo) return Result.Error('账号或密码错误');
// 检查密码编码后是否与数据库存储的编码一致
let matched = crypto_utils.hashMatch('md5', sendData.userPassword, userInfo.user_password);
if (!matched) {
return Result.Error('账号或密码错误');
}
sendData.platId = userInfo.plat_id;
sendData.userType = userInfo.user_type;
const data = {
user: userInfo.toJSON()
};
//屏蔽用户密码输出
data.user.user_password = void 0;
return express_passport.create(req, res, data).then((express_data) => {
return Result.Ok('登录成功', data);
});
} catch(err) {
logger.error('error', err);
return Result.Error('登录失败');
}
};
/**
* 注销
* @param req
* @param res
*/
this.logoutGet = async function (req, res) {
const active_user = req.session.active_user;
await express_passport.destroy(req, res);
if (!active_user) {
req.session.destroy();
return Result.Ok('注销成功');
}
req.session.destroy();
const key = `${active_user.user_id}@${active_user.plat_id}`;
return redisDb
.DELAsync(key)
.then(() => {
return Result.Ok('注销成功');
})
.catch(err => {
logger.error(`${req.baseUrl}${req.url} => `, err);
return Result.Error('注销成功');
});
};
/**
* 快速登录 不分平台直接登录
* @param req
* @param res
* @returns {Promise.<T>}
*/
this.quickPass = function (req, res, db) {
return express_passport.validate(req, res)
.then(async function (express_data) {
if (express_data) {
const
User = po.import(db, 'user'),
userWhereObj = {
user_id: express_data.user_id
};
const
userInfoArray = _.difference(Object.keys(User.fieldRawAttributesMap), ENUM.DEFAULT_PARAMS_ARRAY);
const userInfo = await User.findOne({where: userWhereObj, attributes: userInfoArray});
if (!userInfo) return Result.Error('登录信息已失效, 请重新登录!');
const data = {
user: userInfo.toJSON()
};
//屏蔽用户密码输出
data.user.user_password = void 0;
return express_passport.refresh(req, res, data).then(() => {
return Result.Ok('登录成功', data);
});
} else {
res.status(401);
return Result.Error('登录信息已失效, 请重新登录!');
}
});
};
};