up. 使用状态管理器管理设备

This commit is contained in:
u2nyakim 2025-08-29 10:09:03 +08:00
parent ed15d9ffd8
commit 64e91f1f5c
4 changed files with 64 additions and 8 deletions

View File

@ -38,7 +38,7 @@
import { ref } from 'vue'; import { ref } from 'vue';
import { WsEvent, wsEventManager } from '@/plugins/websocket'; import { WsEvent, wsEventManager } from '@/plugins/websocket';
import { clientScreenUnlock } from '@/api/layout'; import { clientScreenUnlock } from '@/api/layout';
import {useClientStore} from "@/store/modules/client"; import { LockScreenStatus, useClientStore } from "@/store/modules/client";
/** 设备状态管理器 */ /** 设备状态管理器 */
const clientStore = useClientStore(); const clientStore = useClientStore();
@ -55,7 +55,7 @@
clientScreenUnlock({ password: lockPassword.value }) clientScreenUnlock({ password: lockPassword.value })
.then(() => { .then(() => {
clientStore.setLockScreen(false) clientStore.setLockScreen(LockScreenStatus.UNLOCK)
lockPassword.value = ''; // lockPassword.value = ''; //
}) })
.catch((message) => { .catch((message) => {
@ -67,6 +67,6 @@
}; };
/** 事件订阅器 - 锁屏事件 */ /** 事件订阅器 - 锁屏事件 */
wsEventManager.subscribe(WsEvent.LOCK_CLIENT_SCREEN, () => { wsEventManager.subscribe(WsEvent.LOCK_CLIENT_SCREEN, () => {
clientStore.setLockScreen(true); clientStore.setLockScreen(LockScreenStatus.LOCK);
}); });
</script> </script>

View File

@ -2,20 +2,58 @@
* *
*/ */
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import {CLIENT_ID_CACHE_NAME} from "@/config/setting";
import { generateGUID } from "@/utils/common";
export enum LockScreenStatus {
LOCK = 1,
UNLOCK = 0
}
export interface ClientState { export interface ClientState {
/** 客户端ID */
clientId: string;
/** 客户端名称 */
clientName: string;
/** 客户端版本号 */
clientVersion: string;
/** 是否锁定会话 */
lockScreen: boolean; lockScreen: boolean;
} }
export const useClientStore = defineStore('client', { export const useClientStore = defineStore('client', {
state: (): ClientState => ({ state: (): ClientState => ({
/** 客户端ID */
clientId: getClientId(),
/** 客户端名称 */
clientName: 'PAdmin',
/** 客户端版本号 */
clientVersion: "1.0.0",
/** 锁定会话 */ /** 锁定会话 */
lockScreen: false, lockScreen: false,
}), }),
actions: { actions: {
setLockScreen(value: boolean) { /**
console.log("锁屏") *
this.lockScreen = value; * @param isLock
*/
setLockScreen(isLock: LockScreenStatus = LockScreenStatus.UNLOCK) {
this.lockScreen = isLock == 1;
}, },
} }
}); });
/**
* IDGUID格式
*/
function getClientId(): string {
let clientId = localStorage.getItem(CLIENT_ID_CACHE_NAME);
if (!clientId) {
clientId = generateGUID();
localStorage.setItem(CLIENT_ID_CACHE_NAME, clientId);
return clientId
}
return clientId
}

View File

@ -2,14 +2,19 @@ import type { Router } from 'vue-router';
import type { Action } from 'element-plus'; import type { Action } from 'element-plus';
import { ElMessageBox } from 'element-plus'; import { ElMessageBox } from 'element-plus';
import { removeToken } from '@/utils/token-util'; import { removeToken } from '@/utils/token-util';
import { uuid } from 'ele-admin-plus';
export function generateGUID() { export function generateGUID(): string {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => let guidStr = ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: number) =>
( (
c ^ c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16) ).toString(16)
); );
if (guidStr) {
return guidStr
}
return uuid()
} }
/** /**
* 退 * 退

View File

@ -4,6 +4,19 @@
import { TOKEN_CACHE_NAME, CLIENT_ID_CACHE_NAME } from '@/config/setting'; import { TOKEN_CACHE_NAME, CLIENT_ID_CACHE_NAME } from '@/config/setting';
import {generateGUID} from "@/utils/common"; import {generateGUID} from "@/utils/common";
export function getClientId() {
let clientId = localStorage.getItem(CLIENT_ID_CACHE_NAME);
if (!clientId) {
clientId = generateGUID();
localStorage.setItem(CLIENT_ID_CACHE_NAME, clientId);
}
return {
name: 'PAdmin',
id: clientId,
version: "1.0.0"
};
}
export function getClientInfo() { export function getClientInfo() {
let clientId = localStorage.getItem(CLIENT_ID_CACHE_NAME); let clientId = localStorage.getItem(CLIENT_ID_CACHE_NAME);
if (!clientId) { if (!clientId) {