完成uml-backend

master^2
P-Jhao 15 hours ago
parent 1edac893b0
commit a9b1848293

@ -2,3 +2,4 @@ DB_NAME = "uml"
DB_USER = "root" DB_USER = "root"
DB_PASSWORD = "123456" DB_PASSWORD = "123456"
DB_HOST = "localhost" DB_HOST = "localhost"
JWT_SECRET = "PJH"

@ -1,2 +0,0 @@
# backend

@ -3,17 +3,28 @@ var express = require('express');
var path = require('path'); var path = require('path');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var logger = require('morgan'); var logger = require('morgan');
const cors = require('cors');
//优先读取环境变量 //优先读取环境变量
require("dotenv").config() require("dotenv").config()
//引入数据库连接 //引入数据库连接
require("./dao/db") require("./dao/db")
//引入路由 //引入路由
const userRouter = require('./routes/user'); const userRouter = require('./routes/user');
const expressRouter = require("./routes/express")
var app = 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(logger('dev'));
app.use(express.json()); app.use(express.json());
@ -24,7 +35,8 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
//使用路由中间件 //使用路由中间件
app.use('/', userRouter); app.use('/user', userRouter);
app.use("/express", expressRouter)
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function (req, res, next) { app.use(function (req, res, next) {

@ -1,6 +1,10 @@
const sequelize = require("./dbConnect") const sequelize = require("./dbConnect")
const userModel = require("./model/userModel") const userModel = require("./model/userModel")
const expressModel = require("./model/expressModel")
const md5 = require("md5") const md5 = require("md5")
const {
getRandomExpressId
} = require("../utils/tools")
//将数据模型和表进行同步 //将数据模型和表进行同步
async function test() { async function test() {
await sequelize.sync({ await sequelize.sync({
@ -19,6 +23,36 @@ async function test() {
console.log("用户数据初始化完毕") 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("数据库数据准备完成") console.log("数据库数据准备完成")
} }
test() test()

@ -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())
}

@ -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,
})

@ -11,7 +11,11 @@ module.exports = sequelize.define("user", {
loginPwd: { loginPwd: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
} },
sex: DataTypes.STRING,
brithday: DataTypes.STRING,
region: DataTypes.STRING,
number: DataTypes.STRING
}, { }, {
freezeTableName: true, freezeTableName: true,
createdAt: false, createdAt: false,

@ -9,4 +9,20 @@ module.exports.loginDao = async function ({
loginPwd loginPwd
} }
}) })
}
module.exports.updateUserDao = async function (loginInfo) {
return await userModel.update(loginInfo, {
where: {
loginId: loginInfo.loginId,
}
})
}
module.exports.getUserInfoDao = async function (loginId) {
return await userModel.findOne({
where: {
loginId,
}
})
} }

130
package-lock.json generated

