|
@@ -1,5 +1,5 @@
|
|
|
import { Config, Inject, Provide } from '@midwayjs/core';
|
|
|
-import { GetModel, ServiceError } from 'free-midway-component';
|
|
|
+import { GetModel } from 'free-midway-component';
|
|
|
import { get, isEqual, upperFirst } from 'lodash';
|
|
|
import { LoginDTO, LoginType, UPwdDTO } from '../interface/login.interface';
|
|
|
import { RoleService } from '../service/system/role.service';
|
|
@@ -7,7 +7,7 @@ import { RedisService } from '@midwayjs/redis';
|
|
|
import * as Crypto from 'crypto-js';
|
|
|
import { Context } from '@midwayjs/koa';
|
|
|
import { I18nService } from './i18n.service';
|
|
|
-import { FrameErrorEnum } from '../error/frame.error';
|
|
|
+import { ServiceError, ErrorCode } from '../error/service.error';
|
|
|
@Provide()
|
|
|
export class LoginService {
|
|
|
@Inject()
|
|
@@ -39,31 +39,6 @@ export class LoginService {
|
|
|
return code;
|
|
|
}
|
|
|
|
|
|
- async onePointCheck() {
|
|
|
- const user = this.ctx.user;
|
|
|
- if (!user) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.NOT_LOGIN), FrameErrorEnum.NOT_LOGIN);
|
|
|
- const { _id, role, login_code } = user;
|
|
|
- if (!login_code) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.NOT_LOGIN), FrameErrorEnum.NOT_LOGIN);
|
|
|
- // 解密
|
|
|
- const decodeResult = Crypto.AES.decrypt(login_code, this.jwtSecret);
|
|
|
- const decode = Crypto.enc.Utf8.stringify(decodeResult).toString();
|
|
|
- // 取出code
|
|
|
- const codeArr = decode.split(':');
|
|
|
- const code = codeArr[codeArr.length - 1];
|
|
|
- // 拼接redis的key
|
|
|
- const rediskey = `${this.loginSign}:${role}:${_id}`;
|
|
|
- // 取出当前记录在案的 code
|
|
|
- const redisCode = await this.redisService.get(rediskey);
|
|
|
- if (!redisCode) throw new ServiceError(this.i18n.translateError(`${FrameErrorEnum.ACCOUNT_HAS_EXPIRED}`), FrameErrorEnum.ACCOUNT_HAS_EXPIRED);
|
|
|
- // 判断是否一致
|
|
|
- if (code === redisCode) {
|
|
|
- // 一致,延时
|
|
|
- await this.redisService.expire(rediskey, this.jwtExpiresIn);
|
|
|
- } else {
|
|
|
- throw new ServiceError(this.i18n.translateError(`${FrameErrorEnum.ACCOUNT_LOGGED_IN_ELESWHERE}`), FrameErrorEnum.ACCOUNT_LOGGED_IN_ELESWHERE);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 账密登录
|
|
|
* @param data 用户名和密码
|
|
@@ -73,9 +48,9 @@ export class LoginService {
|
|
|
async loginByAccount(data: LoginDTO, type: LoginType) {
|
|
|
const model = GetModel(upperFirst(type));
|
|
|
const user = await model.findOne({ account: data.account }, '+password').lean();
|
|
|
- if (!user) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_NOT_FOUND), FrameErrorEnum.USER_NOT_FOUND);
|
|
|
+ if (!user) throw new ServiceError(ErrorCode.USER_NOT_FOUND);
|
|
|
await this.checkAccountCanLogin(user, type);
|
|
|
- if (!isEqual(user.password.secret, data.password)) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.BAD_PASSWORD), FrameErrorEnum.BAD_PASSWORD);
|
|
|
+ if (!isEqual(user.password.secret, data.password)) throw new ServiceError(ErrorCode.BAD_PASSWORD);
|
|
|
return user;
|
|
|
}
|
|
|
/**
|
|
@@ -89,19 +64,19 @@ export class LoginService {
|
|
|
// 其他用户需要看status是不是'1';
|
|
|
if (type === 'Admin') {
|
|
|
if (get(user, 'is_super') === '1') {
|
|
|
- if (get(user, 'is_use') === '1') throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_IS_DISABLED), FrameErrorEnum.USER_IS_DISABLED);
|
|
|
+ if (get(user, 'is_use') === '1') throw new ServiceError(ErrorCode.USER_IS_DISABLED);
|
|
|
}
|
|
|
} else {
|
|
|
- if (get(user, 'status') !== '1') throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_IS_DISABLED), FrameErrorEnum.USER_IS_DISABLED);
|
|
|
+ if (get(user, 'status') !== '1') throw new ServiceError(ErrorCode.USER_IS_DISABLED);
|
|
|
const role = await this.roleService.findOne({ code: type, is_use: '0' });
|
|
|
- if (!role) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.ROLE_IS_DISABLED), FrameErrorEnum.ROLE_IS_DISABLED);
|
|
|
+ if (!role) throw new ServiceError(ErrorCode.ROLE_IS_DISABLED);
|
|
|
}
|
|
|
}
|
|
|
// 随机密码
|
|
|
async updatePwd(data: UPwdDTO, type: LoginType) {
|
|
|
const model = GetModel(upperFirst(type));
|
|
|
const user = await model.findById(data._id);
|
|
|
- if (!user) new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_NOT_FOUND), FrameErrorEnum.USER_NOT_FOUND);
|
|
|
+ if (!user) new ServiceError(ErrorCode.USER_NOT_FOUND);
|
|
|
user.password = data.password;
|
|
|
await user.save();
|
|
|
}
|