1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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<typeof User>;
- /**
- * 检查创建时,检查手机号和姓名
- * @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
- );
- }
- }
- }
- }
|