login_controller.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * 接口登录页面
  151. *
  152. * @param {Object} ctx - egg全局页面
  153. * @return {void}
  154. */
  155. async port(ctx) {
  156. // 获取系统维护信息
  157. const maintainData = await ctx.service.maintain.getDataById(1);
  158. if (!ctx.app.config.is_debug) {
  159. await ctx.service.maintain.syncMaintainData();
  160. }
  161. let pa;
  162. try {
  163. if (ctx.session.loginError !== null) {
  164. throw ctx.session.loginError;
  165. }
  166. if (!ctx.query.mobile) {
  167. throw '参数有误';
  168. }
  169. pa = await ctx.service.projectAccount.getDataByCondition({ mobile: ctx.query.mobile, project_id: ctx.projectData.id });
  170. if (!pa) {
  171. throw '您无权限登录系统。';
  172. }
  173. if (pa.bind === 0) {
  174. // 先绑定再登录
  175. throw '';
  176. } else {
  177. if (pa.enable !== 1) {
  178. throw '该账号已被停用,请联系销售人员';
  179. }
  180. const result = await ctx.service.projectAccount.accountLogin({ project: ctx.projectData, accountData: pa }, 3);
  181. if (!result) {
  182. throw '登录出错';
  183. }
  184. ctx.redirect('/dashboard');
  185. }
  186. } catch (error) {
  187. this.log(error);
  188. ctx.session.loginError = error;
  189. }
  190. const errorMessage = ctx.session.loginError;
  191. // 显示完删除
  192. ctx.session.loginError = null;
  193. const renderData = {
  194. maintainData,
  195. maintainConst,
  196. errorMessage,
  197. projectData: ctx.projectData,
  198. accountData: pa,
  199. };
  200. await ctx.render('login/login_port.ejs', renderData);
  201. }
  202. /**
  203. * 登录操作
  204. *
  205. * @param {Object} ctx - egg全局变量
  206. * @return {void}
  207. */
  208. async loginPort(ctx) {
  209. let loginType = ctx.request.body.type;
  210. try {
  211. loginType = parseInt(loginType);
  212. const data = await ctx.service.project.getProjectByCode(ctx.request.body.code.toString().trim());
  213. if (data === null) {
  214. throw '不存在项目数据';
  215. }
  216. if (data.custom === 0) {
  217. throw '无法通过接口登录本系统';
  218. }
  219. if (data && data.custom === 1) {
  220. const pa = await ctx.service.projectAccount.getDataById(ctx.request.body.accountId);
  221. if (!pa) {
  222. throw '您无权限登录系统。';
  223. }
  224. if (pa.enable !== 1) {
  225. throw '该账号已被停用,请联系销售人员';
  226. }
  227. const updateData = {
  228. bind: 1,
  229. };
  230. await ctx.service.projectAccount.update(updateData, { id: pa.id });
  231. const result = await ctx.service.projectAccount.accountLogin({ project: data, accountData: pa }, loginType);
  232. if (!result) {
  233. throw '绑定登录出错,请使用账号密码登录';
  234. }
  235. ctx.redirect('/dashboard');
  236. }
  237. } catch (error) {
  238. this.log(error);
  239. ctx.session.loginError = error;
  240. ctx.redirect('/login');
  241. }
  242. }
  243. }
  244. return LoginController;
  245. };