Browse Source

注册验证码

lrf 1 day ago
parent
commit
34a3e04fe7

+ 4 - 0
src/config/config.default.ts

@@ -21,8 +21,12 @@ export default {
     path: 'C:\\temp\\cxyy\\',
   },
   showDBSub: false,
+  /**登录短信前缀 */
   loginSmsSign: 'LoginSmsSign',
+  /**忘记密码短信前缀 */
   pwdSmsSign: 'PasswordSmsSign',
+  /**注册短信前缀 */
+  regSmsSign: 'RegisterSmsSign',
   aliyunSms: {
     template: 'SMS_275370127', //模板code
     sign: '吉林省华欣数字科技股份', //签名

+ 15 - 0
src/controller/login.controller.ts

@@ -28,6 +28,21 @@ export class LoginController {
   @Inject()
   ctx: Context;
 
+  @Post('/reg/check', { routerName: '用户注册-验证码验证' })
+  async regCodeCheck(@Body('phone') phone: string, @Body('checkCode') code: string) {
+    if (!phone) throw new ServiceError(ErrorCode.PWD_NO_PHONE);
+    if (!code) throw new ServiceError(ErrorCode.PWD_NO_CODE);
+    await this.loginService.checkRegCode(phone, code);
+  }
+
+  @Post('/reg/code', { routerName: '用户注册-获取验证码' })
+  async getRegCode(@Body('phone') phone: string) {
+    if (!phone) throw new ServiceError(ErrorCode.PWD_NO_PHONE); // 缺少手机号
+    const res = await this.userSerivce.checkPhone({ phone });
+    if (!res) throw new ServiceError(ErrorCode.PHONE_IS_EXISTS);
+    await this.loginService.sendRegCode(phone)
+  }
+
   @Post('/pwd/reset', { routerName: '用户忘记密码-重置密码' })
   async pwdReset(@Body('id') id: number, @Body('password') password: string) {
     if (!id || !password) throw new ServiceError(ErrorCode.BODY_ERROR);

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

@@ -30,6 +30,8 @@ export enum ErrorCode {
   PWD_NO_CODE = 'PWD_NO_CODE',
   PWD_VALICODE_EXPIRES = 'PWD_VALICODE_EXPIRES',
   PWD_VALICODE_ERROR = 'PWD_VALICODE_ERROR',
+  REG_VALICODE_EXPIRES = 'REG_VALICODE_EXPIRES',
+  REG_VALICODE_ERROR = 'REG_VALICODE_ERROR',
 
   // 参数
   ID_NOT_FOUND = 'ID_NOT_FOUND',

+ 29 - 1
src/service/login.service.ts

@@ -32,6 +32,8 @@ export class LoginService {
   loginSmsSign;
   @Config('pwdSmsSign')
   pwdSmsSign;
+  @Config('regSmsSign')
+  regSmsSign;
 
   @Inject()
   redisService: RedisService;
@@ -46,6 +48,32 @@ export class LoginService {
   @InjectEntityModel(User)
   userModel: Repository<User>;
 
+  /**
+   * 注册验证 验证码
+   * @param phone 电话号码
+   * @param code 验证码
+   * @returns 
+   */
+  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); // 验证码错误
+    await this.redisService.del(redisKey);
+    return true;
+  }
+
+  /**
+   * 发送注册验证码
+   * @param phone 电话号码
+   */
+  async sendRegCode(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);
+  }
+
   /**
    * 验证:找回密码的验证是否正确
    * @param type 验证方式
@@ -71,7 +99,7 @@ export class LoginService {
     const code = random(1000, 999999);
     await this.redisService.set(redisKey, code, 'EX', this.timeLimit);
     if (type === 'email') {
-      // TODO: 发邮箱
+      // 发邮箱
       await this.emailService.sendPwdCode(to, code);
     } else if (type === 'phone') {
       // 发短信

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

@@ -23,7 +23,7 @@ export class UserService extends BaseServiceV2 {
     const phone = get(data, 'phone');
     if (!phone) return;
     const num = await this.model.createQueryBuilder().where('"phone" =:value', { value: phone }).getCount();
-    if (num <= 0) return;
+    if (num <= 0) return true;
     // 手机号存在
     throw new ServiceError(ErrorCode.PHONE_IS_EXISTS);
   }

+ 4 - 1
src/service/thirdParty/aliyunSms.service.ts

@@ -6,6 +6,8 @@ import Util, * as $Util from '@alicloud/tea-util';
 export class AliyunSmsService {
   @Config('aliyunSms')
   aliyunSms;
+  @Config('showDBSub')
+  showDBSub: any;
 
   client: Dysmsapi20170525;
   @Init()
@@ -25,7 +27,8 @@ export class AliyunSmsService {
       templateParam: JSON.stringify({ code }),
     });
     try {
-      await this.client.sendSms(smsRequest);
+      const res = await this.client.sendSms(smsRequest);
+      if(this.showDBSub) console.log(res)
     } catch (error) {
       console.log(error);
     }