123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 'use strict';
- /**
- * 登录页面控制器
- *
- * @author CaiAoLin
- * @date 2017/11/15
- * @version
- */
- const URL = require('url');
- const maintainConst = require('../const/maintain');
- 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 maintainData = await ctx.service.maintain.getDataById(1);
- await ctx.service.maintain.syncMaintainData();
- const renderData = {
- maintainData,
- maintainConst,
- 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 '用户名或密码错误';
- }
- if (result === 2) {
- 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 = error;
- 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;
- }
- /**
- * 忘记密码-重置密码
- * @param ctx
- * @returns {Promise<void>}
- */
- async resetPassword(ctx) {
- const response = {
- err: 0,
- index: 0,
- msg: '',
- };
- const code = ctx.request.body.code;
- const name = ctx.request.body.name;
- try {
- const data = await ctx.service.project.getProjectByCode(code);
- if (data) {
- const pa = await ctx.service.projectAccount.getDataByCondition({ account: name, project_id: data.id });
- if (!pa) {
- response.index = 2;
- throw '登录账号不存在,请检查是否输入错误。';
- }
- if (!pa.auth_mobile) {
- response.index = 2;
- throw '登录账号还没有认证手机,请联系项目管理员。';
- }
- // 重置密码并发短信
- const newpwd = ctx.helper.generateRandomString(6, 2);
- console.log(newpwd);
- const result = await ctx.service.projectAccount.resetPassword(pa.id, newpwd);
- if (!result) {
- throw '修改密码失败';
- }
- response.data = {
- pName: data.name,
- name: pa.name,
- mobile: pa.auth_mobile.substr(0, 3) + '****' + pa.auth_mobile.substr(7),
- account: pa.account,
- };
- } else {
- response.index = 1;
- throw '项目不存在,请检查是否输入有误。';
- }
- } catch (err) {
- response.err = 1;
- response.msg = err;
- }
- ctx.body = response;
- }
- }
- return LoginController;
- };
|