@ -9,11 +9,13 @@
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9", "debug": "~2.6.9",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "~1.11.0", "jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",
"md5": "^2.3.0", "md5": "^2.3.0",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"mysql2": "^3.11.4", "mysql2": "^3.11.4",
@ -160,6 +162,12 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
"license": "BSD-3-Clause"
},
"node_modules/bytes": { "node_modules/bytes": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.0.0.tgz", "resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.0.0.tgz",
@ -310,6 +318,19 @@
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
"license": "MIT" "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": { "node_modules/crypt": {
"version": "0.0.2", "version": "0.0.2",
"resolved": "https://registry.npmmirror.com/crypt/-/crypt-0.0.2.tgz", "resolved": "https://registry.npmmirror.com/crypt/-/crypt-0.0.2.tgz",
@ -398,6 +419,15 @@
"integrity": "sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==", "integrity": "sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"resolved": "https://registry.npmmirror.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
}
},
"node_modules/ee-first": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz",
@ -620,6 +650,34 @@
"jade": "bin/jade.js" "jade": "bin/jade.js"
} }
}, },
"node_modules/jsonwebtoken": {
"version": "9.0.2",
"resolved": "https://registry.npmmirror.com/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",
"integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",
"license": "MIT",
"dependencies": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^7.5.4"
},
"engines": {
"node": ">=12",
"npm": ">=6"
}
},
"node_modules/jsonwebtoken/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/jstransformer": { "node_modules/jstransformer": {
"version": "0.0.2", "version": "0.0.2",
"resolved": "https://registry.npmmirror.com/jstransformer/-/jstransformer-0.0.2.tgz", "resolved": "https://registry.npmmirror.com/jstransformer/-/jstransformer-0.0.2.tgz",
@ -630,6 +688,27 @@
"promise": "^6.0.1" "promise": "^6.0.1"
} }
}, },
"node_modules/jwa": {
"version": "1.4.1",
"resolved": "https://registry.npmmirror.com/jwa/-/jwa-1.4.1.tgz",
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
"license": "MIT",
"dependencies": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"node_modules/jws": {
"version": "3.2.2",
"resolved": "https://registry.npmmirror.com/jws/-/jws-3.2.2.tgz",
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
"license": "MIT",
"dependencies": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"node_modules/kind-of": { "node_modules/kind-of": {
"version": "3.2.2", "version": "3.2.2",
"resolved": "https://registry.npmmirror.com/kind-of/-/kind-of-3.2.2.tgz", "resolved": "https://registry.npmmirror.com/kind-of/-/kind-of-3.2.2.tgz",
@ -657,6 +736,48 @@
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmmirror.com/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==",
"license": "MIT"
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmmirror.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==",
"license": "MIT"
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmmirror.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==",
"license": "MIT"
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmmirror.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==",
"license": "MIT"
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmmirror.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==",
"license": "MIT"
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmmirror.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==",
"license": "MIT"
},
"node_modules/lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmmirror.com/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==",
"license": "MIT"
},
"node_modules/long": { "node_modules/long": {
"version": "5.2.3", "version": "5.2.3",
"resolved": "https://registry.npmmirror.com/long/-/long-5.2.3.tgz", "resolved": "https://registry.npmmirror.com/long/-/long-5.2.3.tgz",
@ -878,6 +999,15 @@
"node": ">= 0.6" "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": { "node_modules/on-finished": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.3.0.tgz", "resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.3.0.tgz",

@ -8,11 +8,13 @@
}, },
"dependencies": { "dependencies": {
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9", "debug": "~2.6.9",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "~1.11.0", "jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",
"md5": "^2.3.0", "md5": "^2.3.0",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"mysql2": "^3.11.4", "mysql2": "^3.11.4",

@ -0,0 +1,27 @@
const express = require("express")
const router = express.Router()
const {
getExpressServices,
addExpressServices,
updateExpressServices
} = require("../services/expressService")
const {
formatResponse
} = require("../utils/tools")
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

@ -1,10 +1,13 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
const { const {
loginService loginService,
updateUserInfoService,
getUserInfoService
} = require("../services/userService") } = require("../services/userService")
const { const {
formatResponse formatResponse,
analysisToken
} = require("../utils/tools") } = require("../utils/tools")
/* GET home page. */ /* GET home page. */
@ -13,11 +16,31 @@ router.post('/login', async function (req, res, next) {
const result = await loginService(req.body) const result = await loginService(req.body)
if (result.token) { if (result.token) {
//有token登陆成功 //有token登陆成功
console.log(result.data)
res.setHeader("authentication", result.token)
res.send(formatResponse(0, "", result.data)) res.send(formatResponse(0, "", result.data))
} else { } else {
throw new Error("账号或密码错误") // throw new Error("账号或密码错误")
res.send(formatResponse(401, "账号或密码错误", null))
} }
}); });
router.get("/whoami", async function (req, res, next) {
//获取token,然后进行解析
const {
loginId,
} = analysisToken(req.get("authorization"))
const {
loginPwd,
...data
} = await getUserInfoService(loginId)
res.send(formatResponse(0, "", data))
})
router.put("/", async function (req, res, next) {
const data = await updateUserInfoService(req.body)
res.send(formatResponse(0, "", data))
})
module.exports = router; module.exports = router;

@ -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)
}

@ -1,27 +1,47 @@
const md5 = require('md5'); const md5 = require('md5');
const { const {
loginDao loginDao,
updateUserDao,
getUserInfoDao
} = require("../dao/userDao") } = require("../dao/userDao")
const jwt = require("jsonwebtoken");
const {
getJwtToken
} = require('../utils/tools');
module.exports.loginService = async function (loginInfo) { module.exports.loginService = async function (loginInfo) {
//将明文密码转换为加密密码 //将明文密码转换为加密密码
loginInfo.loginPwd = md5(loginInfo.loginPwd); loginInfo.loginPwd = md5(loginInfo.loginPwd);
let userInfo = await loginDao(loginInfo) let userInfo = await loginDao(loginInfo)
if (userInfo) { if (userInfo) {
userInfo = { const {
id: userInfo.id, loginPwd,
loginId: userInfo.loginId id,
} ...data
console.log(userInfo) } = userInfo.dataValues
//添加token后返回 //添加token后返回
let token = "" let token = ""
token = "123" token = getJwtToken(data)
return { return {
token, token,
data: userInfo data
} }
} }
return { return {
data: userInfo data: userInfo
} }
}
module.exports.updateUserInfoService = async function (userInfo) {
// userInfo.loginPwd = md5(userInfo.loginPwd)
await updateUserDao(userInfo)
const result = await getUserInfoDao(userInfo.loginId)
return result.dataValues
}
module.exports.getUserInfoService = async function (loginId) {
const data = await getUserInfoDao(loginId)
return data.dataValues
} }

@ -1,7 +1,37 @@
const jwt = require("jsonwebtoken")
const md5 = require("md5")
module.exports.formatResponse = function (code, msg, data) { module.exports.formatResponse = function (code, msg, data) {
return { return {
code, code,
msg, msg,
data data
} }
}
module.exports.analysisToken = function (token) {
return jwt.verify(token.split(" ")[1], md5(process.env.JWT_SECRET))
}
module.exports.getJwtToken = function (payload) {
let loginPeriod = 1; //默认记住一天
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)
// }
//倒叙输出
for (let i = arr.length - 1; i >= 0; i--) {
result.push(arr[i].dataValues)
}
return result
} }
Loading…
Cancel
Save