login_controller.js 4.8 KB

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