最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

NestJS 集成圖片驗(yàn)證碼服務(wù)的實(shí)現(xiàn)

 更新時(shí)間:2026年03月27日 08:34:02   作者:當(dāng)時(shí)只道尋常  
圖形驗(yàn)證碼服務(wù)基于svg-captcha生成數(shù)學(xué)表達(dá)式驗(yàn)證碼,通過(guò)Redis緩存答案,返回base64格式圖片供前端渲染, 本文就來(lái)詳細(xì)的介紹了NestJS 集成圖片驗(yàn)證碼服務(wù)的實(shí)現(xiàn),感興趣的可以了解一下

所需依賴

首先,我們需要安裝驗(yàn)證碼包svg-captcha來(lái)生成圖片驗(yàn)證碼

pnpm i svg-captcha

代碼配置

import { randomUUID } from 'crypto'
import svgCaptcha from 'svg-captcha'
import { Injectable } from '@nestjs/common'
import { RedisService } from './redis.service'
import { BusinessException, RedisConstant } from '@/common'
@Injectable()
export class CaptchaService {
 /** 驗(yàn)證碼過(guò)期時(shí)間(秒) */
 private readonly CAPTCHA_EXPIRES_IN = 60
 constructor(private readonly redisService: RedisService) {}
 /**
  * 生成圖形驗(yàn)證碼
  * @description 生成數(shù)學(xué)表達(dá)式驗(yàn)證碼并緩存答案,返回 base64 格式圖片
  * @returns 驗(yàn)證碼唯一標(biāo)識(shí)和 base64 圖片數(shù)據(jù)
  */
 public async create(): Promise<{ uuid: string; captcha: string }> {
   // 1. 生成唯一標(biāo)識(shí),用于關(guān)聯(lián)驗(yàn)證碼答案與圖片
   const uuid = randomUUID()
   // 2. 創(chuàng)建數(shù)學(xué)表達(dá)式驗(yàn)證碼
   const { data, text } = svgCaptcha.createMathExpr({ background: '#C0C8BE', noise: 4 })
   // 3. 轉(zhuǎn)換為 base64 格式,前端可直接通過(guò) img 標(biāo)簽渲染
   const captcha = this.convertToBase64(data)
   // 4. 緩存驗(yàn)證碼答案,有效期 60 秒
   const key = this.getCacheKey(uuid)
   await this.redisService.set(key, text, 'EX', this.CAPTCHA_EXPIRES_IN)
   // 5. 返回驗(yàn)證碼數(shù)據(jù)
   return { uuid, captcha }
 }
 /**
  * 校驗(yàn)圖形驗(yàn)證碼
  * @description 驗(yàn)證用戶輸入的驗(yàn)證碼是否正確,校驗(yàn)通過(guò)后立即刪除緩存防止重復(fù)使用
  * @param uuid - 驗(yàn)證碼唯一標(biāo)識(shí)
  * @param captcha - 用戶輸入的驗(yàn)證碼
  * @throws {BusinessException} 驗(yàn)證碼錯(cuò)誤時(shí)拋出異常
  */
 public async validate(uuid: string, captcha: string): Promise<boolean> {
   // 1. 參數(shù)校驗(yàn)
   if (!uuid || !captcha) throw new BusinessException('驗(yàn)證碼校驗(yàn)參數(shù)不完整')
   // 2. 獲取緩存的驗(yàn)證碼答案
   const key = this.getCacheKey(uuid)
   const cachedValue = await this.redisService.get(key)
   // 3. 驗(yàn)證碼已過(guò)期或不存在
   if (!cachedValue) throw new BusinessException('驗(yàn)證碼已過(guò)期,請(qǐng)刷新后重試')
   // 4. 驗(yàn)證碼匹配校驗(yàn)(不區(qū)分大小寫)
   const isValid = cachedValue.toLowerCase() === captcha.toLowerCase()
   if (!isValid) throw new BusinessException('驗(yàn)證碼錯(cuò)誤,請(qǐng)刷新后重試')
   // 5. 校驗(yàn)通過(guò),立即刪除緩存,防止重復(fù)使用
   await this.redisService.del(key)
   // 6. 返回校驗(yàn)成功標(biāo)識(shí)
   return true
 }
 /**
  * 獲取 Redis 緩存 Key
  * @param uuid - 驗(yàn)證碼唯一標(biāo)識(shí)
  */
 private getCacheKey(uuid: string): string {
   return `${RedisConstant.CAPTCHA_KEY}:${uuid}`
 }
 /**
  * 將 SVG 字符串轉(zhuǎn)換為 base64 格式
  * @param svgData - SVG 原始數(shù)據(jù)
  * @returns base64 格式的圖片數(shù)據(jù)
  */
 private convertToBase64(svgData: string): string {
   const base64 = Buffer.from(svgData).toString('base64')
   return `data:image/svg+xml;base64,${base64}`
 }
}

到此這篇關(guān)于NestJS 集成圖片驗(yàn)證碼服務(wù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)NestJS 圖片驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

郓城县| 象山县| 武宣县| 虞城县| 同仁县| 古浪县| 阿鲁科尔沁旗| 林州市| 拜泉县| 鄂尔多斯市| 陕西省| 永胜县| 祁连县| 商洛市| 图木舒克市| 清河县| 精河县| 天镇县| 永春县| 搜索| 双柏县| 大邑县| 恩平市| 南皮县| 雅江县| 闵行区| 榆林市| 海林市| 巫溪县| 静安区| 陵水| 郎溪县| 大宁县| 东山县| 永昌县| 侯马市| 兴国县| 榆社县| 金门县| 陆丰市| 历史|