user.util.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
  5. import { User } from '../../entity/User.entity';
  6. import {
  7. CheckUpdateCardAndPid,
  8. checkPhoneAndPid,
  9. } from '../../interface/util/user.util.interface';
  10. @Provide()
  11. export class UserUtilService {
  12. @InjectEntityModel(User)
  13. UserModel: ReturnModelType<typeof User>;
  14. /**
  15. * 检查创建时,检查手机号和姓名
  16. * @param data 参数
  17. */
  18. async checkPhoneAndPid(data: checkPhoneAndPid) {
  19. if (data.tel) {
  20. // 检查手机号
  21. const useran = await this.UserModel.count({ tel: data.tel });
  22. if (useran > 0) {
  23. throw new ServiceError(
  24. '已有用户 使用该手机号!',
  25. FrameworkErrorEnum.BAD_BODY
  26. );
  27. }
  28. }
  29. if (data.name) {
  30. // 检查姓名
  31. const username = await this.UserModel.count({ name: data.name });
  32. if (username > 0) {
  33. throw new ServiceError(
  34. '已有用户 使用该名称!',
  35. FrameworkErrorEnum.BAD_BODY
  36. );
  37. }
  38. }
  39. if (data.openid) {
  40. // 检查opneid
  41. let openidan;
  42. openidan = await this.UserModel.count({
  43. openid: data.openid,
  44. status: '0',
  45. });
  46. if (openidan > 0) {
  47. throw new ServiceError(
  48. '注册成功 管理员审核中!',
  49. FrameworkErrorEnum.BAD_BODY
  50. );
  51. }
  52. openidan = await this.UserModel.count({
  53. openid: data.openid,
  54. status: '1',
  55. });
  56. if (openidan > 0) {
  57. throw new ServiceError(
  58. '已有用户信息 请直接登录!',
  59. FrameworkErrorEnum.BAD_BODY
  60. );
  61. }
  62. }
  63. }
  64. /**
  65. * 检查创建时,检查手机号和姓名
  66. * @param id 当前要修改的数据
  67. * @param data 参数
  68. */
  69. async checkUpdateCardAndPid(id, data: CheckUpdateCardAndPid) {
  70. const { tel, name } = data;
  71. if (tel) {
  72. const useran = await this.UserModel.count({
  73. _id: { $ne: id },
  74. tel,
  75. });
  76. if (useran > 0) {
  77. throw new ServiceError(
  78. '已有用户 使用该手机号!',
  79. FrameworkErrorEnum.BAD_BODY
  80. );
  81. }
  82. }
  83. if (name) {
  84. const username = await this.UserModel.count({
  85. _id: { $ne: id },
  86. name,
  87. });
  88. if (username > 0) {
  89. throw new ServiceError(
  90. '已有用户 使用该名称!',
  91. FrameworkErrorEnum.BAD_BODY
  92. );
  93. }
  94. }
  95. }
  96. }