login_controller.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (ctx.helper.isMobile(ctx.request.header['user-agent'])) {
  21. ctx.redirect('/wap');
  22. return;
  23. }
  24. const errorMessage = ctx.session.loginError;
  25. // 显示完删除
  26. ctx.session.loginError = null;
  27. // 获取系统维护信息
  28. const maintainData = await ctx.service.maintain.getDataById(1);
  29. if (!ctx.app.config.is_debug) {
  30. await ctx.service.maintain.syncMaintainData();
  31. }
  32. const renderData = {
  33. maintainData,
  34. maintainConst,
  35. errorMessage,
  36. };
  37. await ctx.render('login/login.ejs', renderData);
  38. }
  39. /**
  40. * 登录操作
  41. *
  42. * @param {Object} ctx - egg全局变量
  43. * @return {void}
  44. */
  45. async login(ctx) {
  46. let loginType = ctx.request.body.type;
  47. try {
  48. loginType = parseInt(loginType);
  49. const result = await ctx.service.projectAccount.accountLogin(ctx.request.body, loginType);
  50. if (!result) {
  51. throw '用户名或密码错误';
  52. }
  53. if (result === 2) {
  54. throw '该账号已被停用,请联系销售人员';
  55. }
  56. // 调用 rotateCsrfSecret 刷新用户的 CSRF token
  57. ctx.rotateCsrfSecret();
  58. // 判断是否已经有对应用户信息,没有则跳转初始化页面
  59. const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
  60. const url = needBoot ? '/boot' : '/dashboard';
  61. const query = URL.parse(ctx.request.header.referer, true).query;
  62. ctx.redirect(query.referer ? query.referer : url);
  63. } catch (error) {
  64. this.log(error);
  65. ctx.session.loginError = error;
  66. ctx.redirect('/login');
  67. }
  68. }
  69. /**
  70. * 退出登录
  71. *
  72. * @param {Object} ctx - egg全局变量
  73. * @return {void}
  74. */
  75. async logout(ctx) {
  76. // 删除session并跳转
  77. ctx.session = null;
  78. ctx.redirect('/');
  79. }
  80. /**
  81. * 获取项目名
  82. *
  83. * @param {Object} ctx - egg全局context
  84. * @return {void}
  85. */
  86. async projectName(ctx) {
  87. const response = {
  88. err: 0,
  89. msg: '',
  90. };
  91. const code = ctx.query.code;
  92. try {
  93. const data = await ctx.service.project.getProjectByCode(code);
  94. if (data) {
  95. response.data = data.name;
  96. } else {
  97. throw '项目不存在';
  98. }
  99. } catch (err) {
  100. response.err = 1;
  101. response.msg = err;
  102. }
  103. ctx.body = response;
  104. }
  105. /**
  106. * 忘记密码-重置密码
  107. * @param ctx
  108. * @returns {Promise<void>}
  109. */
  110. async resetPassword(ctx) {
  111. const response = {
  112. err: 0,
  113. index: 0,
  114. msg: '',
  115. };
  116. const code = ctx.request.body.code;
  117. const name = ctx.request.body.name;
  118. try {
  119. const data = await ctx.service.project.getProjectByCode(code);
  120. if (data) {
  121. const pa = await ctx.service.projectAccount.getDataByCondition({ account: name, project_id: data.id });
  122. if (!pa) {
  123. response.index = 2;
  124. throw '登录账号不存在,请检查是否输入错误。';
  125. }
  126. if (!pa.auth_mobile) {
  127. response.index = 2;
  128. throw '登录账号还没有认证手机,请联系项目管理员。';
  129. }
  130. // 重置密码并发短信
  131. const newpwd = ctx.helper.generateRandomString(6, 2);
  132. console.log(newpwd);
  133. const result = await ctx.service.projectAccount.resetPassword(pa.id, newpwd);
  134. if (!result) {
  135. throw '修改密码失败';
  136. }
  137. response.data = {
  138. pName: data.name,
  139. name: pa.name,
  140. mobile: pa.auth_mobile.substr(0, 3) + '****' + pa.auth_mobile.substr(7),
  141. account: pa.account,
  142. };
  143. } else {
  144. response.index = 1;
  145. throw '项目不存在,请检查是否输入有误。';
  146. }
  147. } catch (err) {
  148. response.err = 1;
  149. response.msg = err;
  150. }
  151. ctx.body = response;
  152. }
  153. /**
  154. * 接口登录页面
  155. *
  156. * @param {Object} ctx - egg全局页面
  157. * @return {void}
  158. */
  159. async port(ctx) {
  160. // 获取系统维护信息
  161. const maintainData = await ctx.service.maintain.getDataById(1);
  162. if (!ctx.app.config.is_debug) {
  163. await ctx.service.maintain.syncMaintainData();
  164. }
  165. let pa;
  166. try {
  167. if (ctx.session.loginError !== null) {
  168. throw ctx.session.loginError;
  169. }
  170. if (!ctx.query.mobile) {
  171. throw '参数有误';
  172. }
  173. pa = await ctx.service.projectAccount.getDataByCondition({ mobile: ctx.query.mobile, project_id: ctx.projectData.id });
  174. if (!pa) {
  175. throw '您无权限登录系统。';
  176. }
  177. if (pa.bind === 0) {
  178. // 先绑定再登录
  179. throw '';
  180. } else {
  181. if (pa.enable !== 1) {
  182. throw '该账号已被停用,请联系销售人员';
  183. }
  184. const result = await ctx.service.projectAccount.accountLogin({ project: ctx.projectData, accountData: pa }, 3);
  185. if (!result) {
  186. throw '登录出错';
  187. }
  188. ctx.redirect('/dashboard');
  189. }
  190. } catch (error) {
  191. this.log(error);
  192. ctx.session.loginError = error;
  193. }
  194. const errorMessage = ctx.session.loginError;
  195. // 显示完删除
  196. ctx.session.loginError = null;
  197. const renderData = {
  198. maintainData,
  199. maintainConst,
  200. errorMessage,
  201. projectData: ctx.projectData,
  202. accountData: pa,
  203. };
  204. await ctx.render('login/login_port.ejs', renderData);
  205. }
  206. /**
  207. * 登录操作
  208. *
  209. * @param {Object} ctx - egg全局变量
  210. * @return {void}
  211. */
  212. async loginPort(ctx) {
  213. let loginType = ctx.request.body.type;
  214. try {
  215. loginType = parseInt(loginType);
  216. const data = await ctx.service.project.getProjectByCode(ctx.request.body.projectCode.toString().trim());
  217. if (data === null) {
  218. throw '不存在项目数据';
  219. }
  220. if (data.custom === 0) {
  221. throw '无法通过接口登录本系统';
  222. }
  223. if (data && data.custom === 1) {
  224. const pa = await ctx.service.projectAccount.getDataById(ctx.request.body.accountId);
  225. if (!pa) {
  226. throw '您无权限登录系统。';
  227. }
  228. if (pa.enable !== 1) {
  229. throw '该账号已被停用,请联系销售人员';
  230. }
  231. const updateData = {
  232. bind: 1,
  233. };
  234. await ctx.service.projectAccount.update(updateData, { id: pa.id });
  235. const result = await ctx.service.projectAccount.accountLogin({ project: data, accountData: pa }, loginType);
  236. if (!result) {
  237. throw '绑定登录出错,请使用账号密码登录';
  238. }
  239. ctx.redirect('/dashboard');
  240. }
  241. } catch (error) {
  242. this.log(error);
  243. ctx.session.loginError = error;
  244. ctx.redirect('/login');
  245. }
  246. }
  247. }
  248. return LoginController;
  249. };