login_controller.js 5.0 KB

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