You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

60 lines
1.5 KiB

import { defineStore } from 'pinia'
import { useUserStore } from './user'
import type { LockInfo } from '@/types/store'
import { LOCK_INFO_KEY } from '@/enums/cacheEnum'
import { Persistent } from '@/utils/cache/persistent'
interface LockState {
lockInfo: Nullable<LockInfo>
}
export const useLockStore = defineStore('app-lock', {
state: (): LockState => ({
lockInfo: Persistent.getLocal(LOCK_INFO_KEY),
}),
getters: {
getLockInfo(state): Nullable<LockInfo> {
return state.lockInfo
},
},
actions: {
setLockInfo(info: LockInfo) {
this.lockInfo = Object.assign({}, this.lockInfo, info)
Persistent.setLocal(LOCK_INFO_KEY, this.lockInfo, true)
},
resetLockInfo() {
Persistent.removeLocal(LOCK_INFO_KEY, true)
this.lockInfo = null
},
// Unlock
async unLock(password?: string) {
const userStore = useUserStore()
if (this.lockInfo?.pwd === password) {
this.resetLockInfo()
return true
}
const tryLogin = async () => {
// TODO 滑块验证码
try {
const username = userStore.getUserInfo?.user.nickname
const res = await userStore.login({
username,
password: password!,
goHome: false,
mode: 'none',
captchaVerification: '',
})
if (res)
this.resetLockInfo()
return res
}
catch (error) {
return false
}
}
return await tryLogin()
},
},
})