refactor(websocket): 统一将member_id重命名为user_id
修改认证参数和相关变量名,从member_id改为user_id以保持命名一致性
This commit is contained in:
@@ -19,7 +19,7 @@ class WebSocket extends WebSocketBase
|
|||||||
// 控制器属性,用于替代BaseApi中的属性
|
// 控制器属性,用于替代BaseApi中的属性
|
||||||
public $params;
|
public $params;
|
||||||
public $token;
|
public $token;
|
||||||
protected $member_id;
|
protected $user_id;
|
||||||
protected $site_id;
|
protected $site_id;
|
||||||
protected $uniacid;
|
protected $uniacid;
|
||||||
protected $site_ids = [];
|
protected $site_ids = [];
|
||||||
@@ -36,7 +36,7 @@ class WebSocket extends WebSocketBase
|
|||||||
// 初始化控制器属性
|
// 初始化控制器属性
|
||||||
$this->params = [];
|
$this->params = [];
|
||||||
$this->token = '';
|
$this->token = '';
|
||||||
$this->member_id = 0;
|
$this->user_id = 0;
|
||||||
$this->site_id = 0;
|
$this->site_id = 0;
|
||||||
$this->uniacid = 0;
|
$this->uniacid = 0;
|
||||||
$this->app_type = 'weapp'; // 默认微信小程序
|
$this->app_type = 'weapp'; // 默认微信小程序
|
||||||
@@ -53,7 +53,7 @@ class WebSocket extends WebSocketBase
|
|||||||
$this->clientData[$conn->resourceId] = [
|
$this->clientData[$conn->resourceId] = [
|
||||||
'connection' => $conn,
|
'connection' => $conn,
|
||||||
'site_id' => null,
|
'site_id' => null,
|
||||||
'member_id' => null,
|
'user_id' => null,
|
||||||
'token' => null,
|
'token' => null,
|
||||||
'is_authenticated' => false,
|
'is_authenticated' => false,
|
||||||
'conversation_id' => null,
|
'conversation_id' => null,
|
||||||
@@ -155,18 +155,18 @@ class WebSocket extends WebSocketBase
|
|||||||
* - WebSocket 连接场景没有 request()/input(),所以这里直接根据客户端传入的 site_id + token 解密校验
|
* - WebSocket 连接场景没有 request()/input(),所以这里直接根据客户端传入的 site_id + token 解密校验
|
||||||
* - 校验 token 解密成功、未过期、且 token 内 member_id 与传入 member_id 一致
|
* - 校验 token 解密成功、未过期、且 token 内 member_id 与传入 member_id 一致
|
||||||
*/
|
*/
|
||||||
protected function doAuth(ConnectionInterface $conn, $site_id, $member_id, $token)
|
protected function doAuth(ConnectionInterface $conn, $site_id, $user_id, $token)
|
||||||
{
|
{
|
||||||
// 与 Kefu.php 保持一致,支持使用 uniacid 作为站点ID
|
// 与 Kefu.php 保持一致,支持使用 uniacid 作为站点ID
|
||||||
$site_id = (int)$site_id;
|
$site_id = (int)$site_id;
|
||||||
$member_id = (int)$member_id;
|
$user_id = (int)$user_id;
|
||||||
$token = (string)$token;
|
$token = (string)$token;
|
||||||
|
|
||||||
if ($site_id <= 0 || $member_id <= 0 || $token === '') {
|
if ($site_id <= 0 || $user_id <= 0 || $token === '') {
|
||||||
throw new \Exception('Missing authentication parameters');
|
throw new \Exception('Missing authentication parameters');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->log('doAuth: ' . json_encode(['site_id' => $site_id, 'member_id' => $member_id, 'token' => $token]), 'info');
|
$this->log('doAuth: ' . json_encode(['site_id' => $site_id, 'user_id' => $user_id, 'token' => $token]), 'info');
|
||||||
|
|
||||||
// 生成与 BaseApi::checkToken 一致的解密 key:private_key + 'site' . site_id(如启用 API 私钥)
|
// 生成与 BaseApi::checkToken 一致的解密 key:private_key + 'site' . site_id(如启用 API 私钥)
|
||||||
$key = 'site' . $site_id;
|
$key = 'site' . $site_id;
|
||||||
@@ -208,15 +208,15 @@ class WebSocket extends WebSocketBase
|
|||||||
$this->log('expire_time:' . $expire_time, 'info');
|
$this->log('expire_time:' . $expire_time, 'info');
|
||||||
|
|
||||||
// 与 BaseApi 行为一致:临近过期时生成 refresh_token 放入缓存(可选,不强制给客户端)
|
// 与 BaseApi 行为一致:临近过期时生成 refresh_token 放入缓存(可选,不强制给客户端)
|
||||||
if ($expire_time !== 0 && ($expire_time - time()) < 300 && !Cache::get('member_token' . $member_id)) {
|
if ($expire_time !== 0 && ($expire_time - time()) < 300 && !Cache::get('member_token' . $user_id)) {
|
||||||
try {
|
try {
|
||||||
// WebSocket 场景不强制下发 refresh_token,但仍按原逻辑缓存,便于其他接口复用
|
// WebSocket 场景不强制下发 refresh_token,但仍按原逻辑缓存,便于其他接口复用
|
||||||
$refresh_token = encrypt(json_encode([
|
$refresh_token = encrypt(json_encode([
|
||||||
'member_id' => $member_id,
|
'member_id' => $user_id,
|
||||||
'create_time' => time(),
|
'create_time' => time(),
|
||||||
'expire_time' => $expire_time,
|
'expire_time' => $expire_time,
|
||||||
]), $key);
|
]), $key);
|
||||||
Cache::set('member_token' . $member_id, $refresh_token, 360);
|
Cache::set('member_token' . $user_id, $refresh_token, 360);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
// 刷新失败不影响当前鉴权通过
|
// 刷新失败不影响当前鉴权通过
|
||||||
}
|
}
|
||||||
@@ -235,14 +235,14 @@ class WebSocket extends WebSocketBase
|
|||||||
|
|
||||||
// 获取请求参数,与 Kefu.php 保持一致
|
// 获取请求参数,与 Kefu.php 保持一致
|
||||||
$query = $data['query'] ?? $data['message'] ?? '';
|
$query = $data['query'] ?? $data['message'] ?? '';
|
||||||
$user_id = $data['user_id'] ?? $clientInfo['member_id'];
|
$user_id = $data['user_id'] ?? $clientInfo['user_id'];
|
||||||
$conversation_id = $data['conversation_id'] ?? '';
|
$conversation_id = $data['conversation_id'] ?? '';
|
||||||
$stream = $data['stream'] ?? false;
|
$stream = $data['stream'] ?? false;
|
||||||
$response_mode = $data['response_mode'] ?? 'streaming'; // 与 Kefu.php 保持一致
|
$response_mode = $data['response_mode'] ?? 'streaming'; // 与 Kefu.php 保持一致
|
||||||
|
|
||||||
// 设置当前控制器的属性,与 Kefu.php 保持一致的参数优先级
|
// 设置当前控制器的属性,与 Kefu.php 保持一致的参数优先级
|
||||||
$this->site_id = $data['uniacid'] ?? $clientInfo['site_id'];
|
$this->site_id = $data['uniacid'] ?? $clientInfo['site_id'];
|
||||||
$this->member_id = $data['member_id'] ?? $clientInfo['member_id'];
|
$this->user_id = $data['user_id'] ?? $clientInfo['user_id'];
|
||||||
$this->token = $data['token'] ?? $clientInfo['token'];
|
$this->token = $data['token'] ?? $clientInfo['token'];
|
||||||
$this->params = [
|
$this->params = [
|
||||||
'query' => $query,
|
'query' => $query,
|
||||||
@@ -251,7 +251,7 @@ class WebSocket extends WebSocketBase
|
|||||||
'stream' => $stream,
|
'stream' => $stream,
|
||||||
'response_mode' => $response_mode,
|
'response_mode' => $response_mode,
|
||||||
'uniacid' => $this->site_id,
|
'uniacid' => $this->site_id,
|
||||||
'member_id' => $this->member_id,
|
'user_id' => $this->user_id,
|
||||||
'token' => $this->token,
|
'token' => $this->token,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -303,7 +303,7 @@ class WebSocket extends WebSocketBase
|
|||||||
$kefu_conversation_model = new KefuConversationModel();
|
$kefu_conversation_model = new KefuConversationModel();
|
||||||
$kefu_message_model = new KefuMessageModel();
|
$kefu_message_model = new KefuMessageModel();
|
||||||
$site_id = $this->site_id;
|
$site_id = $this->site_id;
|
||||||
$current_user_id = $this->member_id;
|
$current_user_id = $this->user_id;
|
||||||
|
|
||||||
// 定义变量
|
// 定义变量
|
||||||
$real_conversation_id = '';
|
$real_conversation_id = '';
|
||||||
|
|||||||
@@ -379,7 +379,7 @@
|
|||||||
const authMsg = JSON.stringify({
|
const authMsg = JSON.stringify({
|
||||||
action: 'auth',
|
action: 'auth',
|
||||||
uniacid: 1,
|
uniacid: 1,
|
||||||
member_id: 1,
|
user_id: 1,
|
||||||
token: 'test_token'
|
token: 'test_token'
|
||||||
});
|
});
|
||||||
wsConnections[name].send(authMsg);
|
wsConnections[name].send(authMsg);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ abstract class WebSocketBase implements MessageComponentInterface
|
|||||||
// 控制器属性
|
// 控制器属性
|
||||||
public $params;
|
public $params;
|
||||||
public $token;
|
public $token;
|
||||||
protected $member_id;
|
protected $user_id;
|
||||||
protected $site_id;
|
protected $site_id;
|
||||||
protected $uniacid;
|
protected $uniacid;
|
||||||
protected $site_ids = [];
|
protected $site_ids = [];
|
||||||
@@ -36,7 +36,7 @@ abstract class WebSocketBase implements MessageComponentInterface
|
|||||||
// 初始化控制器属性
|
// 初始化控制器属性
|
||||||
$this->params = [];
|
$this->params = [];
|
||||||
$this->token = '';
|
$this->token = '';
|
||||||
$this->member_id = 0;
|
$this->user_id = 0;
|
||||||
$this->site_id = 0;
|
$this->site_id = 0;
|
||||||
$this->uniacid = 0;
|
$this->uniacid = 0;
|
||||||
$this->app_type = 'weapp'; // 默认微信小程序
|
$this->app_type = 'weapp'; // 默认微信小程序
|
||||||
@@ -53,7 +53,7 @@ abstract class WebSocketBase implements MessageComponentInterface
|
|||||||
$this->clientData[$conn->resourceId] = [
|
$this->clientData[$conn->resourceId] = [
|
||||||
'connection' => $conn,
|
'connection' => $conn,
|
||||||
'site_id' => null,
|
'site_id' => null,
|
||||||
'member_id' => null,
|
'user_id' => null,
|
||||||
'token' => null,
|
'token' => null,
|
||||||
'is_authenticated' => false,
|
'is_authenticated' => false,
|
||||||
'conversation_id' => null,
|
'conversation_id' => null,
|
||||||
@@ -146,21 +146,21 @@ abstract class WebSocketBase implements MessageComponentInterface
|
|||||||
try {
|
try {
|
||||||
// 优先使用 uniacid 参数,与 Kefu.php 保持一致
|
// 优先使用 uniacid 参数,与 Kefu.php 保持一致
|
||||||
$site_id = $data['uniacid'] ?? $data['site_id'] ?? null;
|
$site_id = $data['uniacid'] ?? $data['site_id'] ?? null;
|
||||||
$member_id = $data['member_id'] ?? null;
|
$user_id = $data['user_id'] ?? null;
|
||||||
$token = $data['token'] ?? null;
|
$token = $data['token'] ?? null;
|
||||||
|
|
||||||
if (empty($site_id) || empty($member_id) || empty($token)) {
|
if (empty($site_id) || empty($user_id) || empty($token)) {
|
||||||
throw new \Exception('Missing authentication parameters');
|
throw new \Exception('Missing authentication parameters');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 子类可以重写此方法来实现更严格的认证逻辑
|
// 子类可以重写此方法来实现更严格的认证逻辑
|
||||||
$devMode = true; // 开发模式下,关闭严格认证
|
$devMode = true; // 开发模式下,关闭严格认证
|
||||||
if (!$devMode) {
|
if (!$devMode) {
|
||||||
$this->doAuth($conn, $site_id, $member_id, $token);
|
$this->doAuth($conn, $site_id, $user_id, $token);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->clientData[$conn->resourceId]['site_id'] = $site_id;
|
$this->clientData[$conn->resourceId]['site_id'] = $site_id;
|
||||||
$this->clientData[$conn->resourceId]['member_id'] = $member_id;
|
$this->clientData[$conn->resourceId]['user_id'] = $user_id;
|
||||||
$this->clientData[$conn->resourceId]['token'] = $token;
|
$this->clientData[$conn->resourceId]['token'] = $token;
|
||||||
$this->clientData[$conn->resourceId]['is_authenticated'] = true;
|
$this->clientData[$conn->resourceId]['is_authenticated'] = true;
|
||||||
|
|
||||||
@@ -174,10 +174,10 @@ abstract class WebSocketBase implements MessageComponentInterface
|
|||||||
* 实际的认证逻辑,子类可以重写此方法
|
* 实际的认证逻辑,子类可以重写此方法
|
||||||
* @param ConnectionInterface $conn
|
* @param ConnectionInterface $conn
|
||||||
* @param int $site_id
|
* @param int $site_id
|
||||||
* @param int $member_id
|
* @param int $user_id
|
||||||
* @param string $token
|
* @param string $token
|
||||||
*/
|
*/
|
||||||
protected function doAuth(ConnectionInterface $conn, $site_id, $member_id, $token)
|
protected function doAuth(ConnectionInterface $conn, $site_id, $user_id, $token)
|
||||||
{
|
{
|
||||||
// 默认实现,子类应该重写此方法
|
// 默认实现,子类应该重写此方法
|
||||||
// 这里可以添加更严格的认证逻辑,例如验证token的有效性
|
// 这里可以添加更严格的认证逻辑,例如验证token的有效性
|
||||||
|
|||||||
Reference in New Issue
Block a user