2024-08-20 12:11:33 +00:00
|
|
|
import { computed, onMounted, onUnmounted } from 'vue'
|
|
|
|
import { useStore } from 'vuex'
|
|
|
|
import { useRouter, useRoute } from 'vue-router'
|
2024-08-22 08:50:27 +00:00
|
|
|
import { getClientHello } from 'services/ukeyAuth.js'
|
|
|
|
import { replaceToken } from 'services'
|
2024-08-23 08:45:57 +00:00
|
|
|
import { getToken, getTrxToken, getUkeyPassword } from 'utils/auth'
|
2024-08-20 12:11:33 +00:00
|
|
|
|
|
|
|
export default function () {
|
|
|
|
const store = useStore()
|
|
|
|
const router = useRouter()
|
|
|
|
const route = useRoute()
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
store.commit('SET_OPERATETIME')
|
|
|
|
let lockData = localStorage.getItem('lockData')
|
|
|
|
if (lockData) {
|
|
|
|
lockData = JSON.parse(lockData)
|
|
|
|
if ((lockData as any).isLock) router.push({ name: 'LockMe' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init()
|
2024-08-22 08:50:27 +00:00
|
|
|
// 获取最新token
|
|
|
|
async function getLastToken() {
|
|
|
|
const token = getToken()
|
2024-08-23 08:45:57 +00:00
|
|
|
const trxToken = getTrxToken()
|
2024-08-26 09:25:36 +00:00
|
|
|
const params: any = {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
if (trxToken) {
|
|
|
|
params.trxToken = trxToken
|
|
|
|
}
|
2024-08-30 07:59:03 +00:00
|
|
|
console.log('replaceToken开始, 传参:', params)
|
2024-08-26 09:25:36 +00:00
|
|
|
const res = await replaceToken(params)
|
2024-08-30 07:59:03 +00:00
|
|
|
console.log('replaceToken返回', res)
|
2024-08-22 08:50:27 +00:00
|
|
|
if (!res.success) {
|
|
|
|
clearTimer()
|
|
|
|
store.dispatch('permission/ResetRoutes')
|
|
|
|
}
|
|
|
|
}
|
2024-08-20 12:11:33 +00:00
|
|
|
const operateTime = computed(() => store.state.app.operateTime)
|
|
|
|
const lockScreenTime = computed(() => store.getters.systemConfig.lockScreenTime)
|
|
|
|
|
|
|
|
let timer: any = 0
|
|
|
|
function clearTimer() {
|
|
|
|
clearInterval(timer)
|
|
|
|
timer = 0
|
|
|
|
}
|
|
|
|
onMounted(() => {
|
|
|
|
timer = setInterval(() => {
|
2024-08-22 08:50:27 +00:00
|
|
|
getLastToken()
|
2024-08-20 12:11:33 +00:00
|
|
|
// 锁屏
|
|
|
|
const interval = 1000 * 60 * Number(lockScreenTime.value)
|
|
|
|
// checkUserStatus()
|
|
|
|
if (interval && new Date().getTime() - operateTime.value >= interval) {
|
|
|
|
lockScreen()
|
|
|
|
}
|
2024-08-22 08:50:27 +00:00
|
|
|
// 每 20s 在线检测 ueky 是否存在
|
|
|
|
if (getUkeyPassword()) {
|
|
|
|
getClientHello(getUkeyPassword()).then((checkRes: any) => {
|
2024-08-30 09:43:17 +00:00
|
|
|
console.log('在线检测 ueky 是否存在, ', checkRes)
|
2024-08-22 08:50:27 +00:00
|
|
|
if (checkRes.result !== 0) {
|
|
|
|
store.dispatch('permission/ResetRoutes')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-08-20 12:11:33 +00:00
|
|
|
}, 1000 * 20)
|
|
|
|
})
|
|
|
|
onUnmounted(clearTimer)
|
|
|
|
// 锁屏
|
|
|
|
function lockScreen() {
|
|
|
|
localStorage.setItem(
|
|
|
|
'lockData',
|
|
|
|
JSON.stringify({
|
|
|
|
path: route.fullPath,
|
|
|
|
isLock: true
|
|
|
|
})
|
|
|
|
)
|
|
|
|
router.push({ name: 'LockMe' })
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
lockScreen
|
|
|
|
}
|
|
|
|
}
|