import { Provide } from '@midwayjs/decorator'; import { InjectEntityModel } from '@midwayjs/typegoose'; import { ReturnModelType } from '@typegoose/typegoose'; import { FrameworkErrorEnum, ServiceError } from 'free-midway-component'; import { User } from '../../entity/User.entity'; import { CheckUpdateCardAndPid, checkPhoneAndPid, } from '../../interface/util/user.util.interface'; @Provide() export class UserUtilService { @InjectEntityModel(User) UserModel: ReturnModelType; /** * 检查创建时,检查手机号和姓名 * @param data 参数 */ async checkPhoneAndPid(data: checkPhoneAndPid) { if (data.tel) { // 检查手机号 const useran = await this.UserModel.count({ tel: data.tel }); if (useran > 0) { throw new ServiceError( '已有用户 使用该手机号!', FrameworkErrorEnum.BAD_BODY ); } } if (data.name) { // 检查姓名 const username = await this.UserModel.count({ name: data.name }); if (username > 0) { throw new ServiceError( '已有用户 使用该名称!', FrameworkErrorEnum.BAD_BODY ); } } if (data.openid) { // 检查opneid let openidan; openidan = await this.UserModel.count({ openid: data.openid, status: '0', }); if (openidan > 0) { throw new ServiceError( '注册成功 管理员审核中!', FrameworkErrorEnum.BAD_BODY ); } openidan = await this.UserModel.count({ openid: data.openid, status: '1', }); if (openidan > 0) { throw new ServiceError( '已有用户信息 请直接登录!', FrameworkErrorEnum.BAD_BODY ); } } } /** * 检查创建时,检查手机号和姓名 * @param id 当前要修改的数据 * @param data 参数 */ async checkUpdateCardAndPid(id, data: CheckUpdateCardAndPid) { const { tel, name } = data; if (tel) { const useran = await this.UserModel.count({ _id: { $ne: id }, tel, }); if (useran > 0) { throw new ServiceError( '已有用户 使用该手机号!', FrameworkErrorEnum.BAD_BODY ); } } if (name) { const username = await this.UserModel.count({ _id: { $ne: id }, name, }); if (username > 0) { throw new ServiceError( '已有用户 使用该名称!', FrameworkErrorEnum.BAD_BODY ); } } } }