update open live page

This commit is contained in:
2023-11-15 14:38:58 +08:00
parent 8e110956a4
commit f117f11407
11 changed files with 626 additions and 137 deletions

View File

@@ -4,12 +4,14 @@ import { QueryPostAPI } from '@/api/query'
import { ref } from 'vue'
import { useLocalStorage } from '@vueuse/core'
import { createDiscreteApi } from 'naive-ui'
import { isSameDay } from 'date-fns'
export const ACCOUNT = ref<AccountInfo>()
export const isLoadingAccount = ref(true)
const { message } = createDiscreteApi(['message'])
const cookie = useLocalStorage('JWT_Token', '')
const cookieRefreshDate = useLocalStorage('JWT_Token_Last_Refresh', Date.now())
export async function GetSelfAccount() {
if (cookie.value) {
@@ -18,6 +20,9 @@ export async function GetSelfAccount() {
ACCOUNT.value = result.data
isLoadingAccount.value = false
console.log('[vtsuru] 已获取账户信息')
if (!isSameDay(new Date(), cookieRefreshDate.value)) {
refreshCookie()
}
return result.data
} else if (result.code == 401) {
localStorage.removeItem('JWT_Token')
@@ -25,7 +30,7 @@ export async function GetSelfAccount() {
message.error('Cookie 已失效, 需要重新登陆')
setTimeout(() => {
location.reload()
}, 1500);
}, 1500)
} else {
console.warn('[vtsuru] ' + result.message)
message.error(result.message)
@@ -33,6 +38,15 @@ export async function GetSelfAccount() {
}
isLoadingAccount.value = false
}
function refreshCookie() {
QueryPostAPI<string>(`${ACCOUNT_API_URL}refresh-token`).then((data) => {
if (data.code == 200) {
cookie.value = data.data
cookieRefreshDate.value = Date.now()
console.log('[vtsuru] 已刷新Cookie')
}
})
}
export function useAccount() {
return ACCOUNT
}