修改商城逻辑
This commit is contained in:
200
routes/auth.js
200
routes/auth.js
@@ -6,9 +6,117 @@ const { getDB } = require('../database');
|
||||
const router = express.Router();
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';
|
||||
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* 需要提供有效的激活码才能注册
|
||||
* @swagger
|
||||
* tags:
|
||||
* name: Authentication
|
||||
* description: 用户认证API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* components:
|
||||
* schemas:
|
||||
* LoginCredentials:
|
||||
* type: object
|
||||
* required:
|
||||
* - username
|
||||
* - password
|
||||
* properties:
|
||||
* username:
|
||||
* type: string
|
||||
* description: 用户名或手机号
|
||||
* password:
|
||||
* type: string
|
||||
* description: 密码
|
||||
* RegisterRequest:
|
||||
* type: object
|
||||
* required:
|
||||
* - username
|
||||
* - phone
|
||||
* - password
|
||||
* - registrationCode
|
||||
* - city
|
||||
* - district_id
|
||||
* - captchaId
|
||||
* - captchaText
|
||||
* - smsCode
|
||||
* properties:
|
||||
* username:
|
||||
* type: string
|
||||
* description: 用户名
|
||||
* phone:
|
||||
* type: string
|
||||
* description: 手机号
|
||||
* password:
|
||||
* type: string
|
||||
* description: 密码
|
||||
* registrationCode:
|
||||
* type: string
|
||||
* description: 注册激活码
|
||||
* city:
|
||||
* type: string
|
||||
* description: 城市
|
||||
* district_id:
|
||||
* type: string
|
||||
* description: 区域ID
|
||||
* captchaId:
|
||||
* type: string
|
||||
* description: 图形验证码ID
|
||||
* captchaText:
|
||||
* type: string
|
||||
* description: 图形验证码文本
|
||||
* smsCode:
|
||||
* type: string
|
||||
* description: 短信验证码
|
||||
* role:
|
||||
* type: string
|
||||
* description: 用户角色
|
||||
* default: user
|
||||
*/
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /auth/register:
|
||||
* post:
|
||||
* summary: 用户注册
|
||||
* description: 需要提供有效的激活码才能注册
|
||||
* tags: [Authentication]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/RegisterRequest'
|
||||
* responses:
|
||||
* 201:
|
||||
* description: 用户注册成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* token:
|
||||
* type: string
|
||||
* description: JWT认证令牌
|
||||
* user:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* username:
|
||||
* type: string
|
||||
* role:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/register', async (req, res) => {
|
||||
try {
|
||||
@@ -175,7 +283,55 @@ router.post('/register', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 用户登录
|
||||
/**
|
||||
* @swagger
|
||||
* /auth/login:
|
||||
* post:
|
||||
* summary: 用户登录
|
||||
* tags: [Authentication]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/LoginCredentials'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 登录成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* token:
|
||||
* type: string
|
||||
* description: JWT认证令牌
|
||||
* user:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* username:
|
||||
* type: string
|
||||
* role:
|
||||
* type: string
|
||||
* avatar:
|
||||
* type: string
|
||||
* points:
|
||||
* type: integer
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* 401:
|
||||
* description: 用户名或密码错误
|
||||
* 403:
|
||||
* description: 账户审核未通过
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/login', async (req, res) => {
|
||||
try {
|
||||
const db = getDB();
|
||||
@@ -185,9 +341,41 @@ router.post('/login', async (req, res) => {
|
||||
return res.status(400).json({ success: false, message: '用户名和密码不能为空' });
|
||||
}
|
||||
|
||||
// if (!captchaId || !captchaText) {
|
||||
// return res.status(400).json({ success: false, message: '验证码不能为空' });
|
||||
// }
|
||||
if (!captchaId || !captchaText) {
|
||||
return res.status(400).json({ success: false, message: '验证码不能为空' });
|
||||
}
|
||||
// 获取存储的验证码
|
||||
const storedCaptcha = global.captchaStore.get(captchaId);
|
||||
console.log(storedCaptcha);
|
||||
|
||||
if (!storedCaptcha) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '验证码不存在或已过期'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查是否过期
|
||||
if (Date.now() > storedCaptcha.expires) {
|
||||
global.captchaStore.delete(captchaId);
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '验证码已过期'
|
||||
});
|
||||
}
|
||||
|
||||
// 验证验证码(不区分大小写)
|
||||
const isValid = storedCaptcha.text === captchaText.toLowerCase();
|
||||
|
||||
// 删除已验证的验证码
|
||||
global.captchaStore.delete(captchaId);
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '验证码错误'
|
||||
});
|
||||
}
|
||||
|
||||
// 注意:验证码已在前端通过 /captcha/verify 接口验证过,这里不再重复验证
|
||||
|
||||
|
||||
Reference in New Issue
Block a user