| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:15 |
| 4 | * @LastEditTime: 2025-02-06 13:44:23 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { AppHttpException } from '../filters/http-exception.filter'; |
| 10 | import { ErrHttpBack } from '../filters/http-exception.back-code'; |
| 11 | import { RedisService } from '../lib/redis/redis.service'; |
| 12 | import { AlicloudSmsService } from '../lib/sms/alicloud-sms.service'; |
| 13 | import { getRandomString } from '../util'; |
| 14 | |
| 15 | export enum LoginTypeCacheKey { |
| 16 | Password = 'phone_register_code', |
| 17 | Code = 'phone_login_code', |
| 18 | PhoneAuth = 'phone_login_auth_token', // 手机号一键登录 |
| 19 | } |
| 20 | @Injectable() |
| 21 | export class LoginService { |
| 22 | constructor( |
| 23 | private readonly redisService: RedisService, |
| 24 | private readonly alicloudSmsService: AlicloudSmsService, |
| 25 | ) {} |
| 26 | |
| 27 | /** |
| 28 | * 发送手机号注册的验证码 |
| 29 | * @param phone |
| 30 | */ |
| 31 | async postPhoneRegisterCode(phone: string) { |
| 32 | const cacheKey = `${LoginTypeCacheKey.Password}:${phone}`; |
| 33 | let code = await this.redisService.get(cacheKey); |
| 34 | if (code) throw new AppHttpException(ErrHttpBack.err_user_code_had); |
| 35 | |
| 36 | code = getRandomString(6, true); |
| 37 | const res = await this.alicloudSmsService.sendLoginSms(phone, code); |
| 38 | |
| 39 | if (process.env.NODE_ENV === 'production') { |
| 40 | if (!res) throw new AppHttpException(ErrHttpBack.err_user_code_send_fail); |
| 41 | } |
| 42 | |
| 43 | this.redisService.setKey(cacheKey, code, 60 * 5); |
| 44 | console.log('发送短信成功', code); |
| 45 | |
| 46 | return process.env.NODE_ENV === 'production' ? res : code; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * 发送手机号登录的验证码 |
| 51 | * @param phone |
| 52 | */ |
| 53 | async postPhoneLoginCode(phone: string) { |
| 54 | const cacheKey = `${LoginTypeCacheKey.Code}:${phone}`; |
| 55 | let code = await this.redisService.get(cacheKey); |
| 56 | if (code) throw new AppHttpException(ErrHttpBack.err_user_code_had); |
| 57 | |
| 58 | code = getRandomString(6, true); |
| 59 | |
| 60 | let res = false; |
| 61 | if (process.env.NODE_ENV === 'production') { |
| 62 | res = await this.alicloudSmsService.sendLoginSms(phone, code); |
| 63 | |
| 64 | if (!res) throw new AppHttpException(ErrHttpBack.err_user_code_send_fail); |
| 65 | } |
| 66 | |
| 67 | this.redisService.setKey(cacheKey, code, 60 * 5); |
| 68 | console.log('发送短信成功', code); |
| 69 | |
| 70 | return process.env.NODE_ENV === 'production' ? res : code; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * 发送手机号注册的验证码 |
| 75 | * @param phone |
| 76 | * @param code |
| 77 | */ |
| 78 | async verifyPhoneCode( |
| 79 | phone: string, |
| 80 | code: string, |
| 81 | key: LoginTypeCacheKey = LoginTypeCacheKey.Password, |
| 82 | ): Promise<boolean> { |
| 83 | const cacheKey = `${key}:${phone}`; |
| 84 | const oldCode = await this.redisService.get(cacheKey); |
| 85 | if (!oldCode) return false; |
| 86 | |
| 87 | if (code === oldCode) { |
| 88 | this.redisService.del(cacheKey); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // 微信用户信息 |
| 96 | async setWxUserInfo(userInfo: any) { |
| 97 | console.log('userInfo', userInfo); |
| 98 | } |
| 99 | // 支付宝用户信息 |
| 100 | async setZfbUserInfo(userInfo: any) { |
| 101 | console.log('userInfo', userInfo); |
| 102 | } |
| 103 | } |
| 104 |