2025-12-24 23:48:14 +08:00
|
|
|
import Router from 'koa-router';
|
|
|
|
|
import mysql from "../mysql/index.js";
|
|
|
|
|
import jwt from "jsonwebtoken";
|
|
|
|
|
import * as log4js from "../log4js.js";
|
2025-12-25 00:46:56 +08:00
|
|
|
import {time} from "../utils.js";
|
2025-12-24 23:48:14 +08:00
|
|
|
|
|
|
|
|
const router = new Router()
|
|
|
|
|
|
|
|
|
|
router.post("/api/login", async (ctx) => {
|
|
|
|
|
const {username, password} = ctx.request.body
|
|
|
|
|
if (['admin'].includes(username)) return ctx.body = {code: 1, message: "该账户不对外开放"}
|
|
|
|
|
const [rows] = await mysql.query("SELECT * FROM mir_web.player WHERE username = ? AND password = ?", [username, password])
|
|
|
|
|
if (rows?.length == 1) {
|
|
|
|
|
const token = jwt.sign(rows[0], process.env.SECRET_KEY, {expiresIn: '24h'});
|
|
|
|
|
return ctx.body = {code: 0, message: "登录成功", token}
|
|
|
|
|
}
|
|
|
|
|
log4js.koa.error("用户登录失败", username)
|
|
|
|
|
return ctx.body = {code: 1, message: "用户名或密码错误"}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.post("/api/enter_game", async (ctx) => {
|
|
|
|
|
const {srvId, account} = ctx.request.body
|
2025-12-25 00:46:56 +08:00
|
|
|
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}
|
2025-12-24 23:48:14 +08:00
|
|
|
})
|
2025-12-25 00:46:56 +08:00
|
|
|
|
2025-12-24 23:48:14 +08:00
|
|
|
export default router.routes()
|