优化所有接口使用form-data类型

This commit is contained in:
2026-03-06 12:29:40 +08:00
parent 943d8f7b5f
commit c1b4790f04
2 changed files with 13 additions and 16 deletions

View File

@@ -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);
}

View File

@@ -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);
}