项目初始化
This commit is contained in:
142
backend/src/controllers/playerAuthController.ts
Normal file
142
backend/src/controllers/playerAuthController.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
import { Request, Response } from 'express';
|
||||
import axios from 'axios';
|
||||
|
||||
export class PlayerAuthController {
|
||||
private readonly gameServerUrl: string;
|
||||
|
||||
constructor() {
|
||||
this.gameServerUrl = process.env.GAME_SERVER_PROXY_URL || 'http://127.0.0.1:8080/tool/http';
|
||||
}
|
||||
|
||||
async login(req: Request, res: Response) {
|
||||
try {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '用户名和密码不能为空'
|
||||
});
|
||||
}
|
||||
|
||||
const response = await axios.post(
|
||||
`${this.gameServerUrl}?code=auth/login`,
|
||||
{
|
||||
username,
|
||||
password
|
||||
},
|
||||
{
|
||||
proxy: false
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data.success && response.data.code === 200) {
|
||||
return res.json({
|
||||
success: true,
|
||||
message: '登录成功',
|
||||
data: response.data.data
|
||||
});
|
||||
} else {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: response.data.message || '登录失败'
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('玩家登录失败:', error);
|
||||
if (error.response) {
|
||||
return res.status(error.response.status || 500).json({
|
||||
success: false,
|
||||
message: error.response.data?.message || '登录失败'
|
||||
});
|
||||
}
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async logout(req: any, res: Response) {
|
||||
try {
|
||||
const token = req.headers?.authorization?.replace('Bearer ', '');
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: '未授权'
|
||||
});
|
||||
}
|
||||
|
||||
await axios.post(
|
||||
`${this.gameServerUrl}?code=auth/out_login`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: token
|
||||
},
|
||||
proxy: false
|
||||
}
|
||||
);
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
message: '退出登录成功'
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('玩家退出登录失败:', error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: '退出登录失败'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getAccountInfo(req: any, res: Response) {
|
||||
try {
|
||||
const token = req.headers?.authorization?.replace('Bearer ', '');
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: '未授权'
|
||||
});
|
||||
}
|
||||
|
||||
const response = await axios.post(
|
||||
`${this.gameServerUrl}?code=account/get_account`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: token
|
||||
},
|
||||
proxy: false
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data.success && response.data.code === 200) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: response.data.data
|
||||
});
|
||||
} else {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: response.data.message || '获取账号信息失败'
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('获取账号信息失败:', error);
|
||||
if (error.response) {
|
||||
return res.status(error.response.status || 500).json({
|
||||
success: false,
|
||||
message: error.response.data?.message || '获取账号信息失败'
|
||||
});
|
||||
}
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user