后台新增获取游戏连接按钮
This commit is contained in:
@@ -16,6 +16,7 @@ use plugin\saiadmin\service\Permission;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\api\cache\UserCache;
|
||||
use Tinywan\Jwt\JwtToken;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
|
||||
/**
|
||||
@@ -210,6 +211,63 @@ class DicePlayerController extends BaseController
|
||||
return $this->success('update success');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏链接
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
#[Permission('获取游戏链接', 'dice:player:index:getGameUrl')]
|
||||
public function getGameUrl(Request $request): Response
|
||||
{
|
||||
$id = $request->input('id');
|
||||
if ($id === null || $id === '') {
|
||||
return $this->fail('missing parameter id');
|
||||
}
|
||||
$player = $this->logic->read($id);
|
||||
if (!$player) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
if ((int) ($player->status ?? 1) === 0) {
|
||||
return $this->fail('account is disabled');
|
||||
}
|
||||
$username = trim((string) ($player->username ?? ''));
|
||||
if ($username === '') {
|
||||
return $this->fail('username is empty');
|
||||
}
|
||||
$lang = trim((string) $request->input('lang', 'zh'));
|
||||
if (!in_array($lang, ['zh', 'en'], true)) {
|
||||
$lang = 'zh';
|
||||
}
|
||||
|
||||
$exp = (int) config('api.session_expire', 604800);
|
||||
$tokenResult = JwtToken::generateToken([
|
||||
'id' => (int) $player->id,
|
||||
'username' => $username,
|
||||
'plat' => 'api_login',
|
||||
'access_exp' => $exp,
|
||||
]);
|
||||
$token = (string) ($tokenResult['access_token'] ?? '');
|
||||
if ($token === '') {
|
||||
return $this->fail('generate token failed');
|
||||
}
|
||||
UserCache::setSessionByUsername($username, $token);
|
||||
$userArr = $player->hidden(['password', 'lottery_config_id', 't1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'])->toArray();
|
||||
UserCache::setUser((int) $player->id, $userArr);
|
||||
UserCache::setPlayerByUsername($username, $userArr);
|
||||
|
||||
$gameUrlBase = rtrim((string) env('GAME_URL', (string) config('api.game_url', '')), '/');
|
||||
if ($gameUrlBase === '') {
|
||||
return $this->fail('GAME_URL is not configured');
|
||||
}
|
||||
$tokenInUrl = str_replace('%3D', '=', urlencode($token));
|
||||
$url = $gameUrlBase . '/?token=' . $tokenInUrl . '&lang=' . $lang;
|
||||
return $this->success(['url' => $url]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param Request $request
|
||||
|
||||
Reference in New Issue
Block a user