123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 'use strict';
- /**
- * 登录页面控制器
- *
- * @author CaiAoLin
- * @date 2017/11/15
- * @version
- */
- const URL = require('url');
- module.exports = app => {
- class LoginController extends app.BaseController {
- /**
- * 登录页面
- *
- * @param {Object} ctx - egg全局页面
- * @return {void}
- */
- async index(ctx) {
- const errorMessage = ctx.session.loginError;
- // 显示完删除
- ctx.session.loginError = null;
- const renderData = {
- errorMessage,
- };
- await ctx.render('login/login.ejs', renderData);
- }
- /**
- * 登录操作
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async login(ctx) {
- let loginType = ctx.request.body.type;
- try {
- loginType = parseInt(loginType);
- const result = await ctx.service.projectAccount.accountLogin(ctx.request.body, loginType);
- if (!result) {
- throw '登录失败';
- }
- // 判断是否已经有对应用户信息,没有则跳转初始化页面
- const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
- const url = needBoot ? '/boot' : '/dashboard';
- const query = URL.parse(ctx.request.header.referer, true).query;
- ctx.redirect(query.referer ? query.referer : url);
- } catch (error) {
- this.log(error);
- ctx.session.loginError = '用户名或密码错误';
- ctx.redirect('/login');
- }
- }
- /**
- * 退出登录
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async logout(ctx) {
- // 删除session并跳转
- ctx.session = null;
- ctx.redirect('/');
- }
- /**
- * 获取项目名
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async projectName(ctx) {
- const response = {
- err: 0,
- msg: '',
- };
- const code = ctx.query.code;
- try {
- const data = await ctx.service.project.getProjectByCode(code);
- if (data) {
- response.data = data.name;
- } else {
- throw '项目不存在';
- }
- } catch (err) {
- response.err = 1;
- response.msg = err;
- }
- ctx.body = response;
- }
- }
- return LoginController;
- };
|