Spaces:
Sleeping
Sleeping
File size: 4,483 Bytes
6e41657 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | <script setup lang="ts">
import { formatDistance } from 'date-fns';
import { request } from '#shared/utils/request';
import LoginModal from '~/components/modal/Login.vue';
import StorageUsage from '~/components/StorageUsage.vue';
import { IMAGE_PROXY } from '~/config';
import type { LogoutResponse } from '~/types/types';
const loginAccount = useLoginAccount();
const modal = useModal();
const now = ref(new Date());
const distance = computed(() => {
return (
loginAccount.value &&
formatDistance(new Date(loginAccount.value.expires), now.value, {
includeSeconds: true,
locale: {
formatDistance: function (token, count, options) {
if (now.value >= new Date(loginAccount.value.expires)) {
window.clearInterval(timer);
setTimeout(() => {
loginAccount.value = null;
}, 0);
return '已过期';
}
switch (token) {
case 'aboutXHours':
return '大约' + count + '个小时';
case 'aboutXMonths':
return '大约' + count + '个月';
case 'aboutXWeeks':
return '大约' + count + '周';
case 'aboutXYears':
return '大约' + count + '年';
case 'lessThanXMinutes':
return '小于' + count + '分钟';
case 'almostXYears':
return '接近' + count + '年';
case 'halfAMinute':
return '半分钟';
case 'lessThanXSeconds':
return '小于' + count + '秒';
case 'overXYears':
return '超过' + count + '年';
case 'xDays':
return count + '天';
case 'xHours':
return count + '个小时';
case 'xMinutes':
return count + '分钟';
case 'xMonths':
return count + '个月';
case 'xSeconds':
return count + '秒';
case 'xWeeks':
return count + '周';
case 'xYears':
return count + '年';
default:
return 'unknown';
}
},
},
})
);
});
const warning = computed(() => {
const value = distance.value;
return value === '已过期' || value.includes('分钟') || value.includes('秒');
});
function login() {
modal.open(LoginModal);
}
const logoutBtnLoading = ref(false);
async function logout() {
logoutBtnLoading.value = true;
const { statusCode, statusText } = await request<LogoutResponse>('/api/web/mp/logout');
if (statusCode === 200) {
loginAccount.value = null;
} else {
alert(statusText);
}
logoutBtnLoading.value = false;
}
let timer: number;
onMounted(() => {
timer = window.setInterval(() => {
now.value = new Date();
}, 1000);
});
onUnmounted(() => {
window.clearInterval(timer);
});
</script>
<template>
<footer class="flex flex-col space-y-2 pt-3 border-t dark:border-slate-600">
<div v-if="loginAccount" class="space-y-3">
<div class="flex items-center space-x-2">
<img
v-if="loginAccount.avatar"
:src="IMAGE_PROXY + loginAccount.avatar"
alt=""
class="rounded-full size-10 ring-1 ring-gray-300"
/>
<UTooltip
v-if="loginAccount.nickname"
class="flex-1 overflow-hidden"
:popper="{ placement: 'top-start', offsetDistance: 16 }"
>
<template #text>
<span>{{ loginAccount.nickname }}</span>
</template>
<span class="whitespace-nowrap text-ellipsis overflow-hidden">{{ loginAccount.nickname }}</span>
</UTooltip>
<UButton
icon="i-heroicons-arrow-left-start-on-rectangle-16-solid"
:loading="logoutBtnLoading"
class="bg-slate-10 hover:bg-rose-500 disabled:bg-rose-500"
@click="logout"
>退出
</UButton>
</div>
<div class="text-sm">
<span>登录信息过期时间还剩: </span>
<span class="font-mono" :class="warning ? 'text-rose-500' : 'text-green-500'">{{ distance }}</span>
</div>
</div>
<div v-else>
<UButton color="gray" variant="solid" @click="login">登录公众号</UButton>
</div>
<StorageUsage />
</footer>
</template>
|