56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace app\api\controller\v1;
|
||
|
||
use app\api\logic\UserLogic;
|
||
use app\api\util\ReturnCode;
|
||
use plugin\saiadmin\basic\OpenController;
|
||
use support\Request;
|
||
use support\Response;
|
||
|
||
/**
|
||
* 平台 v1 游戏接口
|
||
* 获取进入游戏:/api/v1/getGameUrl
|
||
* 请求头:auth-token
|
||
* POST 参数:username, password(默认123456), time
|
||
*/
|
||
class GameController extends OpenController
|
||
{
|
||
/**
|
||
* 获取游戏地址
|
||
* 根据 username 创建登录 token(JWT),拼接游戏地址返回
|
||
*/
|
||
public function getGameUrl(Request $request): Response
|
||
{
|
||
$username = trim((string) ($request->post('username', '')));
|
||
$password = trim((string) ($request->post('password', '123456')));
|
||
$time = trim((string) ($request->post('time', '')));
|
||
|
||
if ($username === '') {
|
||
return $this->fail('username 不能为空', ReturnCode::PARAMS_ERROR);
|
||
}
|
||
if ($password === '') {
|
||
$password = '123456';
|
||
}
|
||
if ($time === '') {
|
||
$time = (string) time();
|
||
}
|
||
|
||
try {
|
||
$logic = new UserLogic();
|
||
$result = $logic->loginByUsername($username, $password, 'chs', 0.0, $time);
|
||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||
return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR);
|
||
}
|
||
|
||
$gameUrlBase = rtrim(config('api.game_url', 'dice-game.yuliao666.top'), '/');
|
||
$tokenInUrl = str_replace('%3D', '=', urlencode($result['token']));
|
||
$url = $gameUrlBase . '/?token=' . $tokenInUrl;
|
||
|
||
return $this->success([
|
||
'url' => $url,
|
||
]);
|
||
}
|
||
}
|