Blame view

src/views/sys/exception/Exception.tsx 3.43 KB
陈文彬 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import type { PropType } from 'vue';

import { Result, Button } from 'ant-design-vue';
import { defineComponent, ref, computed, unref } from 'vue';

import { ExceptionEnum } from '/@/enums/exceptionEnum';

import netWorkImg from '/@/assets/images/exception/net-work.png';
import notDataImg from '/@/assets/images/no-data.png';

import { useRoute } from 'vue-router';

import { useGo, useRedo } from '/@/hooks/web/usePage';
import { PageEnum } from '/@/enums/pageEnum';
16
import './exception.less';
陈文彬 authored
17
18
19
20
21
22
interface MapValue {
  title: string;
  subTitle: string;
  btnText?: string;
  icon?: string;
  handler?: Fn;
23
  status?: string;
陈文彬 authored
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
}

export default defineComponent({
  name: 'ErrorPage',
  props: {
    // 状态码
    status: {
      type: Number as PropType<number>,
      default: ExceptionEnum.PAGE_NOT_FOUND,
    },

    title: {
      type: String as PropType<string>,
    },

    subTitle: {
      type: String as PropType<string>,
    },

    full: {
      type: Boolean as PropType<boolean>,
      default: false,
    },
  },
  setup(props) {
    const statusMapRef = ref(new Map<string | number, MapValue>());
    const { query } = useRoute();
    const go = useGo();
    const redo = useRedo();
    const getStatus = computed(() => {
      const { status: routeStatus } = query;
      const { status } = props;
      return Number(routeStatus) || status;
    });

    const getMapValue = computed(
      (): MapValue => {
        return unref(statusMapRef).get(unref(getStatus)) as MapValue;
      }
    );
65
66
67
68
69
70
71
72
    unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_ACCESS, {
      title: '403',
      status: `${ExceptionEnum.PAGE_NOT_ACCESS}`,
      subTitle: `Sorry, you don't have access to this page.!`,
      btnText: props.full ? 'Back Login' : 'Back Home',
      handler: () => (props.full ? go(PageEnum.BASE_LOGIN) : go()),
    });
陈文彬 authored
73
74
    unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_FOUND, {
      title: '404',
75
76
77
      status: `${ExceptionEnum.PAGE_NOT_FOUND}`,
      subTitle: `Sorry, the page you visited does not exist.`,
      btnText: props.full ? 'Back Login' : 'Back Home',
陈文彬 authored
78
79
80
81
82
      handler: () => (props.full ? go(PageEnum.BASE_LOGIN) : go()),
    });

    unref(statusMapRef).set(ExceptionEnum.ERROR, {
      title: '500',
83
84
85
      status: `${ExceptionEnum.ERROR}`,
      subTitle: `Sorry, the server is reporting an error.`,
      btnText: 'Back Home',
陈文彬 authored
86
87
88
89
      handler: () => go(),
    });

    unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_DATA, {
90
      title: 'No data on the current page',
陈文彬 authored
91
      subTitle: '',
92
      btnText: 'Refresh',
陈文彬 authored
93
94
95
96
97
      handler: () => redo(),
      icon: notDataImg,
    });

    unref(statusMapRef).set(ExceptionEnum.NET_WORK_ERROR, {
98
99
100
      title: 'Network Error',
      subTitle: 'Sorry,Your network connection has been disconnected, please check your network!',
      btnText: 'Refresh',
陈文彬 authored
101
102
103
104
105
      handler: () => redo(),
      icon: netWorkImg,
    });

    return () => {
106
      const { title, subTitle, btnText, icon, handler, status } = unref(getMapValue) || {};
陈文彬 authored
107
108
      return (
        <Result
109
          class="exception "
110
          status={status as any}
陈文彬 authored
111
112
113
114
115
116
117
118
119
120
          title={props.title || title}
          sub-title={props.subTitle || subTitle}
        >
          {{
            extra: () =>
              btnText && (
                <Button type="primary" onClick={handler}>
                  {() => btnText}
                </Button>
              ),
121
            icon: () => (icon ? <img src={icon} /> : null),
陈文彬 authored
122
123
124
125
126
127
          }}
        </Result>
      );
    };
  },
});