|
@@ -42,6 +42,11 @@ module.exports = app => {
|
|
project: { type: 'string', required: true, min: 13 },
|
|
project: { type: 'string', required: true, min: 13 },
|
|
};
|
|
};
|
|
break;
|
|
break;
|
|
|
|
+ case 'ssoLogin':
|
|
|
|
+ rule = {
|
|
|
|
+ username: { type: 'string', required: true, min: 2 },
|
|
|
|
+ password: { type: 'string', required: true, min: 4 },
|
|
|
|
+ };
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -53,49 +58,76 @@ module.exports = app => {
|
|
* 账号登录
|
|
* 账号登录
|
|
*
|
|
*
|
|
* @param {Object} data - 表单post数据
|
|
* @param {Object} data - 表单post数据
|
|
|
|
+ * @param {Number} loginType - 登录类型 1 | 2
|
|
* @return {Boolean} - 返回登录结果
|
|
* @return {Boolean} - 返回登录结果
|
|
*/
|
|
*/
|
|
- async accountLogin(data) {
|
|
|
|
|
|
+ async accountLogin(data, loginType) {
|
|
let result = false;
|
|
let result = false;
|
|
try {
|
|
try {
|
|
// 验证数据
|
|
// 验证数据
|
|
- const rule = this.rule('login');
|
|
|
|
|
|
+ const scene = loginType === 1 ? 'ssoLogin' : 'login';
|
|
|
|
+ const rule = this.rule(scene);
|
|
this.ctx.validate(rule, data);
|
|
this.ctx.validate(rule, data);
|
|
|
|
|
|
- // 查找项目数据
|
|
|
|
- const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString());
|
|
|
|
- if (projectData === null) {
|
|
|
|
- throw '不存在项目数据';
|
|
|
|
- }
|
|
|
|
|
|
+ let accountData = {};
|
|
|
|
+ if (loginType === 2) {
|
|
|
|
+ // 查找项目数据
|
|
|
|
+ const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString());
|
|
|
|
+ if (projectData === null) {
|
|
|
|
+ throw '不存在项目数据';
|
|
|
|
+ }
|
|
|
|
|
|
- // 查找对应数据
|
|
|
|
- const accountData = await this.db.get(this.tableName, {
|
|
|
|
- account: data.account,
|
|
|
|
- project_id: projectData.id,
|
|
|
|
- });
|
|
|
|
|
|
+ // 查找对应数据
|
|
|
|
+ accountData = await this.db.get(this.tableName, {
|
|
|
|
+ account: data.account,
|
|
|
|
+ project_id: projectData.id,
|
|
|
|
+ });
|
|
|
|
|
|
- if (accountData === null) {
|
|
|
|
- throw '不存在对应用户数据';
|
|
|
|
- }
|
|
|
|
|
|
+ if (accountData === null) {
|
|
|
|
+ throw '不存在对应用户数据';
|
|
|
|
+ }
|
|
|
|
|
|
- // 判断密码
|
|
|
|
- if (accountData.is_admin === 1) {
|
|
|
|
- // 管理员则用sso通道判断
|
|
|
|
- const sso = new SSO(this.ctx);
|
|
|
|
- result = await sso.loginValid(data.account, data.project_password.toString());
|
|
|
|
|
|
+ // 判断密码
|
|
|
|
+ if (accountData.is_admin === 1) {
|
|
|
|
+ // 管理员则用sso通道判断
|
|
|
|
+ const sso = new SSO(this.ctx);
|
|
|
|
+ result = await sso.loginValid(data.account, data.project_password.toString());
|
|
|
|
+ } else {
|
|
|
|
+ // 加密密码
|
|
|
|
+ const encryptPassword = crypto.createHmac('sha1', data.account).update(data.project_password)
|
|
|
|
+ .digest().toString('base64');
|
|
|
|
+ result = encryptPassword === accountData.password;
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- // 加密密码
|
|
|
|
- const encryptPassword = crypto.createHmac('sha1', data.account).update(data.project_password)
|
|
|
|
- .digest().toString('base64');
|
|
|
|
- result = encryptPassword === accountData.password;
|
|
|
|
|
|
+ // sso登录(演示版)
|
|
|
|
+ const sso = new SSO(this.ctx);
|
|
|
|
+ result = await sso.loginValid(data.username, data.password.toString());
|
|
|
|
+ accountData.account = data.username;
|
|
|
|
+ accountData.id = sso.accountID;
|
|
|
|
+ console.log(accountData);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
// 如果成功则更新登录时间
|
|
// 如果成功则更新登录时间
|
|
if (result) {
|
|
if (result) {
|
|
- const updateData = {
|
|
|
|
- last_login: new Date().getTime() / 1000,
|
|
|
|
|
|
+ const currentTime = new Date().getTime() / 1000;
|
|
|
|
+ if (loginType === 2) {
|
|
|
|
+ const updateData = {
|
|
|
|
+ last_login: currentTime,
|
|
|
|
+ };
|
|
|
|
+ await this.update(updateData, { id: accountData.id });
|
|
|
|
+ }
|
|
|
|
+ // 加密token
|
|
|
|
+ const sessionToken = crypto.createHmac('sha1', currentTime + '').update(accountData.account)
|
|
|
|
+ .digest().toString('base64');
|
|
|
|
+ // 存入session
|
|
|
|
+ this.ctx.session.sessionUser = {
|
|
|
|
+ account: accountData.account,
|
|
|
|
+ accountId: accountData.id,
|
|
|
|
+ loginTime: currentTime,
|
|
|
|
+ sessionToken,
|
|
|
|
+ loginType,
|
|
};
|
|
};
|
|
- await this.update(updateData, { id: accountData.id });
|
|
|
|
}
|
|
}
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.log(error);
|
|
console.log(error);
|