diff --git a/server/app/api/controller/GameController.php b/server/app/api/controller/GameController.php index 57e1a22..bd07061 100644 --- a/server/app/api/controller/GameController.php +++ b/server/app/api/controller/GameController.php @@ -71,7 +71,10 @@ class GameController extends OpenController { $userId = (int) ($request->player_id ?? 0); $direction = $request->post('direction'); - if (!in_array($direction, ['0', '1'], true)) { + if ($direction !== null) { + $direction = (int) $direction; + } + if (!in_array($direction, [0, 1], true)) { return $this->fail('direction 必须为 0 或 1', ReturnCode::PARAMS_ERROR); } diff --git a/server/app/api/controller/UserController.php b/server/app/api/controller/UserController.php index 4898eaf..b72a9e9 100644 --- a/server/app/api/controller/UserController.php +++ b/server/app/api/controller/UserController.php @@ -19,26 +19,20 @@ use plugin\saiadmin\basic\OpenController; class UserController extends OpenController { /** - * 登录(JSON body) + * 登录(form-data 参数) * POST /api/user/Login - * body: { "username": "+60123456789", "password": "123456", "lang": "chs", "coin": 2000.00, "time": 1772692089 } + * body: username, password, lang(可选), coin(可选), time(可选) * 根据 username 查找或创建 DicePlayer,按 coin 增减平台币,会话写 Redis,返回带 token 的连接地址 */ public function Login(Request $request): Response { - $body = $request->rawBody(); - if ($body === '' || $body === null) { - return $this->fail('请提交 JSON body', ReturnCode::PARAMS_ERROR); - } - $data = json_decode($body, true); - if (!is_array($data)) { - return $this->fail('JSON 格式错误', ReturnCode::PARAMS_ERROR); - } - $username = trim((string) ($data['username'] ?? '')); - $password = trim((string) ($data['password'] ?? '')); - $lang = trim((string) ($data['lang'] ?? 'chs')); - $coin = isset($data['coin']) ? (float) $data['coin'] : 0.0; - $time = isset($data['time']) ? (string) $data['time'] : (string) time(); + $username = trim((string) ($request->post('username', ''))); + $password = trim((string) ($request->post('password', ''))); + $lang = trim((string) ($request->post('lang', 'chs'))); + $coin = $request->post('coin'); + $coin = $coin !== null && $coin !== '' ? (float) $coin : 0.0; + $time = $request->post('time'); + $time = $time !== null && $time !== '' ? (string) $time : (string) time(); if ($username === '' || $password === '') { return $this->fail('username、password 不能为空', ReturnCode::PARAMS_ERROR); }