Files
fe.ems.vue3/src/store/modules/locked.ts
2024-01-17 20:25:33 +08:00

54 lines
1.3 KiB
TypeScript

import { getSysConf } from '@/api';
import { CACHE_LOCAL_LOCK } from '@/constants/cache-keys-constants';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { localGet, localSet } from '@/utils/cache-local-utils';
import { defineStore } from 'pinia';
/**锁屏信息类型 */
type Locked = {
/**锁定状态 */
isLocked: boolean;
/**锁屏类型 */
lockType: 'lock' | 'reload';
/**超时锁屏时间,秒*/
lockTimeout: number;
};
const useLockedStore = defineStore('locked', {
state: (): Locked => ({
isLocked: localGet(CACHE_LOCAL_LOCK) === 'true',
lockType: 'lock',
lockTimeout: 0,
}),
getters: {},
actions: {
// 重启等待-轮询
async relaodWait() {
const res = await getSysConf();
if (res.code === RESULT_CODE_SUCCESS && res.data) {
console.log(res);
this.fnLock('lock', false);
} else {
// 延迟5秒
setTimeout(() => {
this.relaodWait();
}, 5_000);
}
},
// 设置锁定
async fnLock(type: 'lock' | 'reload', v: boolean) {
this.lockType = type;
this.isLocked = v;
localSet(CACHE_LOCAL_LOCK, `${v}`);
if (type === 'reload') {
// 延迟5秒
setTimeout(() => {
this.relaodWait();
}, 5_000);
}
},
},
});
export default useLockedStore;