修改商城逻辑
This commit is contained in:
@@ -2,8 +2,15 @@ const express = require('express');
|
||||
const crypto = require('crypto');
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* tags:
|
||||
* name: Captcha
|
||||
* description: 验证码API
|
||||
*/
|
||||
|
||||
// 内存存储验证码(生产环境建议使用Redis)
|
||||
const captchaStore = new Map();
|
||||
|
||||
|
||||
/**
|
||||
* 生成随机验证码字符串
|
||||
@@ -101,7 +108,33 @@ function generateCaptchaSVG(text) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码接口
|
||||
* @swagger
|
||||
* /captcha/generate:
|
||||
* get:
|
||||
* summary: 生成图形验证码
|
||||
* tags: [Captcha]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功生成验证码
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: true
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* captchaId:
|
||||
* type: string
|
||||
* description: 验证码唯一ID
|
||||
* image:
|
||||
* type: string
|
||||
* description: Base64编码的SVG验证码图片
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.get('/generate', (req, res) => {
|
||||
try {
|
||||
@@ -112,7 +145,7 @@ router.get('/generate', (req, res) => {
|
||||
const captchaId = crypto.randomUUID();
|
||||
|
||||
// 存储验证码(5分钟过期)
|
||||
captchaStore.set(captchaId, {
|
||||
global.captchaStore.set(captchaId, {
|
||||
text: captchaText.toLowerCase(), // 存储小写用于比较
|
||||
expires: Date.now() + 5 * 60 * 1000 // 5分钟过期
|
||||
});
|
||||
@@ -137,9 +170,56 @@ router.get('/generate', (req, res) => {
|
||||
});
|
||||
|
||||
/**
|
||||
* 验证验证码接口
|
||||
* @param {string} captchaId 验证码ID
|
||||
* @param {string} captchaText 用户输入的验证码
|
||||
* @swagger
|
||||
* /captcha/verify:
|
||||
* post:
|
||||
* summary: 验证用户输入的验证码
|
||||
* tags: [Captcha]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - captchaId
|
||||
* - captchaText
|
||||
* properties:
|
||||
* captchaId:
|
||||
* type: string
|
||||
* description: 验证码唯一ID
|
||||
* captchaText:
|
||||
* type: string
|
||||
* description: 用户输入的验证码
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 验证码验证成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: true
|
||||
* message:
|
||||
* type: string
|
||||
* example: 验证码验证成功
|
||||
* 400:
|
||||
* description: 验证码错误或已过期
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: false
|
||||
* message:
|
||||
* type: string
|
||||
* example: 验证码错误
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/verify', (req, res) => {
|
||||
try {
|
||||
@@ -153,7 +233,7 @@ router.post('/verify', (req, res) => {
|
||||
}
|
||||
|
||||
// 获取存储的验证码
|
||||
const storedCaptcha = captchaStore.get(captchaId);
|
||||
const storedCaptcha = global.captchaStore.get(captchaId);
|
||||
|
||||
if (!storedCaptcha) {
|
||||
return res.status(400).json({
|
||||
@@ -164,7 +244,7 @@ router.post('/verify', (req, res) => {
|
||||
|
||||
// 检查是否过期
|
||||
if (Date.now() > storedCaptcha.expires) {
|
||||
captchaStore.delete(captchaId);
|
||||
global.captchaStore.delete(captchaId);
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '验证码已过期'
|
||||
@@ -175,7 +255,7 @@ router.post('/verify', (req, res) => {
|
||||
const isValid = storedCaptcha.text === captchaText.toLowerCase();
|
||||
|
||||
// 验证后删除验证码(无论成功失败)
|
||||
captchaStore.delete(captchaId);
|
||||
global.captchaStore.delete(captchaId);
|
||||
|
||||
if (isValid) {
|
||||
res.json({
|
||||
@@ -200,9 +280,9 @@ router.post('/verify', (req, res) => {
|
||||
// 清理过期验证码的定时任务
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [id, captcha] of captchaStore.entries()) {
|
||||
for (const [id, captcha] of global.captchaStore.entries()) {
|
||||
if (now > captcha.expires) {
|
||||
captchaStore.delete(id);
|
||||
global.captchaStore.delete(id);
|
||||
}
|
||||
}
|
||||
}, 60 * 1000); // 每分钟清理一次
|
||||
@@ -210,7 +290,7 @@ setInterval(() => {
|
||||
// 导出验证函数供其他模块使用
|
||||
module.exports = router;
|
||||
module.exports.verifyCaptcha = (captchaId, captchaText) => {
|
||||
const captcha = captchaStore.get(captchaId);
|
||||
const captcha = global.captchaStore.get(captchaId);
|
||||
if (!captcha) {
|
||||
return false; // 验证码不存在或已过期
|
||||
}
|
||||
@@ -220,6 +300,6 @@ module.exports.verifyCaptcha = (captchaId, captchaText) => {
|
||||
}
|
||||
|
||||
// 验证成功后删除验证码(一次性使用)
|
||||
captchaStore.delete(captchaId);
|
||||
global.captchaStore.delete(captchaId);
|
||||
return true;
|
||||
};
|
||||
Reference in New Issue
Block a user