添加银行和政府端小程序
This commit is contained in:
@@ -23,8 +23,8 @@ const accessLogStream = fs.createWriteStream(
|
||||
);
|
||||
app.use(morgan('combined', { stream: accessLogStream }));
|
||||
|
||||
// 数据库连接
|
||||
const sequelize = require('./config/database');
|
||||
// 数据库连接(暂时注释掉,使用内存数据)
|
||||
// const sequelize = require('./config/database');
|
||||
|
||||
// 路由
|
||||
app.use('/api/auth', require('./routes/auth'));
|
||||
@@ -57,5 +57,5 @@ app.use((err, req, res, next) => {
|
||||
const PORT = process.env.PORT || 5352;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`政府管理系统后端服务已启动,端口: ${PORT}`);
|
||||
console.log(`数据库: ${process.env.DB_NAME}@${process.env.DB_HOST}:${process.env.DB_PORT}`);
|
||||
console.log(`使用内存数据,无需数据库连接`);
|
||||
});
|
||||
@@ -1,13 +1,34 @@
|
||||
const jwt = require('jsonwebtoken')
|
||||
const { JWT_SECRET } = require('../config')
|
||||
|
||||
// JWT配置
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production'
|
||||
|
||||
// 临时用户数据(实际项目中应该从数据库获取)
|
||||
const users = [
|
||||
{
|
||||
id: 1,
|
||||
username: 'admin',
|
||||
password: '123456',
|
||||
name: '系统管理员',
|
||||
role: 'admin',
|
||||
email: 'admin@example.com'
|
||||
}
|
||||
]
|
||||
|
||||
exports.login = async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body
|
||||
|
||||
// 临时模拟登录验证
|
||||
if (username === 'admin' && password === '123456') {
|
||||
const token = jwt.sign({ username }, JWT_SECRET, { expiresIn: '2h' })
|
||||
// 查找用户
|
||||
const user = users.find(u => u.username === username && u.password === password)
|
||||
|
||||
if (user) {
|
||||
const token = jwt.sign({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
role: user.role
|
||||
}, JWT_SECRET, { expiresIn: '2h' })
|
||||
|
||||
return res.json({
|
||||
code: 200,
|
||||
message: '登录成功',
|
||||
@@ -26,4 +47,57 @@ exports.login = async (req, res) => {
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
exports.getUserInfo = async (req, res) => {
|
||||
try {
|
||||
// 从token中解析用户信息
|
||||
const token = req.headers.authorization?.replace('Bearer ', '')
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
code: 401,
|
||||
message: '未提供认证令牌'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, JWT_SECRET)
|
||||
const user = users.find(u => u.id === decoded.id)
|
||||
|
||||
if (user) {
|
||||
const userInfo = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
avatar: '',
|
||||
email: user.email,
|
||||
permissions: ['dashboard', 'users', 'settings']
|
||||
}
|
||||
|
||||
return res.json({
|
||||
code: 200,
|
||||
message: '获取用户信息成功',
|
||||
data: userInfo
|
||||
})
|
||||
} else {
|
||||
return res.status(401).json({
|
||||
code: 401,
|
||||
message: '用户不存在'
|
||||
})
|
||||
}
|
||||
} catch (jwtError) {
|
||||
return res.status(401).json({
|
||||
code: 401,
|
||||
message: '认证令牌无效'
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器错误',
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
const { login } = require('../controllers/authController')
|
||||
const { login, getUserInfo } = require('../controllers/authController')
|
||||
|
||||
// 用户登录
|
||||
router.post('/login', login)
|
||||
|
||||
// 获取用户信息
|
||||
router.get('/userinfo', getUserInfo)
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user