project_account.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. /**
  3. * 项目账号数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. // 加密类
  10. const crypto = require('crypto');
  11. const SSO = require('../lib/sso');
  12. module.exports = app => {
  13. class ProjectAccount extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'project_account';
  23. }
  24. /**
  25. * 数据验证规则
  26. *
  27. * @param {String} scene - 场景
  28. * @return {Object} - 返回数据
  29. */
  30. rule(scene) {
  31. let rule = {};
  32. switch (scene) {
  33. case 'login':
  34. rule = {
  35. account: { type: 'string', required: true, min: 2 },
  36. project_password: { type: 'string', required: true, min: 4 },
  37. project: { type: 'string', required: true, min: 13 },
  38. };
  39. break;
  40. case 'ssoLogin':
  41. rule = {
  42. username: { type: 'string', required: true, min: 2 },
  43. password: { type: 'string', required: true, min: 4 },
  44. };
  45. default:
  46. break;
  47. }
  48. return rule;
  49. }
  50. /**
  51. * 账号登录
  52. *
  53. * @param {Object} data - 表单post数据
  54. * @param {Number} loginType - 登录类型 1 | 2
  55. * @return {Boolean} - 返回登录结果
  56. */
  57. async accountLogin(data, loginType) {
  58. let result = false;
  59. try {
  60. // 验证数据
  61. const scene = loginType === 1 ? 'ssoLogin' : 'login';
  62. const rule = this.rule(scene);
  63. this.ctx.validate(rule, data);
  64. let accountData = {};
  65. if (loginType === 2) {
  66. // 查找项目数据
  67. const projectData = await this.ctx.service.project.getProjectByCode(data.project.toString());
  68. if (projectData === null) {
  69. throw '不存在项目数据';
  70. }
  71. // 查找对应数据
  72. accountData = await this.db.get(this.tableName, {
  73. account: data.account,
  74. project_id: projectData.id,
  75. });
  76. if (accountData === null) {
  77. throw '不存在对应用户数据';
  78. }
  79. // 判断密码
  80. if (accountData.is_admin === 1) {
  81. // 管理员则用sso通道判断
  82. const sso = new SSO(this.ctx);
  83. result = await sso.loginValid(data.account, data.project_password.toString());
  84. } else {
  85. // 加密密码
  86. const encryptPassword = crypto.createHmac('sha1', data.account).update(data.project_password)
  87. .digest().toString('base64');
  88. result = encryptPassword === accountData.password;
  89. }
  90. } else {
  91. // sso登录(演示版)
  92. const sso = new SSO(this.ctx);
  93. result = await sso.loginValid(data.username, data.password.toString());
  94. accountData.account = data.username;
  95. accountData.id = sso.accountID;
  96. console.log(accountData);
  97. }
  98. // 如果成功则更新登录时间
  99. if (result) {
  100. const currentTime = new Date().getTime() / 1000;
  101. if (loginType === 2) {
  102. const updateData = {
  103. last_login: currentTime,
  104. };
  105. await this.update(updateData, { id: accountData.id });
  106. }
  107. // 加密token
  108. const sessionToken = crypto.createHmac('sha1', currentTime + '').update(accountData.account)
  109. .digest().toString('base64');
  110. // 存入session
  111. this.ctx.session.sessionUser = {
  112. account: accountData.account,
  113. accountId: accountData.id,
  114. loginTime: currentTime,
  115. sessionToken,
  116. loginType,
  117. };
  118. }
  119. } catch (error) {
  120. console.log(error);
  121. result = false;
  122. }
  123. return result;
  124. }
  125. }
  126. return ProjectAccount;
  127. };