login_controller.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. /**
  3. * 登录页面控制器
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/15
  7. * @version
  8. */
  9. const URL = require('url');
  10. const maintainConst = require('../const/maintain');
  11. module.exports = app => {
  12. class LoginController extends app.BaseController {
  13. /**
  14. * 登录页面
  15. *
  16. * @param {Object} ctx - egg全局页面
  17. * @return {void}
  18. */
  19. async index(ctx) {
  20. const errorMessage = ctx.session.loginError;
  21. // 显示完删除
  22. ctx.session.loginError = null;
  23. // 获取系统维护信息
  24. const maintainData = await ctx.service.maintain.getDataById(1);
  25. if (!ctx.app.config.is_debug) {
  26. await ctx.service.maintain.syncMaintainData();
  27. }
  28. const renderData = {
  29. maintainData,
  30. maintainConst,
  31. errorMessage,
  32. };
  33. await ctx.render('login/login.ejs', renderData);
  34. }
  35. /**
  36. * 登录操作
  37. *
  38. * @param {Object} ctx - egg全局变量
  39. * @return {void}
  40. */
  41. async login(ctx) {
  42. let loginType = ctx.request.body.type;
  43. try {
  44. loginType = parseInt(loginType);
  45. const result = await ctx.service.projectAccount.accountLogin(ctx.request.body, loginType);
  46. if (!result) {
  47. throw '用户名或密码错误';
  48. }
  49. if (result === 2) {
  50. throw '该账号已被停用,请联系销售人员';
  51. }
  52. // 调用 rotateCsrfSecret 刷新用户的 CSRF token
  53. ctx.rotateCsrfSecret();
  54. // 判断是否已经有对应用户信息,没有则跳转初始化页面
  55. const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
  56. const url = needBoot ? '/boot' : '/dashboard';
  57. const query = URL.parse(ctx.request.header.referer, true).query;
  58. ctx.redirect(query.referer ? query.referer : url);
  59. } catch (error) {
  60. this.log(error);
  61. ctx.session.loginError = error;
  62. ctx.redirect('/login');
  63. }
  64. }
  65. /**
  66. * 退出登录
  67. *
  68. * @param {Object} ctx - egg全局变量
  69. * @return {void}
  70. */
  71. async logout(ctx) {
  72. // 删除session并跳转
  73. ctx.session = null;
  74. ctx.redirect('/');
  75. }
  76. /**
  77. * 获取项目名
  78. *
  79. * @param {Object} ctx - egg全局context
  80. * @return {void}
  81. */
  82. async projectName(ctx) {
  83. const response = {
  84. err: 0,
  85. msg: '',
  86. };
  87. const code = ctx.query.code;
  88. try {
  89. const data = await ctx.service.project.getProjectByCode(code);
  90. if (data) {
  91. response.data = data.name;
  92. } else {
  93. throw '项目不存在';
  94. }
  95. } catch (err) {
  96. response.err = 1;
  97. response.msg = err;
  98. }
  99. ctx.body = response;
  100. }
  101. /**
  102. * 忘记密码-重置密码
  103. * @param ctx
  104. * @returns {Promise<void>}
  105. */
  106. async resetPassword(ctx) {
  107. const response = {
  108. err: 0,
  109. index: 0,
  110. msg: '',
  111. };
  112. const code = ctx.request.body.code;
  113. const name = ctx.request.body.name;
  114. try {
  115. const data = await ctx.service.project.getProjectByCode(code);
  116. if (data) {
  117. const pa = await ctx.service.projectAccount.getDataByCondition({ account: name, project_id: data.id });
  118. if (!pa) {
  119. response.index = 2;
  120. throw '登录账号不存在,请检查是否输入错误。';
  121. }
  122. if (!pa.auth_mobile) {
  123. response.index = 2;
  124. throw '登录账号还没有认证手机,请联系项目管理员。';
  125. }
  126. // 重置密码并发短信
  127. const newpwd = ctx.helper.generateRandomString(6, 2);
  128. console.log(newpwd);
  129. const result = await ctx.service.projectAccount.resetPassword(pa.id, newpwd);
  130. if (!result) {
  131. throw '修改密码失败';
  132. }
  133. response.data = {
  134. pName: data.name,
  135. name: pa.name,
  136. mobile: pa.auth_mobile.substr(0, 3) + '****' + pa.auth_mobile.substr(7),
  137. account: pa.account,
  138. };
  139. } else {
  140. response.index = 1;
  141. throw '项目不存在,请检查是否输入有误。';
  142. }
  143. } catch (err) {
  144. response.err = 1;
  145. response.msg = err;
  146. }
  147. ctx.body = response;
  148. }
  149. }
  150. return LoginController;
  151. };