From bf6a9bff1ffe09fb45febe48128b10408e91a6b7 Mon Sep 17 00:00:00 2001 From: P-Jhao <113034340+P-Jhao@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:06:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=BF=90=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 12 ++++++++++++ dao/db.js | 34 ++++++++++++++++++++++++++++++++++ dao/expressDao.js | 20 ++++++++++++++++++++ dao/model/expressModel.js | 37 +++++++++++++++++++++++++++++++++++++ package-lock.json | 23 +++++++++++++++++++++++ package.json | 1 + routes/express.js | 25 +++++++++++++++++++++++-- routes/user.js | 1 + services/expressService.js | 26 ++++++++++++++++++++++++++ utils/tools.js | 12 ++++++++++++ 10 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 dao/expressDao.js create mode 100644 dao/model/expressModel.js create mode 100644 services/expressService.js diff --git a/app.js b/app.js index 99e535d..0534bc9 100644 --- a/app.js +++ b/app.js @@ -3,17 +3,28 @@ var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); +const cors = require('cors'); //优先读取环境变量 require("dotenv").config() + //引入数据库连接 require("./dao/db") //引入路由 const userRouter = require('./routes/user'); +const expressRouter = require("./routes/express") var app = express(); +//配置cors +const corsOptions = { + allowedHeaders: ['Content-Type', 'Authorization'], + exposedHeaders: ['Authentication', 'X-Custom-Header'], + credentials: true, +} +//为所有源跨域 +app.use(cors(corsOptions)) //使用各种中间件 app.use(logger('dev')); app.use(express.json()); @@ -25,6 +36,7 @@ app.use(express.static(path.join(__dirname, 'public'))); //使用路由中间件 app.use('/user', userRouter); +app.use("/express", expressRouter) // catch 404 and forward to error handler app.use(function (req, res, next) { diff --git a/dao/db.js b/dao/db.js index e0892b8..77b70d2 100644 --- a/dao/db.js +++ b/dao/db.js @@ -1,6 +1,10 @@ const sequelize = require("./dbConnect") const userModel = require("./model/userModel") +const expressModel = require("./model/expressModel") const md5 = require("md5") +const { + getRandomExpressId +} = require("../utils/tools") //将数据模型和表进行同步 async function test() { await sequelize.sync({ @@ -19,6 +23,36 @@ async function test() { console.log("用户数据初始化完毕") } + const expressCount = await expressModel.count() + if (!expressCount) { + expressModel.bulkCreate([{ + expressId: getRandomExpressId(), + senderCity: "深圳市", + senderName: "冰凉凉1", + receiverCity: "福州市", + receiverName: "叶丽丽", + isSigned: 1, + signedTime: "2024-10-24 11:58" + }, { + expressId: getRandomExpressId(), + senderCity: "深圳市", + senderName: "冰凉凉2", + receiverCity: "福州市", + receiverName: "叶丽丽", + isSigned: 1, + signedTime: "2024-10-24 11:58" + }, { + expressId: getRandomExpressId(), + senderCity: "深圳市", + senderName: "冰凉凉3", + receiverCity: "福州市", + receiverName: "叶丽丽", + isSigned: 1, + signedTime: "2024-10-24 11:58" + }]) + console.log("express初始化完成") + } + console.log("数据库数据准备完成") } test() \ No newline at end of file diff --git a/dao/expressDao.js b/dao/expressDao.js new file mode 100644 index 0000000..be01a19 --- /dev/null +++ b/dao/expressDao.js @@ -0,0 +1,20 @@ +const { + handleArrayDaoData +} = require("../utils/tools") +const expressModel = require("./model/expressModel") +module.exports.getExpressDao = async function () { + return handleArrayDaoData(await expressModel.findAll()) +} +module.exports.addExpressDao = async function (expData) { + await expressModel.create(expData) + return handleArrayDaoData(await expressModel.findAll()) +} + +module.exports.updateExpressDao = async function (expData) { + await expressModel.update(expData, { + where: { + id: expData.id + } + }) + return handleArrayDaoData(await expressModel.findAll()) +} \ No newline at end of file diff --git a/dao/model/expressModel.js b/dao/model/expressModel.js new file mode 100644 index 0000000..6ec2d36 --- /dev/null +++ b/dao/model/expressModel.js @@ -0,0 +1,37 @@ +const sequelize = require("../dbConnect") +const { + DataTypes +} = require("sequelize") + +module.exports = sequelize.define("express", { + expressId: { + type: DataTypes.STRING, + allowNull: false + }, + senderName: { + type: DataTypes.STRING, + allowNull: false + }, + senderCity: { + type: DataTypes.STRING, + allowNull: false + }, + receiverName: { + type: DataTypes.STRING, + allowNull: false + }, + receiverCity: { + type: DataTypes.STRING, + allowNull: false + }, + isSigned: { + type: DataTypes.INTEGER, + allowNull: false + }, + signedTime: DataTypes.STRING + +}, { + freezeTableName: true, + createdAt: false, + updatedAt: false, +}) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5b57fca..3bafdd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "dependencies": { "cookie-parser": "~1.4.4", + "cors": "^2.8.5", "debug": "~2.6.9", "dotenv": "^16.4.5", "express": "~4.16.1", @@ -317,6 +318,19 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "license": "MIT" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmmirror.com/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/crypt": { "version": "0.0.2", "resolved": "https://registry.npmmirror.com/crypt/-/crypt-0.0.2.tgz", @@ -985,6 +999,15 @@ "node": ">= 0.6" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmmirror.com/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/on-finished": { "version": "2.3.0", "resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.3.0.tgz", diff --git a/package.json b/package.json index 0800e4d..2e76415 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "cookie-parser": "~1.4.4", + "cors": "^2.8.5", "debug": "~2.6.9", "dotenv": "^16.4.5", "express": "~4.16.1", diff --git a/routes/express.js b/routes/express.js index 978506c..13b782f 100644 --- a/routes/express.js +++ b/routes/express.js @@ -1,6 +1,27 @@ const express = require("express") -const router = express.router +const router = express.Router() +const { + getExpressServices, + addExpressServices, + updateExpressServices +} = require("../services/expressService") +const { + formatResponse +} = require("../utils/tools") -router. +router.get("/", async function (req, res, next) { + const data = await getExpressServices() + res.send(formatResponse(0, "", data)) +}) + +router.post("/", async function (req, res, next) { + const data = await addExpressServices(req.body) + res.send(formatResponse(0, "", data)) +}) + +router.put("/", async function (req, res, next) { + const data = await updateExpressServices(req.body) + res.send(formatResponse(0, "", data)) +}) module.exports = router \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index a465969..3651035 100644 --- a/routes/user.js +++ b/routes/user.js @@ -15,6 +15,7 @@ router.post('/login', async function (req, res, next) { const result = await loginService(req.body) if (result.token) { //有token,登陆成功 + console.log(result.data) res.setHeader("authentication", result.token) res.send(formatResponse(0, "", result.data)) } else { diff --git a/services/expressService.js b/services/expressService.js new file mode 100644 index 0000000..70db178 --- /dev/null +++ b/services/expressService.js @@ -0,0 +1,26 @@ +const { + getExpressDao, + addExpressDao, + updateExpressDao +} = require("../dao/expressDao") +const { + getRandomExpressId +} = require("../utils/tools") +module.exports.getExpressServices = async function () { + return await getExpressDao() +} + +module.exports.addExpressServices = async function (expData) { + //获取的对象需要补充"expressId", "isSigned" + const data = { + expressId: getRandomExpressId(), + isSigned: 0, + ...expData, + } + return await addExpressDao(data) +} + +module.exports.updateExpressServices = async function (expData) { + //这里需要提供id + return await updateExpressDao(expData) +} \ No newline at end of file diff --git a/utils/tools.js b/utils/tools.js index 05875fe..68d310e 100644 --- a/utils/tools.js +++ b/utils/tools.js @@ -18,4 +18,16 @@ module.exports.getJwtToken = function (payload) { return jwt.sign(payload, md5(process.env.JWT_SECRET), { expiresIn: 60 * 60 * 24 * loginPeriod }) +} + +module.exports.getRandomExpressId = function () { + return "SF" + Math.floor(Math.random() * 1000000000000) +} + +module.exports.handleArrayDaoData = function (arr) { + const result = [] + for (const item of arr) { + result.push(item.dataValues) + } + return result } \ No newline at end of file