|
@@ -1,5 +1,5 @@
|
|
import { Config, Inject, Provide } from '@midwayjs/core';
|
|
import { Config, Inject, Provide } from '@midwayjs/core';
|
|
-import { FrameworkErrorEnum, GetModel, ServiceError } from 'free-midway-component';
|
|
|
|
|
|
+import { GetModel, ServiceError } from 'free-midway-component';
|
|
import { get, isEqual, upperFirst } from 'lodash';
|
|
import { get, isEqual, upperFirst } from 'lodash';
|
|
import { LoginDTO, LoginType, UPwdDTO } from '../interface/login.interface';
|
|
import { LoginDTO, LoginType, UPwdDTO } from '../interface/login.interface';
|
|
import { RoleService } from '../service/system/role.service';
|
|
import { RoleService } from '../service/system/role.service';
|
|
@@ -7,11 +7,11 @@ import { RedisService } from '@midwayjs/redis';
|
|
import * as Crypto from 'crypto-js';
|
|
import * as Crypto from 'crypto-js';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { I18nService } from './i18n.service';
|
|
import { I18nService } from './i18n.service';
|
|
|
|
+import { FrameErrorEnum } from '../error/frame.error';
|
|
@Provide()
|
|
@Provide()
|
|
export class LoginService {
|
|
export class LoginService {
|
|
@Inject()
|
|
@Inject()
|
|
roleService: RoleService;
|
|
roleService: RoleService;
|
|
-
|
|
|
|
@Inject()
|
|
@Inject()
|
|
ctx: Context;
|
|
ctx: Context;
|
|
@Config('loginSign')
|
|
@Config('loginSign')
|
|
@@ -41,9 +41,9 @@ export class LoginService {
|
|
|
|
|
|
async onePointCheck() {
|
|
async onePointCheck() {
|
|
const user = this.ctx.user;
|
|
const user = this.ctx.user;
|
|
- if (!user) throw new ServiceError(this.i18n.translateError(FrameworkErrorEnum.NOT_LOGIN), FrameworkErrorEnum.NOT_LOGIN);
|
|
|
|
|
|
+ if (!user) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.NOT_LOGIN), FrameErrorEnum.NOT_LOGIN);
|
|
const { _id, role, login_code } = user;
|
|
const { _id, role, login_code } = user;
|
|
- if (!login_code) throw new ServiceError(this.i18n.translateError(FrameworkErrorEnum.NOT_LOGIN), FrameworkErrorEnum.NOT_LOGIN);
|
|
|
|
|
|
+ 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 decodeResult = Crypto.AES.decrypt(login_code, this.jwtSecret);
|
|
const decode = Crypto.enc.Utf8.stringify(decodeResult).toString();
|
|
const decode = Crypto.enc.Utf8.stringify(decodeResult).toString();
|
|
@@ -54,13 +54,13 @@ export class LoginService {
|
|
const rediskey = `${this.loginSign}:${role}:${_id}`;
|
|
const rediskey = `${this.loginSign}:${role}:${_id}`;
|
|
// 取出当前记录在案的 code
|
|
// 取出当前记录在案的 code
|
|
const redisCode = await this.redisService.get(rediskey);
|
|
const redisCode = await this.redisService.get(rediskey);
|
|
- if (!redisCode) throw new ServiceError(this.i18n.translateError(`${FrameworkErrorEnum.NOT_LOGIN}-1`), FrameworkErrorEnum.NOT_LOGIN);
|
|
|
|
|
|
+ if(!redisCode) throw new ServiceError(this.i18n.translateError(`${FrameErrorEnum.ACCOUNT_HAS_EXPIRED}`), FrameErrorEnum.ACCOUNT_HAS_EXPIRED);
|
|
// 判断是否一致
|
|
// 判断是否一致
|
|
if (code === redisCode) {
|
|
if (code === redisCode) {
|
|
// 一致,延时
|
|
// 一致,延时
|
|
await this.redisService.expire(rediskey, this.jwtExpiresIn);
|
|
await this.redisService.expire(rediskey, this.jwtExpiresIn);
|
|
} else {
|
|
} else {
|
|
- throw new ServiceError(this.i18n.translateError(`${FrameworkErrorEnum.NOT_LOGIN}-2`), FrameworkErrorEnum.NOT_LOGIN);
|
|
|
|
|
|
+ throw new ServiceError(this.i18n.translateError(`${FrameErrorEnum.ACCOUNT_LOGGED_IN_ELESWHERE}`), FrameErrorEnum.ACCOUNT_LOGGED_IN_ELESWHERE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -73,9 +73,9 @@ export class LoginService {
|
|
async loginByAccount(data: LoginDTO, type: LoginType) {
|
|
async loginByAccount(data: LoginDTO, type: LoginType) {
|
|
const model = GetModel(upperFirst(type));
|
|
const model = GetModel(upperFirst(type));
|
|
const user = await model.findOne({ account: data.account }, '+password').lean();
|
|
const user = await model.findOne({ account: data.account }, '+password').lean();
|
|
- if (!user) throw new ServiceError('未找到用户信息', FrameworkErrorEnum.NOT_FOUND_DATA);
|
|
|
|
|
|
+ if (!user) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_NOT_FOUND), FrameErrorEnum.USER_NOT_FOUND);
|
|
await this.checkAccountCanLogin(user, type);
|
|
await this.checkAccountCanLogin(user, type);
|
|
- if (!isEqual(user.password.secret, data.password)) throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
|
|
|
+ if (!isEqual(user.password.secret, data.password)) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.BAD_PASSWORD), FrameErrorEnum.BAD_PASSWORD);
|
|
return user;
|
|
return user;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -89,19 +89,19 @@ export class LoginService {
|
|
// 其他用户需要看status是不是'1';
|
|
// 其他用户需要看status是不是'1';
|
|
if (type === 'Admin') {
|
|
if (type === 'Admin') {
|
|
if (get(user, 'is_super') === '1') {
|
|
if (get(user, 'is_super') === '1') {
|
|
- if (get(user, 'is_use') === '1') throw new ServiceError('该用户已被禁用', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
|
|
|
+ if (get(user, 'is_use') === '1') throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_IS_DISABLED), FrameErrorEnum.USER_IS_DISABLED);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- if (get(user, 'status') !== '1') throw new ServiceError('该用户已被禁用', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
|
|
|
+ if (get(user, 'status') !== '1') throw new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_IS_DISABLED), FrameErrorEnum.USER_IS_DISABLED);
|
|
const role = await this.roleService.findOne({ code: type, is_use: '0' });
|
|
const role = await this.roleService.findOne({ code: type, is_use: '0' });
|
|
- if (!role) throw new ServiceError('当前角色下的用户无法使用!', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
|
|
|
+ if (!role) throw new ServiceError(this.i18n.translateError(FrameErrorEnum.ROLE_IS_DISABLED), FrameErrorEnum.ROLE_IS_DISABLED);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async updatePwd(data: UPwdDTO, type: LoginType) {
|
|
async updatePwd(data: UPwdDTO, type: LoginType) {
|
|
const model = GetModel(upperFirst(type));
|
|
const model = GetModel(upperFirst(type));
|
|
const user = await model.findById(data._id);
|
|
const user = await model.findById(data._id);
|
|
- if (!user) new ServiceError('未找到用户信息!', FrameworkErrorEnum.DATA_NOT_FOUND);
|
|
|
|
|
|
+ if (!user) new ServiceError(this.i18n.translateError(FrameErrorEnum.USER_NOT_FOUND), FrameErrorEnum.USER_NOT_FOUND);
|
|
user.password = data.password;
|
|
user.password = data.password;
|
|
await user.save();
|
|
await user.save();
|
|
}
|
|
}
|