Skip to content

Commit

Permalink
feat: Add Error component
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Feb 19, 2022
1 parent 1492f91 commit 7411dbc
Show file tree
Hide file tree
Showing 14 changed files with 16,154 additions and 17 deletions.
5,290 changes: 5,290 additions & 0 deletions src/assets/svgs/403.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5,287 changes: 5,287 additions & 0 deletions src/assets/svgs/404.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5,389 changes: 5,389 additions & 0 deletions src/assets/svgs/500.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/components/Error/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Error from './src/Error.vue'

export { Error }
58 changes: 58 additions & 0 deletions src/components/Error/src/Error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script setup lang="ts">
import pageError from '@/assets/svgs/404.svg'
import networkError from '@/assets/svgs/500.svg'
import noPermission from '@/assets/svgs/403.svg'
import { propTypes } from '@/utils/propTypes'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton } from 'element-plus'
interface ErrorMap {
url: string
message: string
buttonText: string
}
const { t } = useI18n()
const errorMap: {
[key: string]: ErrorMap
} = {
'404': {
url: pageError,
message: t('error.pageError'),
buttonText: t('error.returnToHome')
},
'500': {
url: networkError,
message: t('error.networkError'),
buttonText: t('error.returnToHome')
},
'403': {
url: noPermission,
message: t('error.noPermission'),
buttonText: t('error.returnToHome')
}
}
const props = defineProps({
type: propTypes.string.validate((v: string) => ['404', '500', '403'].includes(v)).def('404')
})
const emit = defineEmits(['errorClick'])
const btnClick = () => {
emit('errorClick', props.type)
}
</script>

<template>
<div class="flex justify-center">
<div class="text-center">
<img width="350" :src="errorMap[type].url" alt="" />
<div class="text-14px text-[var(--el-color-info)]">{{ errorMap[type].message }}</div>
<div class="mt-20px">
<ElButton type="primary" @click="btnClick">{{ errorMap[type].buttonText }}</ElButton>
</div>
</div>
</div>
</template>
9 changes: 8 additions & 1 deletion src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export default {
delNoData: 'Please select the data to delete',
delSuccess: 'Deleted successfully'
},
error: {
noPermission: `Sorry, you don't have permission to access this page.`,
pageError: 'Sorry, the page you visited does not exist.',
networkError: 'Sorry, the server reported an error.',
returnToHome: 'Return to home'
},
setting: {
projectSetting: 'Project setting',
theme: 'Theme',
Expand Down Expand Up @@ -119,7 +125,8 @@ export default {
examplePage: 'Example page',
exampleAdd: 'Example page - add',
exampleEdit: 'Example page - edit',
exampleDetail: 'Example page - detail'
exampleDetail: 'Example page - detail',
errorPage: 'Error page'
},
analysis: {
newUser: 'New user',
Expand Down
9 changes: 8 additions & 1 deletion src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export default {
delNoData: '请选择需要删除的数据',
delSuccess: '删除成功'
},
error: {
noPermission: `抱歉,您无权访问此页面。`,
pageError: '抱歉,您访问的页面不存在。',
networkError: '抱歉,服务器报告错误。',
returnToHome: '返回首页'
},
setting: {
projectSetting: '项目配置',
theme: '主题',
Expand Down Expand Up @@ -119,7 +125,8 @@ export default {
examplePage: '综合示例 - 页面',
exampleAdd: '综合示例 - 新增',
exampleEdit: '综合示例 - 编辑',
exampleDetail: '综合示例 - 详情'
exampleDetail: '综合示例 - 详情',
errorPage: '错误页面'
},
analysis: {
newUser: '新增用户',
Expand Down
4 changes: 2 additions & 2 deletions src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ router.beforeEach(async (to, from, next) => {
next({ path: '/' })
} else {
if (permissionStore.getIsAddRouters) {
next()
to.path === '/' ? next({ path: permissionStore.addRouters[0]?.path as string }) : next()
return
}
await permissionStore.generateRoutes()
Expand All @@ -42,7 +42,7 @@ router.beforeEach(async (to, from, next) => {
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
to.path === '/' ? next({ path: permissionStore.addRouters[0]?.path as string }) : next()
} else {
next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
}
Expand Down
37 changes: 37 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,43 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
}
}
]
},
{
path: '/error',
component: Layout,
redirect: '/error/404',
name: 'Error',
meta: {
title: t('router.errorPage'),
icon: 'ci:error',
alwaysShow: true
},
children: [
{
path: '404',
component: () => import('@/views/Error/404.vue'),
name: '404',
meta: {
title: '404'
}
},
{
path: '403',
component: () => import('@/views/Error/403.vue'),
name: '403',
meta: {
title: '403'
}
},
{
path: '500',
component: () => import('@/views/Error/500.vue'),
name: '500',
meta: {
title: '500'
}
}
]
}
]

Expand Down
17 changes: 17 additions & 0 deletions src/views/Error/403.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { Error } from '@/components/Error'
import { usePermissionStore } from '@/store/modules/permission'
import { useRouter } from 'vue-router'
const { push } = useRouter()
const permissionStore = usePermissionStore()
const errorClick = () => {
push(permissionStore.addRouters[0]?.path as string)
}
</script>

<template>
<Error type="403" @errorClick="errorClick" />
</template>
17 changes: 17 additions & 0 deletions src/views/Error/404.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { Error } from '@/components/Error'
import { usePermissionStore } from '@/store/modules/permission'
import { useRouter } from 'vue-router'
const { push } = useRouter()
const permissionStore = usePermissionStore()
const errorClick = () => {
push(permissionStore.addRouters[0]?.path as string)
}
</script>

<template>
<Error @errorClick="errorClick" />
</template>
17 changes: 17 additions & 0 deletions src/views/Error/500.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { Error } from '@/components/Error'
import { usePermissionStore } from '@/store/modules/permission'
import { useRouter } from 'vue-router'
const { push } = useRouter()
const permissionStore = usePermissionStore()
const errorClick = () => {
push(permissionStore.addRouters[0]?.path as string)
}
</script>

<template>
<Error type="500" @errorClick="errorClick" />
</template>
2 changes: 1 addition & 1 deletion src/views/Example/Dialog/ExampleDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const save = async () => {
</ContentWrap>

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

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

Expand Down
32 changes: 20 additions & 12 deletions src/views/Example/Dialog/components/Write.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive } from 'vue'
import { PropType, reactive, watch } from 'vue'
import { TableData } from '@/api/table/types'
import { useI18n } from '@/hooks/web/useI18n'
import { required } from '@/utils/formRules'
Expand Down Expand Up @@ -113,17 +113,25 @@ const { register, methods, elFormRef } = useForm({
schema
})
if (props.currentRow) {
const { setValues, setSchema } = methods
setValues(props.currentRow)
setSchema([
{
field: 'content',
path: 'componentProps.defaultHtml',
value: props.currentRow.content
}
])
}
watch(
() => props.currentRow,
(currentRow) => {
if (!currentRow) return
const { setValues, setSchema } = methods
setValues(currentRow)
setSchema([
{
field: 'content',
path: 'componentProps.defaultHtml',
value: currentRow.content
}
])
},
{
deep: true,
immediate: true
}
)
defineExpose({
elFormRef,
Expand Down

0 comments on commit 7411dbc

Please sign in to comment.