Compare commits

...

2 Commits

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

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

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

@ -17,4 +17,12 @@ module.exports.updateUserDao = async function (loginInfo) {
loginId: loginInfo.loginId,
}
})
}
module.exports.getUserInfoDao = async function (loginId) {
return await userModel.findOne({
where: {
loginId,
}
})
}

23
package-lock.json generated

@ -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",

@ -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",

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

@ -2,7 +2,8 @@ var express = require('express');
var router = express.Router();
const {
loginService,
updateUserInfoService
updateUserInfoService,
getUserInfoService
} = require("../services/userService")
const {
formatResponse,
@ -15,6 +16,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 {
@ -28,27 +30,16 @@ router.get("/whoami", async function (req, res, next) {
//获取token,然后进行解析
const {
loginId,
sex,
brithday,
region,
number
} = analysisToken(req.get("authorization"))
res.send(formatResponse(0, "", {
loginId,
sex,
brithday,
region,
number
}))
const {
loginPwd,
...data
} = await getUserInfoService(loginId)
res.send(formatResponse(0, "", data))
})
router.put("/", async function (req, res, next) {
const {
token,
data
} = await updateUserInfoService(req.body)
res.setHeader("authentication", token)
const data = await updateUserInfoService(req.body)
res.send(formatResponse(0, "", data))
})

@ -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,7 +1,8 @@
const md5 = require('md5');
const {
loginDao,
updateUserDao
updateUserDao,
getUserInfoDao
} = require("../dao/userDao")
const jwt = require("jsonwebtoken");
const {
@ -31,16 +32,13 @@ module.exports.loginService = async function (loginInfo) {
}
module.exports.updateUserInfoService = async function (userInfo) {
const data = userInfo
data.loginPwd = md5(data.loginPwd)
await updateUserDao(data)
const {
loginPwd,
...result
} = userInfo
//重新赋值token
return {
token: getJwtToken(result),
data: result
}
await updateUserDao(userInfo)
const data = await getUserInfoDao(userInfo.loginId)
return data.dataValues
}
module.exports.getUserInfoService = async function (loginId) {
const data = await getUserInfoDao(loginId)
return data.dataValues
}

@ -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
}
Loading…
Cancel
Save