import { Provide } from '@midwayjs/core'; import { FrameworkErrorEnum, GetModel, ServiceError } from 'free-midway-component'; import { isEqual, upperFirst } from 'lodash'; import { LoginDTO, LoginType, UPwdDTO } from '../interface/login.interface'; @Provide() export class LoginService { /** * 账密登录 * @param data 用户名和密码 * @param type 用户类型 * @returns 用户信息/空值 */ 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('未找到用户信息', FrameworkErrorEnum.NOT_FOUND_DATA); if (!isEqual(user.password.secret, data.password)) throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT); return user; } async updatePwd(data: UPwdDTO, type: LoginType) { const model = GetModel(upperFirst(type)); const user = await model.findById(data._id); if (!user) new ServiceError('未找到用户信息!', FrameworkErrorEnum.DATA_NOT_FOUND); user.password = data.password; await user.save(); } }