admin-vben5/packages/effects/common-ui/src/fallback/fallback.vue

164 lines
3.7 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script setup lang="ts">
2024-06-08 20:14:04 +08:00
import type { FallbackProps } from './fallback';
2024-06-09 18:58:30 +08:00
import { computed, defineAsyncComponent } from 'vue';
2024-05-19 21:20:42 +08:00
import { useRouter } from 'vue-router';
import { IcRoundArrowBackIosNew, IcRoundRefresh } from '@vben-core/icons';
import { $t } from '@vben-core/locales';
2024-06-08 19:49:06 +08:00
import { VbenButton } from '@vben-core/shadcn-ui';
2024-06-08 20:14:04 +08:00
interface Props extends FallbackProps {}
2024-05-19 21:20:42 +08:00
defineOptions({
name: 'Fallback',
});
const props = withDefaults(defineProps<Props>(), {
description: '',
homePath: '/',
image: '',
2024-06-08 20:14:04 +08:00
showBack: true,
2024-07-14 13:48:47 +08:00
status: 'coming-soon',
2024-05-19 21:20:42 +08:00
title: '',
});
2024-06-09 18:58:30 +08:00
const Icon403 = defineAsyncComponent(() => import('./icons/icon-403.vue'));
const Icon404 = defineAsyncComponent(() => import('./icons/icon-404.vue'));
const Icon500 = defineAsyncComponent(() => import('./icons/icon-500.vue'));
2024-06-29 15:41:10 +08:00
const IconHello = defineAsyncComponent(
2024-07-14 13:48:47 +08:00
() => import('./icons/icon-coming-soon.vue'),
2024-06-29 15:41:10 +08:00
);
2024-06-09 18:58:30 +08:00
const IconOffline = defineAsyncComponent(
() => import('./icons/icon-offline.vue'),
);
2024-05-19 21:20:42 +08:00
const titleText = computed(() => {
2024-06-08 20:14:04 +08:00
if (props.title) {
return props.title;
}
switch (props.status) {
case '403': {
return $t('fallback.forbidden');
}
2024-06-16 13:43:33 +08:00
case '404': {
return $t('fallback.pageNotFound');
2024-06-16 13:43:33 +08:00
}
2024-06-08 20:14:04 +08:00
case '500': {
return $t('fallback.internalError');
2024-06-08 20:14:04 +08:00
}
2024-06-09 18:58:30 +08:00
case 'offline': {
return $t('fallback.offlineError');
2024-06-09 18:58:30 +08:00
}
2024-07-14 13:48:47 +08:00
case 'coming-soon': {
return $t('fallback.comingSoon');
2024-06-16 15:45:15 +08:00
}
2024-06-08 20:14:04 +08:00
default: {
2024-06-16 13:43:33 +08:00
return '';
2024-06-08 20:14:04 +08:00
}
}
2024-05-19 21:20:42 +08:00
});
const descText = computed(() => {
2024-06-08 20:14:04 +08:00
if (props.description) {
return props.description;
}
switch (props.status) {
case '403': {
return $t('fallback.forbiddenDesc');
2024-06-08 20:14:04 +08:00
}
2024-06-16 13:43:33 +08:00
case '404': {
return $t('fallback.pageNotFoundDesc');
2024-06-16 13:43:33 +08:00
}
2024-06-08 20:14:04 +08:00
case '500': {
return $t('fallback.internalErrorDesc');
2024-06-08 20:14:04 +08:00
}
2024-06-09 18:58:30 +08:00
case 'offline': {
return $t('fallback.offlineErrorDesc');
2024-06-09 18:58:30 +08:00
}
2024-06-08 20:14:04 +08:00
default: {
2024-06-16 13:43:33 +08:00
return '';
2024-06-08 20:14:04 +08:00
}
}
});
const fallbackIcon = computed(() => {
switch (props.status) {
case '403': {
return Icon403;
}
2024-06-16 13:43:33 +08:00
case '404': {
return Icon404;
}
2024-06-08 20:14:04 +08:00
case '500': {
return Icon500;
}
2024-06-09 18:58:30 +08:00
case 'offline': {
return IconOffline;
}
2024-07-14 13:48:47 +08:00
case 'coming-soon': {
2024-06-16 13:43:33 +08:00
return IconHello;
}
2024-06-08 20:14:04 +08:00
default: {
2024-06-16 13:43:33 +08:00
return null;
2024-06-08 20:14:04 +08:00
}
}
2024-05-19 21:20:42 +08:00
});
2024-06-16 13:43:33 +08:00
const showBack = computed(() => {
return ['403', '404'].includes(props.status);
});
const showRefresh = computed(() => {
return ['500', 'offline'].includes(props.status);
});
2024-05-19 21:20:42 +08:00
const { push } = useRouter();
// 返回首页
function back() {
push(props.homePath);
}
2024-06-16 13:43:33 +08:00
function refresh() {
location.reload();
}
2024-05-19 21:20:42 +08:00
</script>
<template>
2024-06-08 20:14:04 +08:00
<div class="flex size-full flex-col items-center justify-center duration-300">
2024-05-19 21:20:42 +08:00
<img v-if="image" :src="image" class="md:1/3 w-1/2 lg:w-1/4" />
2024-06-16 13:43:33 +08:00
<component
:is="fallbackIcon"
v-else-if="fallbackIcon"
class="md:1/3 h-1/3 w-1/2 lg:w-1/4"
/>
2024-05-28 23:53:15 +08:00
<div class="flex-col-center">
2024-06-16 13:43:33 +08:00
<slot v-if="$slots.title" name="title"></slot>
2024-06-08 20:14:04 +08:00
<p
2024-06-16 13:43:33 +08:00
v-else-if="titleText"
2024-06-16 23:25:28 +08:00
class="text-foreground mt-8 text-2xl md:text-3xl lg:text-4xl"
2024-06-08 20:14:04 +08:00
>
2024-05-19 21:20:42 +08:00
{{ titleText }}
</p>
2024-06-16 13:43:33 +08:00
<slot v-if="$slots.describe" name="describe"></slot>
2024-06-08 20:14:04 +08:00
<p
2024-06-16 13:43:33 +08:00
v-else-if="descText"
2024-06-16 23:25:28 +08:00
class="text-muted-foreground md:text-md my-4 lg:text-lg"
2024-06-08 20:14:04 +08:00
>
2024-05-19 21:20:42 +08:00
{{ descText }}
</p>
2024-06-16 13:43:33 +08:00
<slot v-if="$slots.action" name="action"></slot>
<VbenButton v-else-if="showBack" size="lg" @click="back">
2024-05-19 21:20:42 +08:00
<IcRoundArrowBackIosNew class="mr-2" />
{{ $t('common.backToHome') }}
2024-05-19 21:20:42 +08:00
</VbenButton>
2024-06-16 13:43:33 +08:00
<VbenButton v-else-if="showRefresh" size="lg" @click="refresh">
<IcRoundRefresh class="mr-2" />
{{ $t('common.refresh') }}
</VbenButton>
2024-05-19 21:20:42 +08:00
</div>
</div>
</template>