Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -1,132 +1,37 @@
|
|||||||
# ---> Node
|
# 依赖文件夹
|
||||||
# Logs
|
|
||||||
logs
|
|
||||||
*.log
|
|
||||||
npm-debug.log*
|
|
||||||
yarn-debug.log*
|
|
||||||
yarn-error.log*
|
|
||||||
lerna-debug.log*
|
|
||||||
.pnpm-debug.log*
|
|
||||||
|
|
||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
||||||
|
|
||||||
# Runtime data
|
|
||||||
pids
|
|
||||||
*.pid
|
|
||||||
*.seed
|
|
||||||
*.pid.lock
|
|
||||||
|
|
||||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
||||||
lib-cov
|
|
||||||
|
|
||||||
# Coverage directory used by tools like istanbul
|
|
||||||
coverage
|
|
||||||
*.lcov
|
|
||||||
|
|
||||||
# nyc test coverage
|
|
||||||
.nyc_output
|
|
||||||
|
|
||||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
||||||
.grunt
|
|
||||||
|
|
||||||
# Bower dependency directory (https://bower.io/)
|
|
||||||
bower_components
|
|
||||||
|
|
||||||
# node-waf configuration
|
|
||||||
.lock-wscript
|
|
||||||
|
|
||||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
||||||
build/Release
|
|
||||||
|
|
||||||
# Dependency directories
|
|
||||||
node_modules/
|
node_modules/
|
||||||
jspm_packages/
|
|
||||||
|
|
||||||
# Snowpack dependency directory (https://snowpack.dev/)
|
|
||||||
web_modules/
|
|
||||||
|
|
||||||
# TypeScript cache
|
|
||||||
*.tsbuildinfo
|
|
||||||
|
|
||||||
# Optional npm cache directory
|
|
||||||
.npm
|
|
||||||
|
|
||||||
# Optional eslint cache
|
|
||||||
.eslintcache
|
|
||||||
|
|
||||||
# Optional stylelint cache
|
|
||||||
.stylelintcache
|
|
||||||
|
|
||||||
# Microbundle cache
|
# 环境变量文件
|
||||||
.rpt2_cache/
|
|
||||||
.rts2_cache_cjs/
|
|
||||||
.rts2_cache_es/
|
|
||||||
.rts2_cache_umd/
|
|
||||||
|
|
||||||
# Optional REPL history
|
|
||||||
.node_repl_history
|
|
||||||
|
|
||||||
# Output of 'npm pack'
|
|
||||||
*.tgz
|
|
||||||
|
|
||||||
# Yarn Integrity file
|
|
||||||
.yarn-integrity
|
|
||||||
|
|
||||||
# dotenv environment variable files
|
|
||||||
.env
|
.env
|
||||||
|
.env.local
|
||||||
.env.development.local
|
.env.development.local
|
||||||
.env.test.local
|
.env.test.local
|
||||||
.env.production.local
|
.env.production.local
|
||||||
.env.local
|
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
|
||||||
.cache
|
|
||||||
.parcel-cache
|
|
||||||
|
|
||||||
# Next.js build output
|
|
||||||
.next
|
|
||||||
out
|
|
||||||
|
|
||||||
# Nuxt.js build / generate output
|
|
||||||
.nuxt
|
|
||||||
dist
|
|
||||||
|
|
||||||
# Gatsby files
|
# 日志文件
|
||||||
.cache/
|
logs/
|
||||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
*.log
|
||||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
npm-debug.log*
|
||||||
# public
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
# vuepress build output
|
|
||||||
.vuepress/dist
|
|
||||||
|
|
||||||
# vuepress v2.x temp and cache directory
|
|
||||||
.temp
|
|
||||||
.cache
|
|
||||||
|
|
||||||
# Docusaurus cache and generated files
|
|
||||||
.docusaurus
|
|
||||||
|
|
||||||
# Serverless directories
|
|
||||||
.serverless/
|
|
||||||
|
|
||||||
# FuseBox cache
|
|
||||||
.fusebox/
|
|
||||||
|
|
||||||
# DynamoDB Local files
|
# 编辑器配置
|
||||||
.dynamodb/
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
|
||||||
# TernJS port file
|
# 操作系统临时文件
|
||||||
.tern-port
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
# Stores VSCode versions used for testing VSCode extensions
|
# 构建输出
|
||||||
.vscode-test
|
dist/
|
||||||
|
build/
|
||||||
|
|
||||||
# yarn v2
|
# 测试覆盖率
|
||||||
.yarn/cache
|
coverage/
|
||||||
.yarn/unplugged
|
.nyc_output/
|
||||||
.yarn/build-state.yml
|
|
||||||
.yarn/install-state.gz
|
|
||||||
.pnp.*
|
|
||||||
|
|
||||||
|
# 上传的图片文件夹
|
||||||
|
uploads/images/
|
||||||
|
|||||||
@ -0,0 +1,85 @@
|
|||||||
|
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
|
||||||
|
};
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
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
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
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;
|
||||||
|
After Width: | Height: | Size: 1.1 MiB |
@ -0,0 +1,21 @@
|
|||||||
|
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