Skip to content

Commit e6cb7a8

Browse files
fix: vue-router 升级兼容处理 (#2910)
1 parent e82d1df commit e6cb7a8

10 files changed

Lines changed: 109 additions & 26 deletions

File tree

webfe/package_vue/src/components/app-migration-dialog/index.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,15 @@ export default {
345345
// 列表页直接刷新
346346
window.location.reload();
347347
} else {
348-
await this.$router.push({
348+
const failure = await this.$router.push({
349349
name: 'cloudAppSummary',
350350
params: {
351351
id: this.data.code,
352352
},
353353
});
354-
window.location.reload();
354+
if (!failure || failure.name === 'NavigationDuplicated') {
355+
window.location.reload();
356+
}
355357
}
356358
}
357359
},

webfe/package_vue/src/components/global-search/search-result.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export default {
302302
// 详情
303303
toAppBaseInfo(appItem) {
304304
this.$router.push({
305-
name: 'appBaseInfo',
305+
name: 'appBasicInfo',
306306
params: {
307307
id: appItem.code,
308308
},

webfe/package_vue/src/components/paas-app-nav.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ export default {
530530
});
531531
} else {
532532
this.$router.push({
533-
name: 'appBaseInfo',
533+
name: 'appBasicInfo',
534534
params: {
535535
id: this.curAppInfo.application.code,
536536
},

webfe/package_vue/src/components/plugin-quick-nav.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ export default {
165165
},
166166
async changePlugin(data) {
167167
// 如果去往当前的路由没有权限则去往概览页
168-
const parmas = this.getTarget(data.id, data.pd_id);
168+
const params = this.getTarget(data.id, data.pd_id);
169169
this.hideSelectData();
170-
this.$router.push(parmas);
171170
// 如果当前为插件版本、测试阶段/测试报告,返回概览页
172171
if (['pluginVersionRelease', 'pluginTestReport'].includes(this.$route.name)) {
173-
this.$router.replace({
172+
this.$router.push({
174173
name: 'pluginSummary',
175-
query: {
174+
params: {
176175
id: data.id,
177176
pluginTypeId: data.pd_id,
178177
},
179178
});
179+
} else {
180+
this.$router.push(params);
180181
}
181182
},
182183
searchPlugin: debounce(function () {

webfe/package_vue/src/router/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Vue from 'vue';
2020
import Router from 'vue-router';
2121
import { pluginRouter } from './plugin';
2222
import { platformRouters } from './platform';
23+
import { installNavigationCompat } from './navigation-compat';
2324
import store from '@/store';
2425

2526
const frontPage = () => import(/* webpackChunkName: 'front-page' */'@/views/index').then(module => module).catch((error) => {
@@ -376,6 +377,8 @@ const permission403 = () => import(/* webpackChunkName: 'permission403' */'@/vie
376377

377378
Vue.use(Router);
378379

380+
installNavigationCompat(Router);
381+
379382
const router = new Router({
380383
mode: 'history',
381384
// web url path
@@ -744,6 +747,13 @@ const router = new Router({
744747
},
745748
{
746749
path: 'module-info',
750+
redirect: to => ({
751+
name: 'moduleInfo',
752+
params: to.params,
753+
query: to.query,
754+
}),
755+
},
756+
{
747757
path: 'info',
748758
component: moduleInfo,
749759
name: 'moduleInfo',
@@ -827,9 +837,11 @@ const router = new Router({
827837
path: ':id/deployments',
828838
component: cloudAppDeployManage,
829839
name: 'cloudAppDeployManage',
830-
redirect: {
831-
name: 'cloudAppDeployForProcess',
832-
},
840+
redirect: to => ({
841+
name: 'cloudAppDeployManageStag',
842+
params: to.params,
843+
query: to.query,
844+
}),
833845
children: [
834846
{
835847
path: 'stag',
@@ -1123,7 +1135,7 @@ const router = new Router({
11231135
},
11241136
{
11251137
path: '/developer-center/apps/:id/create/gitee/success',
1126-
name: 'createGithubAppSucc',
1138+
name: 'createGiteeAppSucc',
11271139
component: createGitAppSucc,
11281140
},
11291141
{
@@ -1148,7 +1160,7 @@ router.beforeEach(async (to, from, next) => {
11481160
sessionStorage.setItem('NOTICE', true);
11491161
if (window.location.href.indexOf(window.GLOBAL_CONFIG.V3_OA_DOMAIN) !== -1) {
11501162
const url = window.location.href.replace(window.GLOBAL_CONFIG.V3_OA_DOMAIN, window.GLOBAL_CONFIG.V3_WOA_DOMAIN);
1151-
window.location.replace(url);
1163+
return window.location.replace(url);
11521164
} else {
11531165
const checkUserFeature = async (featureKey) => {
11541166
// 可能为页面刷新重新调用获取功能开关
@@ -1158,11 +1170,12 @@ router.beforeEach(async (to, from, next) => {
11581170
store.state.userFeature[featureKey] ? next() : next({ name: '404' });
11591171
};
11601172
if (to.path.startsWith('/plugin-center')) {
1161-
checkUserFeature('ALLOW_PLUGIN_CENTER');
1173+
await checkUserFeature('ALLOW_PLUGIN_CENTER');
11621174
} else if (to.path.startsWith('/plat-mgt')) {
1163-
checkUserFeature('PLATFORM_MANAGEMENT');
1175+
await checkUserFeature('PLATFORM_MANAGEMENT');
1176+
} else {
1177+
next();
11641178
}
1165-
next();
11661179
}
11671180
});
11681181

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* 判断导航失败是否为可忽略的类型(重复导航或重定向导航)。
3+
* 1. 通过 error.name 直接判断
4+
* 2. 通过 Router.isNavigationFailure 静态方法判断
5+
*
6+
* @param {Object} Router - vue-router 构造函数
7+
* @param {Error|null} error - 导航过程中抛出的错误对象
8+
* @returns {boolean} 是否是可忽略的导航失败
9+
*/
10+
const isIgnoredNavigationFailure = (Router, error) => (
11+
error
12+
&& (
13+
error.name === 'NavigationDuplicated'
14+
|| error.name === 'NavigationRedirected'
15+
|| (Router.isNavigationFailure
16+
&& Router.NavigationFailureType
17+
&& Router.isNavigationFailure(error, Router.NavigationFailureType.duplicated))
18+
|| (Router.isNavigationFailure
19+
&& Router.NavigationFailureType
20+
&& Router.isNavigationFailure(error, Router.NavigationFailureType.redirected))
21+
)
22+
);
23+
24+
/**
25+
* 包装路由导航方法(push/replace),
26+
* 使其在传入回调时保持原有调用方式,
27+
* 在返回 Promise 时静默吞掉可忽略的导航失败异常。
28+
*
29+
* @param {Object} Router - vue-router 构造函数
30+
* @param {Function} fn - 原始导航方法(push 或 replace)
31+
* @returns {Function} 包装后的导航方法
32+
*/
33+
const wrapNavigationMethod = (Router, fn) => function navigation(location, onResolve, onReject) {
34+
// 传统回调风格:用户传入了 onResolve/onReject,直接透传原始方法
35+
if (onResolve || onReject) {
36+
return fn.call(this, location, onResolve, onReject);
37+
}
38+
// Promise 风格:捕获可忽略的失败,避免未捕获的 Promise 异常
39+
const navigationResult = fn.call(this, location);
40+
if (!navigationResult || typeof navigationResult.catch !== 'function') {
41+
return navigationResult;
42+
}
43+
return navigationResult.catch((error) => {
44+
if (isIgnoredNavigationFailure(Router, error)) {
45+
return error;
46+
}
47+
return Promise.reject(error);
48+
});
49+
};
50+
51+
/**
52+
* 安装导航兼容补丁,覆盖 Router.prototype 上的 push 和 replace 方法,
53+
*
54+
* @param {Object} Router - vue-router 构造函数
55+
*/
56+
export function installNavigationCompat(Router) {
57+
const { prototype } = Router;
58+
// 避免重复安装
59+
if (prototype.navigationCompatInstalled) {
60+
return;
61+
}
62+
prototype.push = wrapNavigationMethod(Router, prototype.push);
63+
prototype.replace = wrapNavigationMethod(Router, prototype.replace);
64+
prototype.navigationCompatInstalled = true;
65+
}

webfe/package_vue/src/views/dev-center/app/engine/deployment/comps/deploy-action/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ export default {
23662366
// 未设置插件分类
23672367
case 'FILL_PLUGIN_TAG_INFO':
23682368
this.$router.push({
2369-
name: 'appBaseInfo',
2369+
name: 'appBasicInfo',
23702370
params: {
23712371
id: this.appCode,
23722372
pluginTypeActive: true,

webfe/package_vue/src/views/dev-center/app/engine/processes/comps/process-operation.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ export default {
819819
}
820820
return {
821821
btnLoadingGif: require('@static/images/btn_loading.gif'),
822+
unregisterRouteGuard: null,
822823
processConfigDialog: {
823824
isLoading: false,
824825
visiable: false,
@@ -1111,14 +1112,18 @@ export default {
11111112
moment.locale(this.localLanguage);
11121113
this.init();
11131114
// 切换路由前清空定时器
1114-
this.$router.beforeEach((to, from, next) => {
1115+
this.unregisterRouteGuard = this.$router.beforeEach((to, from, next) => {
11151116
this.closeServerPush();
11161117
next();
11171118
});
11181119
this.isDateChange = false;
11191120
this.getAutoScalFlag();
11201121
},
11211122
beforeDestroy() {
1123+
if (this.unregisterRouteGuard) {
1124+
this.unregisterRouteGuard();
1125+
this.unregisterRouteGuard = null;
1126+
}
11221127
this.closeServerPush();
11231128
this.closeLogDetail();
11241129
},

webfe/package_vue/src/views/dev-center/app/index.vue

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/>
1313
<!-- 普通应用 -->
1414
<paas-app-nav
15-
v-else
15+
v-else-if="type"
1616
:is-migration-entry-shown="isMigrationEntryShown"
1717
@show-migration-dialog="showMigrationDialog"
1818
/>
@@ -59,7 +59,7 @@
5959
{{ $t('您没有访问当前应用该功能的权限,如需申请,请联系') }}
6060
<router-link
6161
class="toRolePage"
62-
:to="{ name: 'appRoles', params: { id: appCode } }"
62+
:to="{ name: 'appMembers', params: { id: appCode } }"
6363
>
6464
{{ $t('成员管理') }}
6565
</router-link>
@@ -136,7 +136,7 @@ export default {
136136
// 非应用引擎 应用 时所要显示的子级导航
137137
subNavIds: [10, 12, 13, 14],
138138
engineEnabled: false,
139-
type: 'default',
139+
type: null,
140140
appMigrationDialogConfig: {
141141
visible: false,
142142
data: {},
@@ -196,9 +196,6 @@ export default {
196196
curAppInfo = store.state.appInfo[appCode];
197197
store.commit('updateCurAppByCode', { appCode, moduleId });
198198
}
199-
next((vm) => {
200-
vm.type = curAppInfo.application.type;
201-
});
202199
// 如果不带moduleId, 以默认模块作一次重定向
203200
if (!moduleId) {
204201
to.params.moduleId = curAppInfo.application.modules.find((module) => module.is_default).name;
@@ -208,7 +205,7 @@ export default {
208205
query: to.query,
209206
});
210207
} else {
211-
next(true);
208+
next((vm) => Object.assign(vm, { type: curAppInfo.application.type }));
212209
}
213210
} catch (e) {
214211
next({

webfe/package_vue/src/views/dev-center/search/comps/application.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default {
133133
134134
toAppBaseInfo(appItem) {
135135
this.$router.push({
136-
name: 'appBaseInfo',
136+
name: 'appBasicInfo',
137137
params: {
138138
id: appItem.code,
139139
},

0 commit comments

Comments
 (0)