Jelajahi Sumber

邮箱唯一

lrf 6 bulan lalu
induk
melakukan
e7c025a67c

+ 3 - 0
src/controller/system/user.controller.ts

@@ -64,6 +64,7 @@ export class UserController implements BaseController {
   async create(@Body() data: object) {
     await this.service.createExamine(data);
     await this.service.checkPhone(data);
+    await this.service.checkEmail(data);
     const dbData = await this.service.create(data);
     const result = new CVO_user(dbData);
     return result;
@@ -75,6 +76,8 @@ export class UserController implements BaseController {
   @ApiResponse({ type: UVAO_user })
   async update(@Param('id') id: number, @Body() data: object) {
     if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
+    await this.service.checkPhone(data)
+    await this.service.checkEmail(data)
     const result = await this.service.update({ id }, data);
     return result;
   }

+ 1 - 0
src/error/service.error.ts

@@ -18,6 +18,7 @@ export enum ErrorCode {
   DUPLICATE_USERS_ACCOUNT = 'DUPLICATE_USERS_ACCOUNT',
   DUPLICATE_USERS_OPENID = 'DUPLICATE_USERS_OPENID',
   PHONE_IS_EXISTS = 'PHONE_IS_EXISTS',
+  EMAIL_IS_EXISTS = 'EMAIL_IS_EXISTS',
   LOGIN_VALICODE_EXPIRES = 'LOGIN_VALICODE_EXPIRES',
   LOGIN_VALICODE_ERROR = 'LOGIN_VALICODE_ERROR',
   PLEASE_INPUT_LOGIN_CODE = 'PLEASE_INPUT_LOGIN_CODE',

+ 4 - 4
src/service/login.service.ts

@@ -52,10 +52,10 @@ export class LoginService {
    * 注册验证 验证码
    * @param phone 电话号码
    * @param code 验证码
-   * @returns 
+   * @returns
    */
-  async checkRegCode(phone,code) {
-    const redisKey = `${this.regSmsSign}:${phone}`
+  async checkRegCode(phone, code) {
+    const redisKey = `${this.regSmsSign}:${phone}`;
     const redisCode = await this.redisService.get(redisKey);
     if (!redisCode) throw new ServiceError(ErrorCode.REG_VALICODE_EXPIRES); // 验证码已过期
     if (`${redisCode}` !== `${code}`) throw new ServiceError(ErrorCode.REG_VALICODE_ERROR); // 验证码错误
@@ -68,7 +68,7 @@ export class LoginService {
    * @param phone 电话号码
    */
   async sendRegCode(phone) {
-    const redisKey = `${this.regSmsSign}:${phone}`
+    const redisKey = `${this.regSmsSign}:${phone}`;
     const code = random(1000, 999999);
     await this.redisService.set(redisKey, code, 'EX', this.timeLimit);
     await this.smsService.send(phone, code);

+ 17 - 1
src/service/system/user.service.ts

@@ -18,11 +18,27 @@ export class UserService extends BaseServiceV2 {
     else data.status = '1';
     return data;
   }
+  async checkEmail(data) {
+    const email = get(data, 'email');
+    if (!email) return;
+    const builder = await this.model.createQueryBuilder().where('"email" =:value', { value: email });
+    // 如果有id,则视为修改,查非自己的email是否存在
+    const id = get(data, 'id');
+    if (id) builder.andWhere('"id" != :id', { id });
+    const num = await builder.getCount();
+    if (num <= 0) return true;
+    // 邮箱存在
+    throw new ServiceError(ErrorCode.EMAIL_IS_EXISTS);
+  }
 
   async checkPhone(data) {
     const phone = get(data, 'phone');
     if (!phone) return;
-    const num = await this.model.createQueryBuilder().where('"phone" =:value', { value: phone }).getCount();
+    const builder = await this.model.createQueryBuilder().where('"phone" =:value', { value: phone });
+    // 如果有id,则视为修改,查非自己的phone是否存在
+    const id = get(data, 'id');
+    if (id) builder.andWhere('"id" != :id', { id });
+    const num = await builder.getCount();
     if (num <= 0) return true;
     // 手机号存在
     throw new ServiceError(ErrorCode.PHONE_IS_EXISTS);