重新设计状态码规范
This commit is contained in:
@@ -25,7 +25,7 @@ class AuthTokenController extends OpenController
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
if (strtoupper($request->method()) !== 'GET') {
|
||||
return $this->fail('仅支持 GET 请求', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('仅支持 GET 请求', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
|
||||
$param = $request->get();
|
||||
@@ -35,27 +35,27 @@ class AuthTokenController extends OpenController
|
||||
$time = trim((string) ($param['time'] ?? ''));
|
||||
|
||||
if ($signature === '' || $secret === '' || $device === '' || $time === '') {
|
||||
return $this->fail('signature、secret、device、time 均为必传且不能为空', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('signature、secret、device、time 均为必传且不能为空', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
|
||||
$serverSecret = trim((string) config('api.auth_token_secret', ''));
|
||||
if ($serverSecret === '') {
|
||||
return $this->fail('服务未配置 API_AUTH_TOKEN_SECRET', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('服务未配置 API_AUTH_TOKEN_SECRET', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
if ($secret !== $serverSecret) {
|
||||
return $this->fail('密钥错误', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('密钥错误', ReturnCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
$tolerance = (int) config('api.auth_token_time_tolerance', 300);
|
||||
$now = time();
|
||||
$ts = is_numeric($time) ? (int) $time : 0;
|
||||
if ($ts <= 0 || abs($now - $ts) > $tolerance) {
|
||||
return $this->fail('时间戳无效或已过期', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('时间戳无效或已过期', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
|
||||
$sign = $this->getAuthToken($device, $serverSecret, $time);
|
||||
if ($sign !== $signature) {
|
||||
return $this->fail('签名验证失败', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('签名验证失败', ReturnCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
$exp = (int) config('api.auth_token_exp', 86400);
|
||||
|
||||
@@ -31,7 +31,7 @@ class GameController extends OpenController
|
||||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||||
$count = (int) $request->post('count', 0);
|
||||
if (!in_array($count, [1, 5, 10], true)) {
|
||||
return $this->fail('购买抽奖券错误', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('购买抽奖券错误', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -45,7 +45,7 @@ class GameController extends OpenController
|
||||
$coin = $player ? (float) $player->coin : 0;
|
||||
return $this->success(['coin' => $coin], $msg);
|
||||
}
|
||||
return $this->fail($msg, ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail($msg, ReturnCode::BUSINESS_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,16 +72,16 @@ class GameController extends OpenController
|
||||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||||
$rediction = $request->post('rediction');
|
||||
if ($rediction === '' || $rediction === null) {
|
||||
return $this->fail('请传递 rediction 参数', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('请传递 rediction 参数', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$direction = (int) $rediction;
|
||||
if (!in_array($direction, [0, 1], true)) {
|
||||
return $this->fail('rediction 必须为 0 或 1', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('rediction 必须为 0 或 1', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
|
||||
$player = DicePlayer::find($userId);
|
||||
if (!$player) {
|
||||
return $this->fail('用户不存在', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
|
||||
}
|
||||
$minEv = (float) DiceRewardConfig::min('real_ev');
|
||||
$minCoin = abs($minEv + 100);
|
||||
@@ -95,7 +95,7 @@ class GameController extends OpenController
|
||||
$data = $logic->run($userId, $direction);
|
||||
return $this->success($data);
|
||||
} catch (ApiException $e) {
|
||||
return $this->fail($e->getMessage(), ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail($e->getMessage(), ReturnCode::BUSINESS_ERROR);
|
||||
} catch (\Throwable $e) {
|
||||
$timeoutRecord = null;
|
||||
try {
|
||||
|
||||
@@ -28,7 +28,7 @@ class UserController extends OpenController
|
||||
$phone = $request->post('phone', '');
|
||||
$password = $request->post('password', '');
|
||||
if ($phone === '' || $password === '') {
|
||||
return $this->fail('请填写手机号和密码', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$logic = new UserLogic();
|
||||
$data = $logic->login($phone, $password);
|
||||
@@ -50,7 +50,7 @@ class UserController extends OpenController
|
||||
$password = $request->post('password', '');
|
||||
$nickname = $request->post('nickname');
|
||||
if ($phone === '' || $password === '') {
|
||||
return $this->fail('请填写手机号和密码', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$logic = new UserLogic();
|
||||
$data = $logic->register($phone, $password, $nickname ? (string) $nickname : null);
|
||||
@@ -70,7 +70,7 @@ class UserController extends OpenController
|
||||
{
|
||||
$token = $request->userToken ?? UserLogic::getTokenFromRequest($request);
|
||||
if ($token === '' || !UserLogic::logout($token)) {
|
||||
return $this->fail('退出失败或 token 已失效', ReturnCode::TOKEN_TIMEOUT);
|
||||
return $this->fail('退出失败或 token 已失效', ReturnCode::TOKEN_INVALID);
|
||||
}
|
||||
return $this->success('已退出登录');
|
||||
}
|
||||
@@ -86,7 +86,7 @@ class UserController extends OpenController
|
||||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||||
$user = UserLogic::getCachedUser($userId);
|
||||
if (empty($user)) {
|
||||
return $this->fail('用户不存在', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
|
||||
}
|
||||
$fields = ['id', 'username', 'phone', 'uid', 'name', 'coin', 'total_draw_count'];
|
||||
$info = [];
|
||||
@@ -108,7 +108,7 @@ class UserController extends OpenController
|
||||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||||
$user = UserLogic::getCachedUser($userId);
|
||||
if (empty($user)) {
|
||||
return $this->fail('用户不存在', ReturnCode::EMPTY_PARAMS);
|
||||
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
|
||||
}
|
||||
$coin = $user['coin'] ?? 0;
|
||||
if (is_string($coin) && is_numeric($coin)) {
|
||||
|
||||
Reference in New Issue
Block a user