login_controller.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // 判断是否已经有对应用户信息,没有则跳转初始化页面
  53. const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
  54. const url = needBoot ? '/boot' : '/dashboard';
  55. const query = URL.parse(ctx.request.header.referer, true).query;
  56. ctx.redirect(query.referer ? query.referer : url);
  57. } catch (error) {
  58. this.log(error);
  59. ctx.session.loginError = error;
  60. ctx.redirect('/login');
  61. }
  62. }
  63. /**
  64. * 退出登录
  65. *
  66. * @param {Object} ctx - egg全局变量
  67. * @return {void}
  68. */
  69. async logout(ctx) {
  70. // 删除session并跳转
  71. ctx.session = null;
  72. ctx.redirect('/');
  73. }
  74. /**
  75. * 获取项目名
  76. *
  77. * @param {Object} ctx - egg全局context
  78. * @return {void}
  79. */
  80. async projectName(ctx) {
  81. const response = {
  82. err: 0,
  83. msg: '',
  84. };
  85. const code = ctx.query.code;
  86. try {
  87. const data = await ctx.service.project.getProjectByCode(code);
  88. if (data) {
  89. response.data = data.name;
  90. } else {
  91. throw '项目不存在';
  92. }
  93. } catch (err) {
  94. response.err = 1;
  95. response.msg = err;
  96. }
  97. ctx.body = response;
  98. }
  99. /**
  100. * 忘记密码-重置密码
  101. * @param ctx
  102. * @returns {Promise<void>}
  103. */
  104. async resetPassword(ctx) {
  105. const response = {
  106. err: 0,
  107. index: 0,
  108. msg: '',
  109. };
  110. const code = ctx.request.body.code;
  111. const name = ctx.request.body.name;
  112. try {
  113. const data = await ctx.service.project.getProjectByCode(code);
  114. if (data) {
  115. const pa = await ctx.service.projectAccount.getDataByCondition({ account: name, project_id: data.id });
  116. if (!pa) {
  117. response.index = 2;
  118. throw '登录账号不存在,请检查是否输入错误。';
  119. }
  120. if (!pa.auth_mobile) {
  121. response.index = 2;
  122. throw '登录账号还没有认证手机,请联系项目管理员。';
  123. }
  124. // 重置密码并发短信
  125. const newpwd = ctx.helper.generateRandomString(6, 2);
  126. console.log(newpwd);
  127. const result = await ctx.service.projectAccount.resetPassword(pa.id, newpwd);
  128. if (!result) {
  129. throw '修改密码失败';
  130. }
  131. response.data = {
  132. pName: data.name,
  133. name: pa.name,
  134. mobile: pa.auth_mobile.substr(0, 3) + '****' + pa.auth_mobile.substr(7),
  135. account: pa.account,
  136. };
  137. } else {
  138. response.index = 1;
  139. throw '项目不存在,请检查是否输入有误。';
  140. }
  141. } catch (err) {
  142. response.err = 1;
  143. response.msg = err;
  144. }
  145. ctx.body = response;
  146. }
  147. }
  148. return LoginController;
  149. };