feat(underwriting): 新增路由跳转工具函数并优化跳转逻辑

- 引入 jump 工具函数并调整其内部路由调用方式
- 新增 navigateRouter 函数用于统一管理路由跳转
- 替换原有 add 方法为 navigateDataCollection 并使用新跳转逻辑
- 移除旧有的手动清理 localStorage 的逻辑
- 优化路由查找逻辑,支持多层级 children 路由匹配
- 添加 URL 参数处理函数 processJson 以适配查询字符串拼接
This commit is contained in:
hz
2025-12-10 16:13:08 +08:00
parent 5af2c7f6bf
commit a8d13be189
3 changed files with 101 additions and 31 deletions

View File

@@ -0,0 +1,82 @@
import jump from '@/assets/js/utils/jump'
import router from '@/router'
/**
* 路由跳转函数
* @param path { String}路径
* @param name
* @param title {String} 标题内容
* @param params { Object}参数
*/
export function navigateRouter({ path = '', name = '', title = '', params = {} } = {}) {
const routes = router.options.routes
let options = {
flag: 'h5',
extra: {
title,
// forbidSwipeBack: 1, //当前页面禁止右滑返回
url: ''
},
routerInfo: { query: params }
}
if (path) {
const route = routes.find(item => {
if (item.children) {
// 可能后面 children 存在多个,目前暂时先判定一层。 待优化
return item.children.find(childItem => `${item.path}/${childItem.path}` === path)
}
return item.path === path
})
if (!route) {
console.error('在 routers 中无法找到该路径,请检查路径是否正确')
return
}
options.extra = {
title: route.meta && route.meta.title ? route.meta.title : '',
url: location.origin + '/#' + path + '?' + processJson(params)
}
options.routerInfo = { path }
} else if (name) {
const route = routes.find(item => {
if (item.children) {
// 可能后面 children 存在多个,目前暂时先判定一层。 待优化
return item.children.find(childItem => childItem.name === name)
}
return item.name === name
})
if (!route) {
console.error('在 routers 中无法找到该路径,请检查路径是否正确')
return
}
// 如何存在子元素, 需要重新更改 path 地址
if (route.children) {
route.path = `${route.path}/${route.children.find(childItem => childItem.name === name).path}`
}
console.log(route)
options.extra = {
title: route.meta && route.meta.title ? route.meta.title : '',
url: location.origin + '/#' + route.path + '?' + processJson(params)
}
options.routerInfo = { name }
}
jump(options)
}
/**
* 处理 json 键值对,到 url 中
*/
function processJson(json) {
if (typeof json !== 'object') return
if (window.URLSearchParams) return new URLSearchParams(json).toString()
let url = ''
for (const key in json) {
if (json.hasOwnProperty(key)) {
url += key + '=' + json[key] + '&'
}
}
return url.substring(0, url.length - 1)
}