up. ws update.

This commit is contained in:
扶桑花间 2025-08-28 22:40:30 +08:00
parent 64f9ffe310
commit a4768d49b0
4 changed files with 48 additions and 17 deletions

View File

@ -3,6 +3,7 @@
namespace app\command\admin;
use app\entity\SysUserClient;
use app\service\admin\LoginService;
use think\console\Command;
use think\console\Input;
@ -51,16 +52,30 @@ class Worker extends Command
if($messageJson) {
if($messageJson['event'] == 'bind_connect_id') {
$authorization = $messageJson['data']['token'] ?? '';
$clientId = $messageJson['data']['clientId'] ?? '';
$clientName = $messageJson['data']['clientName'] ?? '';
$clientVersion = $messageJson['data']['clientVersion'] ?? '';
$authorization = str_replace('Bearer ', '', $authorization);
$loginSrv = new LoginService();
$auth = $loginSrv->checkUserAccessToken($authorization);
var_dump("当前用户: ".$auth->userId);
// 用户信息
$connection->userId = $auth->userId;
// 客户端信息
$connection->clientId = $clientId;
$connection->clientName = $clientName;
$connection->clientVersion = $clientVersion;
// 绑定成功通知
$connection->send(json_encode(['event'=>'bind_connect_id_success','userId'=>$auth->userId]));
}
if ($messageJson['event'] == 'lock_client_screen') {
if($connection->userId) {
$lock_password = $messageJson['data']['lockPassword'] ?? '';
var_dump("锁定客户端/通知全部ws触发锁定操作");
SysUserClient::where([
'client_id' => $connection->clientId,
'client_name'=> $connection->clientName
])->update(['is_lock'=>1,'lock_password'=>$lock_password,'lock_time'=>date('Y-m-d H:i:s')]);
}
}
}

View File

@ -11,9 +11,16 @@
:close-on-press-escape="false"
:close-on-click-modal="false"
>
<el-input type="password" v-model="lockPassword" size="large" placeholder="请输入临时锁定密码/为空则使用默认使用账号密码"></el-input>
<div style="margin-top: 20px;">
<el-button class="ele-fluid" type="primary" @click="confirmLock">锁定客户端</el-button>
<el-input
type="password"
v-model="lockPassword"
size="large"
placeholder="请输入临时锁定密码/为空则使用默认使用账号密码"
/>
<div style="margin-top: 20px">
<el-button class="ele-fluid" type="primary" @click="confirmLock"
>锁定客户端</el-button
>
</div>
</ele-modal>
</template>
@ -21,6 +28,7 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { useMobile } from '@/utils/use-mobile';
import { wsInstance, WsSendEventType } from '@/plugins/websocket';
/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });
@ -29,8 +37,11 @@ const visible = defineModel({ type: Boolean });
const { mobile } = useMobile();
const lockLoading = ref(false);
const lockPassword = ref("");
const lockPassword = ref('');
const confirmLock = () => {
lockLoading.value = true;
}
wsInstance.sendMessage(WsSendEventType.LOCK_CLIENT_SCREEN, {
lockPassword: lockPassword.value
});
};
</script>

View File

@ -6,7 +6,8 @@ export enum WsEvent {
}
export enum WsSendEventType {
BIND_CONNECT_ID = 'bind_connect_id'
BIND_CONNECT_ID = 'bind_connect_id',
LOCK_CLIENT_SCREEN = 'lock_client_screen',
}
export enum WsConnectionStatus {

View File

@ -9,7 +9,7 @@ import type { User } from '@/api/system/user/model';
import type { Menu } from '@/api/system/menu/model';
import type { DictionaryData } from '@/api/system/dictionary-data/model';
import { getUserInfo } from '@/api/layout';
import { getToken } from '@/utils/token-util';
import {getClientInfo, getToken} from '@/utils/token-util';
import {
WsEventContext,
wsEventManager,
@ -79,9 +79,13 @@ export const useUserStore = defineStore('user', {
wsEventManager.subscribe(WsEvent.CONNECTED, (sub: WsEventContext) => {
console.log('事件执行, 绑定连接和用户关系');
// 绑定用户到websocket的connect_id中
const clientInfo = getClientInfo();
wsInstance.sendMessage(WsSendEventType.BIND_CONNECT_ID, {
token: getToken(),
userId: result.userId
userId: result.userId,
clientId: clientInfo.id,
clientName: clientInfo.name,
clientVersion: clientInfo.version
});
});
return { menus, homePath };