Skip to content

Commit 7411dbc

Browse files
feat: Add Error component
1 parent 1492f91 commit 7411dbc

File tree

14 files changed

+16154
-17
lines changed

14 files changed

+16154
-17
lines changed

src/assets/svgs/403.svg

Lines changed: 5290 additions & 0 deletions
Loading

src/assets/svgs/404.svg

Lines changed: 5287 additions & 0 deletions
Loading

src/assets/svgs/500.svg

Lines changed: 5389 additions & 0 deletions
Loading

src/components/Error/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Error from './src/Error.vue'
2+
3+
export { Error }

src/components/Error/src/Error.vue

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<script setup lang="ts">
2+
import pageError from '@/assets/svgs/404.svg'
3+
import networkError from '@/assets/svgs/500.svg'
4+
import noPermission from '@/assets/svgs/403.svg'
5+
import { propTypes } from '@/utils/propTypes'
6+
import { useI18n } from '@/hooks/web/useI18n'
7+
import { ElButton } from 'element-plus'
8+
9+
interface ErrorMap {
10+
url: string
11+
message: string
12+
buttonText: string
13+
}
14+
15+
const { t } = useI18n()
16+
17+
const errorMap: {
18+
[key: string]: ErrorMap
19+
} = {
20+
'404': {
21+
url: pageError,
22+
message: t('error.pageError'),
23+
buttonText: t('error.returnToHome')
24+
},
25+
'500': {
26+
url: networkError,
27+
message: t('error.networkError'),
28+
buttonText: t('error.returnToHome')
29+
},
30+
'403': {
31+
url: noPermission,
32+
message: t('error.noPermission'),
33+
buttonText: t('error.returnToHome')
34+
}
35+
}
36+
37+
const props = defineProps({
38+
type: propTypes.string.validate((v: string) => ['404', '500', '403'].includes(v)).def('404')
39+
})
40+
41+
const emit = defineEmits(['errorClick'])
42+
43+
const btnClick = () => {
44+
emit('errorClick', props.type)
45+
}
46+
</script>
47+
48+
<template>
49+
<div class="flex justify-center">
50+
<div class="text-center">
51+
<img width="350" :src="errorMap[type].url" alt="" />
52+
<div class="text-14px text-[var(--el-color-info)]">{{ errorMap[type].message }}</div>
53+
<div class="mt-20px">
54+
<ElButton type="primary" @click="btnClick">{{ errorMap[type].buttonText }}</ElButton>
55+
</div>
56+
</div>
57+
</div>
58+
</template>

