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; namespace app\command\admin;
use app\entity\SysUserClient;
use app\service\admin\LoginService; use app\service\admin\LoginService;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
@ -51,16 +52,30 @@ class Worker extends Command
if($messageJson) { if($messageJson) {
if($messageJson['event'] == 'bind_connect_id') { if($messageJson['event'] == 'bind_connect_id') {
$authorization = $messageJson['data']['token'] ?? ''; $authorization = $messageJson['data']['token'] ?? '';
$clientId = $messageJson['data']['clientId'] ?? '';
$clientName = $messageJson['data']['clientName'] ?? '';
$clientVersion = $messageJson['data']['clientVersion'] ?? '';
$authorization = str_replace('Bearer ', '', $authorization); $authorization = str_replace('Bearer ', '', $authorization);
$loginSrv = new LoginService(); $loginSrv = new LoginService();
$auth = $loginSrv->checkUserAccessToken($authorization); $auth = $loginSrv->checkUserAccessToken($authorization);
var_dump("当前用户: ".$auth->userId); var_dump("当前用户: ".$auth->userId);
// 用户信息
$connection->userId = $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])); $connection->send(json_encode(['event'=>'bind_connect_id_success','userId'=>$auth->userId]));
} }
if ($messageJson['event'] == 'lock_client_screen') { if ($messageJson['event'] == 'lock_client_screen') {
if($connection->userId) { if($connection->userId) {
$lock_password = $messageJson['data']['lockPassword'] ?? '';
var_dump("锁定客户端/通知全部ws触发锁定操作"); 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,26 +11,37 @@
:close-on-press-escape="false" :close-on-press-escape="false"
:close-on-click-modal="false" :close-on-click-modal="false"
> >
<el-input type="password" v-model="lockPassword" size="large" placeholder="请输入临时锁定密码/为空则使用默认使用账号密码"></el-input> <el-input
<div style="margin-top: 20px;"> type="password"
<el-button class="ele-fluid" type="primary" @click="confirmLock">锁定客户端</el-button> v-model="lockPassword"
size="large"
placeholder="请输入临时锁定密码/为空则使用默认使用账号密码"
/>
<div style="margin-top: 20px">
<el-button class="ele-fluid" type="primary" @click="confirmLock"
>锁定客户端</el-button
>
</div> </div>
</ele-modal> </ele-modal>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from 'vue'; import { ref } from 'vue';
import { useMobile } from '@/utils/use-mobile'; import { useMobile } from '@/utils/use-mobile';
import { wsInstance, WsSendEventType } from '@/plugins/websocket';
/** 弹窗是否打开 */ /** 弹窗是否打开 */
const visible = defineModel({ type: Boolean }); const visible = defineModel({ type: Boolean });
/** 是否是移动端 */ /** 是否是移动端 */
const { mobile } = useMobile(); const { mobile } = useMobile();
const lockLoading = ref(false); const lockLoading = ref(false);
const lockPassword = ref(""); const lockPassword = ref('');
const confirmLock = ()=>{ const confirmLock = () => {
lockLoading.value = true; lockLoading.value = true;
} wsInstance.sendMessage(WsSendEventType.LOCK_CLIENT_SCREEN, {
lockPassword: lockPassword.value
});
};
</script> </script>

View File

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