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

@@ -1,13 +1,17 @@
/* eslint-disable no-undef */
import router from '@/router'
export default function jump(options) {
// eslint-disable
if (window.WebViewJavascriptBridge && options.flag) {
if (options.flag == 'h5' ||
options.flag == 'service' ||
options.flag == 'home' ||
options.flag == 'mine' ||
options.flag == 'message' ||
options.flag == 'setting' ) {
if (
options.flag == 'h5' ||
options.flag == 'service' ||
options.flag == 'home' ||
options.flag == 'mine' ||
options.flag == 'message' ||
options.flag == 'setting'
) {
EWebBridge.webCallAppInJs('bridge', {
flag: options.flag,
extra: options.extra
@@ -23,11 +27,11 @@ export default function jump(options) {
} else {
// 1:replace 2:go 默认为push
if (options.routerInfo && options.routerInfo.type == '1') {
this.$router.replace(options.routerInfo)
router.replace(options.routerInfo)
} else if (options.routerInfo && options.routerInfo.type == '2') {
this.$router.go(options.routerInfo.index || -1)
router.go(options.routerInfo.index || -1)
} else if (options.routerInfo) {
this.$router.push(options.routerInfo)
router.push(options.routerInfo)
}
}
}

View File

@@ -20,13 +20,14 @@
>
</van-list>
<Button v-no-more-click="1000" class="bottom-btn" type="danger" @click="add">点我新增</Button>
<Button v-no-more-click="1000" class="bottom-btn" type="danger" @click="navigateDataCollection">点我新增</Button>
</div>
</template>
<script>
import { navigateRouter } from '@/views/ebiz/underwriting/js/navigate'
import { Search, Tabs, Tab, List, Sticky, Button } from 'vant'
import { orderList, getBankCardSignState } from '@/api/ebiz/sale/sale'
import { orderList } from '@/api/ebiz/sale/sale'
import { formatRiskList } from '@/assets/js/utils/formatRiskList.js'
import dataDictionary from '@/assets/js/utils/data-dictionary' //根据数据字典找到用户等级
@@ -142,26 +143,9 @@ export default {
async goDetail() {},
//新增
add() {
let thisToken = this.$CacheUtils.getLocItem('token')
let thisbranchType = ''
if (this.$CacheUtils.getLocItem('branchType') == '13') {
thisbranchType = this.$CacheUtils.getLocItem('branchType')
}
window.localStorage.clear()
this.$CacheUtils.setLocItem('token', thisToken)
if (thisbranchType) {
this.$CacheUtils.setLocItem('branchType', thisbranchType)
}
localStorage.orderNo = ''
localStorage.chooseProductCodes = '' //置空所选险种
this.$jump({
flag: 'h5',
extra: {
url: location.origin + '/#/sale/insuredInfo'
},
routerInfo: { path: '/sale/insuredInfo' }
navigateDataCollection() {
navigateRouter({
name: 'UnderwritingDataCollection'
})
}
}

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)
}