feat(server): 添加服务器列表接口和用户进入游戏功能

- 在登录模块中添加服务器列表获取接口 /api/server/list
- 实现用户进入游戏功能,记录登录时间和IP地址
- 添加时间工具函数用于格式化时间戳
- 配置Koa代理支持
- 更新白名单路由配置
- 添加MD5加密、Cookie操作和通用工具函数库
This commit is contained in:
2025-12-25 00:46:56 +08:00
parent 293fbd8bc8
commit eb05688ccc
59 changed files with 1019 additions and 138 deletions

View File

@@ -4,6 +4,7 @@ import * as log4js from "../log4js.js";
const whiteList = [
'/',
'/api/login',
"/api/server/list"
]
async function auth(ctx, next) {

View File

@@ -18,6 +18,7 @@ router.get('/', (ctx) => {
router.get('/api/config', (ctx) => {
ctx.body = {data: config}
})
app.proxy = true;
app.use(auth)
app.use(router.routes());
app.use(registry)

View File

@@ -2,6 +2,7 @@ import Router from 'koa-router';
import mysql from "../mysql/index.js";
import jwt from "jsonwebtoken";
import * as log4js from "../log4js.js";
import {time} from "../utils.js";
const router = new Router()
@@ -18,7 +19,16 @@ router.post("/api/login", async (ctx) => {
})
router.post("/api/enter_game", async (ctx) => {
const {srvId, account} = ctx.request.body
if (!srvId || !account) return ctx.body = {code: 1, message: "参数错误"}
log4js.koa.info("用户进入游戏", account, ctx.ip)
await mysql.query("UPDATE mir_web.player_game SET login_time = ?,login_ip = ? WHERE username = ?", [time(), ctx.ip, account])
return ctx.body = {code: 0, message: "进入游戏成功"}
})
router.get("/api/server/list", async (ctx) => {
const [rows] = await mysql.query("SELECT * FROM mir_web.server WHERE status >= 1 ORDER BY server_id ASC limit 1000")
return ctx.body = {code: 0, message: "获取服务器列表成功", data: rows}
})
export default router.routes()

View File

@@ -9,6 +9,7 @@
"dev": "nodemon --exec \"node --env-file=.env\" index.js"
},
"dependencies": {
"dayjs": "^1.11.19",
"jsonwebtoken": "^9.0.3",
"koa": "^2.15.0",
"koa-router": "^12.0.0",

5
module/server/utils.js Normal file
View File

@@ -0,0 +1,5 @@
import dayjs from "dayjs";
export function time(date){
return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}