[积分商城]优化对接API

This commit is contained in:
2026-03-30 11:47:32 +08:00
parent b30ef21780
commit 4a42899bfe
55 changed files with 835 additions and 1241 deletions

View File

@@ -4,9 +4,14 @@ declare(strict_types=1);
namespace app\api\controller\v1;
use ba\Random;
use Throwable;
use app\common\controller\Api;
use app\common\facade\Token;
use app\common\library\Auth as UserAuth;
use app\common\library\AgentJwt;
use app\common\model\ChannelManage;
use app\common\model\MallPlayxUserAsset;
use app\admin\model\Admin;
use Webman\Http\Request;
use support\Response;
@@ -26,6 +31,11 @@ class Auth extends Api
*/
protected int $timeTolerance = 300;
/**
* 临时登录 token 有效期(秒)
*/
protected int $tempTokenExpire = 86400;
/**
* 获取鉴权 TokenGET 请求)
* 参数仅从 Query 读取signature、secret、agent_id、time
@@ -47,7 +57,7 @@ class Auth extends Api
return $this->error(__('Parameter signature/secret/agent_id/time can not be empty'));
}
$timestamp = (int) $time;
$timestamp = intval($time);
if ($timestamp <= 0) {
return $this->error(__('Invalid timestamp'));
}
@@ -62,7 +72,7 @@ class Auth extends Api
return $this->error(__('Agent not found'));
}
$channelId = (int) ($admin->channel_id ?? 0);
$channelId = intval($admin->channel_id ?? 0);
if ($channelId <= 0) {
return $this->error(__('Agent not found'));
}
@@ -81,7 +91,7 @@ class Auth extends Api
return $this->error(__('Invalid signature'));
}
$expire = (int) config('buildadmin.agent_auth.token_expire', 86400);
$expire = intval(config('buildadmin.agent_auth.token_expire', 86400));
$payload = [
'agent_id' => $agentId,
'channel_id' => $channel->id,
@@ -93,4 +103,52 @@ class Auth extends Api
'authtoken' => $authtoken,
]);
}
/**
* H5 临时登录GET/POST
* 参数username
* 写入或复用 mall_playx_user_asset签发 muser 类型 tokenuser_id 为资产表主键)
*/
public function temLogin(Request $request): Response
{
$response = $this->initializeApi($request);
if ($response !== null) {
return $response;
}
$enabled = config('buildadmin.agent_auth.temp_login_enable', false);
if (!$enabled) {
return $this->error(__('Temp login is disabled'));
}
$username = trim(strval($request->get('username', $request->post('username', ''))));
if ($username === '') {
return $this->error(__('Parameter username can not be empty'));
}
try {
$asset = MallPlayxUserAsset::ensureForUsername($username);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
$token = Random::uuid();
$refreshToken = Random::uuid();
$expire = config('buildadmin.agent_auth.temp_login_expire', $this->tempTokenExpire);
$assetId = intval($asset->getKey());
Token::set($token, UserAuth::TOKEN_TYPE_MALL_USER, $assetId, $expire);
Token::set($refreshToken, UserAuth::TOKEN_TYPE_MALL_USER . '-refresh', $assetId, 2592000);
return $this->success('', [
'userInfo' => [
'id' => $assetId,
'username' => strval($asset->username ?? ''),
'nickname' => strval($asset->username ?? ''),
'playx_user_id' => strval($asset->playx_user_id ?? ''),
'token' => $token,
'refresh_token' => $refreshToken,
'expires_in' => $expire,
],
]);
}
}