451 lines
14 KiB
JavaScript
451 lines
14 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { loginLib, stringToBuffer, updateUserIcon } = require('../utils/xnCoreService');
|
|
|
|
// 登录API路由
|
|
router.post('/login', (req, res) => {
|
|
const { username, password } = req.body;
|
|
|
|
if (!username || !password) {
|
|
return res.status(400).json({ success: false, message: '用户名和密码不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const usernameData = stringToBuffer(username);
|
|
const passwordData = stringToBuffer(password);
|
|
|
|
try {
|
|
const userId = loginLib.validateUser(
|
|
usernameData.buffer,
|
|
usernameData.length,
|
|
passwordData.buffer,
|
|
passwordData.length
|
|
);
|
|
|
|
if (userId > 0) {
|
|
try {
|
|
const userInfoBuffer = Buffer.alloc(1024 * 1024); // 增加到1MB以容纳头像数据
|
|
const userInfoState = loginLib.getUserInfo(userId, userInfoBuffer, userInfoBuffer.length);
|
|
|
|
if (userInfoState === 0) {
|
|
const zeroIndex = userInfoBuffer.indexOf(0);
|
|
const userInfoStr = userInfoBuffer.toString('utf8', 0, zeroIndex >= 0 ? zeroIndex : userInfoBuffer.length);
|
|
try {
|
|
const userInfo = JSON.parse(userInfoStr);
|
|
|
|
// 设置 session
|
|
req.session.user = userInfo;
|
|
|
|
console.log('用户', userInfo.username, '登录成功,', '权限等级:', userInfo.access_level);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '登录成功',
|
|
user: userInfo
|
|
});
|
|
} catch (parseError) {
|
|
console.error('解析用户信息失败:', parseError);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '解析用户信息失败',
|
|
error: parseError.message
|
|
});
|
|
}
|
|
} else {
|
|
res.status(501).json({
|
|
success: false,
|
|
message: '获取用户信息失败',
|
|
error: '用户信息获取失败,错误码:' + userInfoState
|
|
});
|
|
}
|
|
} catch (userInfoError) {
|
|
console.error('获取用户信息失败:', userInfoError);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '获取用户信息失败',
|
|
error: userInfoError.message
|
|
});
|
|
}
|
|
} else {
|
|
res.status(401).json({
|
|
success: false,
|
|
message: '用户名或密码错误',
|
|
error: '认证失败,错误码:' + userId
|
|
});
|
|
}
|
|
} catch (callError) {
|
|
console.error('调用动态库失败:', callError);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '调用动态库失败',
|
|
error: callError.message
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('登录处理过程出错:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// 登出API路由
|
|
router.post('/logout', (req, res) => {
|
|
req.session.destroy((err) => {
|
|
if (err) {
|
|
return res.status(500).json({
|
|
success: false,
|
|
message: '登出失败'
|
|
});
|
|
}
|
|
res.json({
|
|
success: true,
|
|
message: '已安全退出登录'
|
|
});
|
|
});
|
|
});
|
|
|
|
// 认证检查API路由
|
|
router.get('/check-auth', (req, res) => {
|
|
if (req.session.user) {
|
|
res.json({
|
|
success: true,
|
|
user: req.session.user
|
|
});
|
|
} else {
|
|
res.json({
|
|
success: false,
|
|
message: '未登录'
|
|
});
|
|
}
|
|
});
|
|
|
|
// 注册用户API路由
|
|
router.post('/register', (req, res) => {
|
|
const { username, password, userInfo } = req.body;
|
|
|
|
if (!username || !password || !userInfo) {
|
|
return res.status(400).json({ success: false, message: '用户名、密码和用户信息不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const usernameData = stringToBuffer(username);
|
|
const passwordData = stringToBuffer(password);
|
|
const userInfoData = stringToBuffer(JSON.stringify(userInfo));
|
|
|
|
try {
|
|
const userId = loginLib.registerUser(
|
|
usernameData.buffer,
|
|
usernameData.length,
|
|
passwordData.buffer,
|
|
passwordData.length,
|
|
userInfoData.buffer,
|
|
userInfoData.length
|
|
);
|
|
|
|
switch (userId) {
|
|
case -1:
|
|
res.status(500).json({ success: false, message: '注册失败:一般错误' });
|
|
break;
|
|
case -2:
|
|
res.status(400).json({ success: false, message: '用户名已存在' });
|
|
break;
|
|
case -3:
|
|
res.status(400).json({ success: false, message: '无效的用户信息格式' });
|
|
break;
|
|
default:
|
|
if (userId > 0) {
|
|
res.json({
|
|
success: true,
|
|
message: '注册成功',
|
|
userId: userId
|
|
});
|
|
} else {
|
|
res.status(500).json({ success: false, message: '未知错误' });
|
|
}
|
|
}
|
|
} catch (callError) {
|
|
throw callError;
|
|
}
|
|
} catch (error) {
|
|
console.error('注册处理过程出错:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// 获取环境变量
|
|
router.get('/env', (req, res) => {
|
|
const { name } = req.query;
|
|
if (!name) {
|
|
return res.status(400).json({ error: '缺少环境变量名称参数' });
|
|
}
|
|
|
|
// 出于安全考虑,只允许查询特定的环境变量
|
|
const allowedEnvVars = ['XNCore', 'PATH'];
|
|
if (!allowedEnvVars.includes(name)) {
|
|
return res.status(403).json({ error: '不允许查询该环境变量' });
|
|
}
|
|
|
|
const value = process.env[name] || '';
|
|
res.json({ name, value });
|
|
});
|
|
|
|
// 修改密码路由
|
|
router.post('/change-password', (req, res) => {
|
|
const { userId, oldPassword, newPassword } = req.body;
|
|
|
|
if (!userId || !oldPassword || !newPassword) {
|
|
return res.status(400).json({ success: false, message: '用户ID、旧密码和新密码不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const oldPasswordData = stringToBuffer(oldPassword);
|
|
const newPasswordData = stringToBuffer(newPassword);
|
|
|
|
const result = loginLib.changePassword(
|
|
userId,
|
|
oldPasswordData.buffer,
|
|
oldPasswordData.length,
|
|
newPasswordData.buffer,
|
|
newPasswordData.length
|
|
);
|
|
|
|
if (result === 0) {
|
|
res.json({ success: true, message: '密码修改成功' });
|
|
} else if(result === -1){
|
|
res.status(400).json({ success: false, message: '新密码或旧密码为空' });
|
|
} else if(result === -2){
|
|
res.status(400).json({ success: false, message: '新密码与旧密码相同' });
|
|
} else if(result === -3){
|
|
res.status(400).json({ success: false, message: '内部错误' });
|
|
} else if(result === -4){
|
|
res.status(400).json({ success: false, message: '旧密码错误' });
|
|
} else if(result === -5){
|
|
res.status(400).json({ success: false, message: '用户不存在' });
|
|
} else {
|
|
res.status(400).json({ success: false, message: '密码修改失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
console.error('密码修改过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新用户信息路由
|
|
router.post('/update-user-info', (req, res) => {
|
|
const { userId, userInfo } = req.body;
|
|
|
|
if (!userId || !userInfo) {
|
|
return res.status(400).json({ success: false, message: '用户ID和用户信息不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const userInfoData = stringToBuffer(JSON.stringify(userInfo));
|
|
|
|
const result = loginLib.updateUserInfo(
|
|
userId,
|
|
userInfoData.buffer,
|
|
userInfoData.length
|
|
);
|
|
|
|
if (result === 0) {
|
|
res.json({ success: true, message: '用户信息更新成功' });
|
|
} else {
|
|
res.status(400).json({ success: false, message: '用户信息更新失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
console.error('更新用户信息过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新用户权限级别路由
|
|
router.post('/update-access-level', (req, res) => {
|
|
const { userId, accessLevel } = req.body;
|
|
|
|
if (!userId || accessLevel === undefined) {
|
|
return res.status(400).json({ success: false, message: '用户ID和权限级别不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const result = loginLib.updateUserAccessLevel(userId, accessLevel);
|
|
|
|
if (result === 0) {
|
|
res.json({ success: true, message: '用户权限级别更新成功' });
|
|
} else {
|
|
console.error('更新用户权限级别',accessLevel,'失败,错误码:', result);
|
|
res.status(400).json({ success: false, message: '用户权限级别更新失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
console.error('更新用户权限级别过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取所有用户简要信息路由
|
|
router.get('/all-users', (req, res) => {
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const resultBuffer = Buffer.alloc(8192); // 分配8KB的缓冲区
|
|
const result = loginLib.getAllUsersSimpleInfo(resultBuffer, resultBuffer.length);
|
|
|
|
if (result === 0) {
|
|
// 找到字符串结束位置
|
|
const zeroIndex = resultBuffer.indexOf(0);
|
|
const usersInfoStr = resultBuffer.toString('utf8', 0, zeroIndex >= 0 ? zeroIndex : resultBuffer.length);
|
|
|
|
try {
|
|
const usersInfo = JSON.parse(usersInfoStr);
|
|
res.json({ success: true, users: usersInfo });
|
|
} catch (parseError) {
|
|
console.error('解析用户信息失败:', parseError);
|
|
res.status(500).json({ success: false, message: '解析用户信息失败', error: parseError.message });
|
|
}
|
|
} else {
|
|
res.status(400).json({ success: false, message: '获取用户信息失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
console.error('获取所有用户信息过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 重置用户密码路由
|
|
router.post('/reset-password', (req, res) => {
|
|
const { userId } = req.body;
|
|
|
|
if (!userId) {
|
|
return res.status(400).json({ success: false, message: '用户ID不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const result = loginLib.resetPassword(parseInt(userId));
|
|
|
|
if (result === 0) {
|
|
res.json({ success: true, message: '密码重置成功' });
|
|
} else {
|
|
res.status(400).json({ success: false, message: '密码重置失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除用户路由
|
|
router.delete('/delete-user', (req, res) => {
|
|
const { userId } = req.body;
|
|
|
|
if (!userId) {
|
|
return res.status(400).json({ success: false, message: '用户ID不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const result = loginLib.deleteUser(parseInt(userId));
|
|
|
|
if (result === 0) {
|
|
res.json({ success: true, message: '用户删除成功' });
|
|
} else {
|
|
res.status(400).json({ success: false, message: '用户删除失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取用户信息路由
|
|
router.get('/user-info/:userId', (req, res) => {
|
|
const { userId } = req.params;
|
|
|
|
if (!userId) {
|
|
return res.status(400).json({ success: false, message: '用户ID不能为空' });
|
|
}
|
|
|
|
try {
|
|
if (!loginLib) {
|
|
throw new Error('动态库未正确加载');
|
|
}
|
|
|
|
const userInfoBuffer = Buffer.alloc(1024 * 1024); // 增加到1MB以容纳头像数据
|
|
const result = loginLib.getUserInfo(parseInt(userId), userInfoBuffer, userInfoBuffer.length);
|
|
|
|
if (result === 0) {
|
|
// 找到字符串结束位置
|
|
const zeroIndex = userInfoBuffer.indexOf(0);
|
|
const userInfoStr = userInfoBuffer.toString('utf8', 0, zeroIndex >= 0 ? zeroIndex : userInfoBuffer.length);
|
|
|
|
try {
|
|
const userInfo = JSON.parse(userInfoStr);
|
|
res.json({ success: true, user: userInfo });
|
|
} catch (parseError) {
|
|
console.error('解析用户信息失败:', parseError);
|
|
res.status(500).json({ success: false, message: '解析用户信息失败', error: parseError.message });
|
|
}
|
|
} else {
|
|
res.status(400).json({ success: false, message: '获取用户信息失败', error: `错误码:${result}` });
|
|
}
|
|
} catch (error) {
|
|
console.error('获取用户信息过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新用户头像路由
|
|
router.post('/update-user-icon', (req, res) => {
|
|
const { userId, iconBase64 } = req.body;
|
|
|
|
if (!userId || !iconBase64) {
|
|
return res.status(400).json({ success: false, message: '用户ID和头像数据不能为空' });
|
|
}
|
|
|
|
try {
|
|
const result = updateUserIcon(parseInt(userId), iconBase64);
|
|
|
|
if (result === '头像更新成功') {
|
|
res.json({ success: true, message: '头像更新成功' });
|
|
} else {
|
|
res.status(400).json({ success: false, message: '头像更新失败', error: result });
|
|
}
|
|
} catch (error) {
|
|
console.error('更新用户头像过程出错:', error);
|
|
res.status(500).json({ success: false, message: '服务器内部错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|