Compare commits
No commits in common. 'master' and 'main' have entirely different histories.
@ -1,85 +0,0 @@
|
||||
const Product = require('../models/Product');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
// 获取商品列表
|
||||
const getProductList = async (req, res) => {
|
||||
try {
|
||||
// 从查询参数获取分页信息
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const limit = parseInt(req.query.limit) || 10;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// 查询条件
|
||||
const whereClause = {};
|
||||
if (req.query.category) {
|
||||
whereClause.category = req.query.category;
|
||||
}
|
||||
if (req.query.status !== undefined) {
|
||||
whereClause.status = req.query.status;
|
||||
}
|
||||
if (req.query.name) {
|
||||
whereClause.name = { [Op.like]: `%${req.query.name}%` };
|
||||
}
|
||||
|
||||
// 查询商品列表
|
||||
const { count, rows } = await Product.findAndCountAll({
|
||||
where: whereClause,
|
||||
limit: limit,
|
||||
offset: offset,
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
code: 200,
|
||||
msg: '获取商品列表成功',
|
||||
data: {
|
||||
products: rows,
|
||||
pagination: {
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(count / limit),
|
||||
totalItems: count,
|
||||
itemsPerPage: limit
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
return res.status(500).json({
|
||||
code: 500,
|
||||
msg: '获取商品列表失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 获取单个商品详情
|
||||
const getProductDetail = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const product = await Product.findByPk(id);
|
||||
|
||||
if (!product) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
msg: '商品不存在'
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
code: 200,
|
||||
msg: '获取商品详情成功',
|
||||
data: product
|
||||
});
|
||||
} catch (error) {
|
||||
return res.status(500).json({
|
||||
code: 500,
|
||||
msg: '获取商品详情失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getProductList,
|
||||
getProductDetail
|
||||
};
|
||||
@ -1,26 +0,0 @@
|
||||
const { verifyToken } = require('../utils/jwt');
|
||||
|
||||
const authenticateToken = (req, res, next) => {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
code: 401,
|
||||
msg: '访问被拒绝,缺少令牌'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = verifyToken(token);
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(403).json({
|
||||
code: 403,
|
||||
msg: '令牌无效或已过期'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { authenticateToken };
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,26 +0,0 @@
|
||||
{
|
||||
"name": "nodejs",
|
||||
"version": "1.0.0",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js",
|
||||
"dev": "nodemon server.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "YuLeYuan",
|
||||
"license": "ISC",
|
||||
"description": "Node.js + Express + MySQL 全栈后端(含登录功能)",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.6",
|
||||
"dotenv": "^17.2.4",
|
||||
"express": "^5.2.1",
|
||||
"express-validator": "^7.3.1",
|
||||
"jsonwebtoken": "^9.0.3",
|
||||
"mysql2": "^3.16.3",
|
||||
"sequelize": "^6.37.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.1.11"
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { authenticateToken } = require('../middlewares/auth');
|
||||
const { getProductList, getProductDetail } = require('../controllers/productController');
|
||||
|
||||
// 获取商品列表(需要认证)
|
||||
router.get('/products', authenticateToken, getProductList);
|
||||
|
||||
// 获取单个商品详情(需要认证)
|
||||
router.get('/products/:id', authenticateToken, getProductDetail);
|
||||
|
||||
module.exports = router;
|
||||
|
Before Width: | Height: | Size: 1.1 MiB |
@ -1,21 +0,0 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
/**
|
||||
* 生成JWT Token
|
||||
* @param {Object} payload - 要存储在token中的数据
|
||||
* @returns {String} 生成的token字符串
|
||||
*/
|
||||
const generateToken = (payload) => {
|
||||
return jwt.sign(payload, process.env.JWT_SECRET);
|
||||
};
|
||||
|
||||
/**
|
||||
* 验证JWT Token
|
||||
* @param {String} token - 要验证的token字符串
|
||||
* @returns {Object} 解码后的payload对象
|
||||
*/
|
||||
const verifyToken = (token) => {
|
||||
return jwt.verify(token, process.env.JWT_SECRET);
|
||||
};
|
||||
|
||||
module.exports = { generateToken, verifyToken };
|
||||
Loading…
Reference in new issue