Compare commits

..

No commits in common. 'ea5a1152fd400ce99deb7fe98a0fe7cdf45a8cb7' and '3acbc3a9b92f84be14a070c1a54a800cec4cbf1f' have entirely different histories.

@ -3,28 +3,17 @@ 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());
@ -36,7 +25,6 @@ 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,10 +1,6 @@
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({
@ -23,36 +19,6 @@ 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()

@ -1,20 +0,0 @@
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())
}

@ -1,37 +0,0 @@
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,12 +17,4 @@ 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,7 +9,6 @@
"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",
@ -318,19 +317,6 @@
"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",
@ -999,15 +985,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": {
"version": "2.3.0",
"resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.3.0.tgz",

@ -8,7 +8,6 @@
},
"dependencies": {
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"express": "~4.16.1",

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

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

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

@ -18,16 +18,4 @@ 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