src/locales/en.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export default {
4141
delNoData: 'Please select the data to delete',
4242
delSuccess: 'Deleted successfully'
4343
},
44+
error: {
45+
noPermission: `Sorry, you don't have permission to access this page.`,
46+
pageError: 'Sorry, the page you visited does not exist.',
47+
networkError: 'Sorry, the server reported an error.',
48+
returnToHome: 'Return to home'
49+
},
4450
setting: {
4551
projectSetting: 'Project setting',
4652
theme: 'Theme',
@@ -119,7 +125,8 @@ export default {
119125
examplePage: 'Example page',
120126
exampleAdd: 'Example page - add',
121127
exampleEdit: 'Example page - edit',
122-
exampleDetail: 'Example page - detail'
128+
exampleDetail: 'Example page - detail',
129+
errorPage: 'Error page'
123130
},
124131
analysis: {
125132
newUser: 'New user',

src/locales/zh-CN.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export default {
4141
delNoData: '请选择需要删除的数据',
4242
delSuccess: '删除成功'
4343
},
44+
error: {
45+
noPermission: `抱歉,您无权访问此页面。`,
46+
pageError: '抱歉,您访问的页面不存在。',
47+
networkError: '抱歉,服务器报告错误。',
48+
returnToHome: '返回首页'
49+
},
4450
setting: {
4551
projectSetting: '项目配置',
4652
theme: '主题',
@@ -119,7 +125,8 @@ export default {
119125
examplePage: '综合示例 - 页面',
120126
exampleAdd: '综合示例 - 新增',
121127
exampleEdit: '综合示例 - 编辑',
122-
exampleDetail: '综合示例 - 详情'
128+
exampleDetail: '综合示例 - 详情',
129+
errorPage: '错误页面'
123130
},
124131
analysis: {
125132
newUser: '新增用户',

src/permission.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ router.beforeEach(async (to, from, next) => {
2727
next({ path: '/' })
2828
} else {
2929
if (permissionStore.getIsAddRouters) {
30-
next()
30+
to.path === '/' ? next({ path: permissionStore.addRouters[0]?.path as string }) : next()
3131
return
3232
}
3333
await permissionStore.generateRoutes()
@@ -42,7 +42,7 @@ router.beforeEach(async (to, from, next) => {
4242
}
4343
} else {
4444
if (whiteList.indexOf(to.path) !== -1) {
45-
next()
45+
to.path === '/' ? next({ path: permissionStore.addRouters[0]?.path as string }) : next()
4646
} else {
4747
next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
4848
}

src/router/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,43 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
401401
}
402402
}
403403
]
404+
},
405+
{
406+
path: '/error',
407+
component: Layout,
408+
redirect: '/error/404',
409+
name: 'Error',
410+
meta: {
411+
title: t('router.errorPage'),
412+
icon: 'ci:error',
413+
alwaysShow: true
414+
},
415+
children: [
416+
{
417+
path: '404',
418+
component: () => import('@/views/Error/404.vue'),
419+
name: '404',
420+
meta: {
421+
title: '404'
422+
}
423+
},
424+
{
425+
path: '403',
426+
component: () => import('@/views/Error/403.vue'),
427+
name: '403',
428+
meta: {
429+
title: '403'
430+
}
431+
},
432+
{
433+
path: '500',
434+
component: () => import('@/views/Error/500.vue'),
435+
name: '500',
436+
meta: {
437+
title: '500'
438+
}
439+
}
440+
]
404441
}
405442
]
406443

src/views/Error/403.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script setup lang="ts">
2+
import { Error } from '@/components/Error'
3+
import { usePermissionStore } from '@/store/modules/permission'
4+
import { useRouter } from 'vue-router'
5+
6+
const { push } = useRouter()
7+
8+
const permissionStore = usePermissionStore()
9+
10+
const errorClick = () => {
11+
push(permissionStore.addRouters[0]?.path as string)
12+
}
13+
</script>
14+
15+
<template>
16+
<Error type="403" @errorClick="errorClick" />
17+
</template>

src/views/Error/404.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script setup lang="ts">
2+
import { Error } from '@/components/Error'
3+
import { usePermissionStore } from '@/store/modules/permission'
4+
import { useRouter } from 'vue-router'
5+
6+
const { push } = useRouter()
7+
8+
const permissionStore = usePermissionStore()
9+
10+
const errorClick = () => {
11+
push(permissionStore.addRouters[0]?.path as string)
12+
}
13+
</script>
14+
15+
<template>
16+
<Error @errorClick="errorClick" />
17+
</template>

src/views/Error/500.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script setup lang="ts">
2+
import { Error } from '@/components/Error'
3+
import { usePermissionStore } from '@/store/modules/permission'
4+
import { useRouter } from 'vue-router'
5+
6+
const { push } = useRouter()
7+
8+
const permissionStore = usePermissionStore()
9+
10+
const errorClick = () => {
11+
push(permissionStore.addRouters[0]?.path as string)
12+
}
13+
</script>
14+
15+
<template>
16+
<Error type="500" @errorClick="errorClick" />
17+
</template>

src/views/Example/Dialog/ExampleDialog.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const save = async () => {
186186
</ContentWrap>
187187

188188
<Dialog v-model="dialogVisible" :title="dialogTitle">
189-
<Write v-if="actionType === 'edit'" ref="writeRef" :current-row="tableObject.currentRow" />
189+
<Write v-if="actionType !== 'detail'" ref="writeRef" :current-row="tableObject.currentRow" />
190190

191191
<Detail v-if="actionType === 'detail'" :current-row="tableObject.currentRow" />
192192

src/views/Example/Dialog/components/Write.vue

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { Form } from '@/components/Form'
33
import { useForm } from '@/hooks/web/useForm'
4-
import { PropType, reactive } from 'vue'
4+
import { PropType, reactive, watch } from 'vue'
55
import { TableData } from '@/api/table/types'
66
import { useI18n } from '@/hooks/web/useI18n'
77
import { required } from '@/utils/formRules'
@@ -113,17 +113,25 @@ const { register, methods, elFormRef } = useForm({
113113
schema
114114
})
115115
116-
if (props.currentRow) {
117-
const { setValues, setSchema } = methods
118-
setValues(props.currentRow)
119-
setSchema([
120-
{
121-
field: 'content',
122-
path: 'componentProps.defaultHtml',
123-
value: props.currentRow.content
124-
}
125-
])
126-
}
116+
watch(
117+
() => props.currentRow,
118+
(currentRow) => {
119+
if (!currentRow) return
120+
const { setValues, setSchema } = methods
121+
setValues(currentRow)
122+
setSchema([
123+
{
124+
field: 'content',
125+
path: 'componentProps.defaultHtml',
126+
value: currentRow.content
127+
}
128+
])
129+
},
130+
{
131+
deep: true,
132+
immediate: true
133+
}
134+
)
127135
128136
defineExpose({
129137
elFormRef,

0 commit comments

Comments
 (0)