Skip to content

Commit

Permalink
chore(front): 优化修改插件钩子参数
Browse files Browse the repository at this point in the history
  • Loading branch information
kanyxmo committed Nov 16, 2024
1 parent 305ad7f commit 36bca4d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
8 changes: 5 additions & 3 deletions web/src/plugins/mine-admin/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @Author X.Mo<[email protected]>
* @Link https://github.com/mineadmin
*/
import type { Router, RouteRecordRaw } from 'vue-router'
import type { Router } from 'vue-router'
import { useProTableRenderPlugin } from '@mineadmin/pro-table'
import type { MineToolbarExpose, Plugin } from '#/global'
import Message from 'vue-m-message'
Expand Down Expand Up @@ -54,8 +54,10 @@ const pluginConfig: Plugin.PluginConfig = {
routesRaw, router,
)
},
routerRedirect: (route: RouteRecordRaw) => {
console.log('demo 插件的路由跳转钩子,此次跳转路由信息:', route)
routerRedirect: (routes, router: Router) => {
router.getRoutes()
console.log('demo 插件的路由跳转钩子,此次跳转旧路由信息:', routes.oldRoute)
console.log('demo 插件的路由跳转钩子,此次跳转新路由信息:', routes.newRoute)
},
networkRequest: (request) => {
console.log('demo 插件的网络请求钩子,此次请求信息:', request)
Expand Down
12 changes: 8 additions & 4 deletions web/src/store/modules/useMenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const useMenuStore = defineStore(
topMenu.value = routesRaw.find((route: any): boolean => route.path === '/')?.children as MineRoute.routeRecord[] ?? []
}, { immediate: true, deep: true })

watch((): any => route, async (newRoute: MineRoute.routeRecord) => {
watchRoute.value = newRoute
watch((): any => route.name, async (newName: string, oldName: string) => {
watchRoute.value = route as MineRoute.routeRecord
if (route.matched[1] && route.matched[1].meta && route.matched[1].meta.breadcrumb) {
const breadcrumb = route.matched[1].meta.breadcrumb as MineRoute.routeRecord[]
activeTopMenu.value = breadcrumb.find((item, index) => index === 0)
Expand All @@ -57,9 +57,13 @@ const useMenuStore = defineStore(
activeTopMenu.value = undefined
}

const newRoute = router.getRoutes().find((route: any): boolean => route.name === newName)
const oldRoute = router.getRoutes().find((route: any): boolean => route.name === oldName)

settingStore.setTitle(route?.meta?.i18n ? t(route.meta.i18n as string) : route.meta.title as string)
await usePluginStore().callHooks('routerRedirect', newRoute, router)
}, { immediate: true, deep: true })

await usePluginStore().callHooks('routerRedirect', { oldRoute, newRoute }, router)
}, { deep: true })

watch((): any => activeTopMenu.value, (newActiveTopMenu: MineRoute.routeRecord) => {
if (newActiveTopMenu && newActiveTopMenu.children && newActiveTopMenu.children?.length > 0) {
Expand Down
16 changes: 2 additions & 14 deletions web/src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,8 @@ http.interceptors.response.use(
})
}
}
case ResultCode.NOT_FOUND:
Message.error('服务器资源不存在', { zIndex: 9999 })
break
case ResultCode.FORBIDDEN:
Message.error('没有权限访问此接口', { zIndex: 9999 })
break
case ResultCode.METHOD_NOT_ALLOWED:
Message.error('请求方法不被允许', { zIndex: 9999 })
break
case ResultCode.FAIL:
Message.error('服务器内部错误', { zIndex: 9999 })
break
default:
Message.error(response?.data?.message ?? '未知错误', { zIndex: 9999 })
Message.error(response?.data?.message ?? '服务器错误', { zIndex: 9999 })
break
}

Expand All @@ -156,7 +144,7 @@ http.interceptors.response.use(
isLoading.value = false
const serverError = useDebounceFn(async () => {
if (error && error.response && error.response.status === 500) {
Message.error(error.message ?? '服务器内部错误', { zIndex: 9999 })
Message.error(error.message ?? '服务器错误', { zIndex: 9999 })
}
}, 3000, { maxWait: 5000 })
await serverError()
Expand Down
2 changes: 1 addition & 1 deletion web/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ declare namespace Plugin {
// 获取用户信息时触发
getUserInfo?: (userInfo: any) => any | void
// 路由跳转时钩子,外链形式路由不生效
routerRedirect?: (route: RouteRecordRaw, router: Router) => any | void
routerRedirect?: ({ oldRoute: RouteRecordRaw, newRoute: RouteRecordRaw }, router: Router) => any | void
// 网络请求时钩子
networkRequest?: (request: T) => any | void
// 网络返回后钩子
Expand Down

0 comments on commit 36bca4d

Please sign in to comment.