login_controller.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /**
  3. * 登录页面控制器
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/15
  7. * @version
  8. */
  9. const SSO = require('../lib/sso');
  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 renderData = {};
  20. await ctx.render('login/login.ejs', renderData);
  21. }
  22. /**
  23. * 登录操作
  24. *
  25. * @param {Object} ctx - egg全局变量
  26. * @return {void}
  27. */
  28. async login(ctx) {
  29. let loginType = ctx.request.body.type;
  30. try {
  31. let result = false;
  32. loginType = parseInt(loginType);
  33. if (loginType === 1) {
  34. // 演示版登陆
  35. const username = ctx.request.body.username;
  36. const password = ctx.request.body.password;
  37. const sso = new SSO(ctx);
  38. result = await sso.loginValid(username, password);
  39. } else {
  40. // 项目版登陆
  41. result = await ctx.service.projectAccount.accountLogin(ctx.request.body);
  42. }
  43. if (!result) {
  44. throw '登录失败';
  45. }
  46. } catch (error) {
  47. console.log(error);
  48. }
  49. ctx.body = 'success';
  50. }
  51. }
  52. return LoginController;
  53. };