Skip to content

Commit b144b50

Browse files
committed
feat(layout): normalize URL pathname and construction
1 parent 8caa5aa commit b144b50

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

packages/layout/src/DesignWorkspace.vue

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<script lang="ts">
7272
import { ref, computed, onMounted, type Component } from 'vue'
7373
import { iconArrowLeft } from '@opentiny/vue-icon'
74-
import { getMetaApi, META_SERVICE, getMergeMeta } from '@opentiny/tiny-engine-meta-register'
74+
import { getMetaApi, META_SERVICE, getMergeMeta, useEnv } from '@opentiny/tiny-engine-meta-register'
7575
import { Popover } from '@opentiny/vue'
7676
7777
export default {
@@ -121,9 +121,15 @@ export default {
121121
: tenantList.value[0]
122122
})
123123
124+
const { BASE_URL } = useEnv()
125+
const basePath = new URL(BASE_URL, window.location.origin).pathname
126+
124127
const changeTenant = (id) => {
125-
const baseUrl = `${window.location.origin}${window.location.pathname}?type=app&`
126-
window.location.href = `${baseUrl}tenant=${id}`
128+
const url = new URL(window.location.origin)
129+
url.pathname = basePath
130+
url.searchParams.set('type', 'app')
131+
url.searchParams.set('tenant', id)
132+
window.location.href = url.toString()
127133
}
128134
129135
const logOut = () => {
@@ -138,9 +144,13 @@ export default {
138144
if (enableLogin) {
139145
const tenantId = getBaseInfo().tenantId || tenantList.value[0].id
140146
const url = new URL(window.location.href)
147+
url.pathname = basePath
141148
if (!url.searchParams.has('tenant')) {
142149
url.searchParams.append('tenant', tenantId)
143-
window.history.replaceState({}, '', url.toString())
150+
}
151+
const newHref = url.toString()
152+
if (window.location.href !== newHref) {
153+
window.history.replaceState({}, '', newHref)
144154
}
145155
}
146156
})

packages/layout/src/Main.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<script lang="ts">
4949
/* metaService: engine.layout.Main */
5050
import { ref, computed } from 'vue'
51-
import { useLayout, getMergeMeta, getMergeMetaByType } from '@opentiny/tiny-engine-meta-register'
51+
import { useLayout, getMergeMeta, getMergeMetaByType, useEnv } from '@opentiny/tiny-engine-meta-register'
5252
import { constants } from '@opentiny/tiny-engine-utils'
5353
import DesignToolbars from './DesignToolbars.vue'
5454
import DesignPlugins from './DesignPlugins.vue'
@@ -81,6 +81,17 @@ export default {
8181
const workspaceRegistry = getMergeMetaByType('workspace')
8282
// @legacy 旧版本兼容,后续废弃 type: 'setting' 的 plugin,全部改为 type: 'plugins'
8383
const settingRegistry = getMergeMetaByType('setting')
84+
// 归一化 URL:移除用户手动输入的随机 pathname,防止后续 URL 构建时残留
85+
const { BASE_URL } = useEnv()
86+
const basePath = new URL(BASE_URL, window.location.origin).pathname
87+
const currentPath = window.location.pathname.replace(/\/$/, '')
88+
const expectedPath = basePath.replace(/\/$/, '')
89+
if (currentPath !== expectedPath) {
90+
const normalizedUrl = new URL(window.location.href)
91+
normalizedUrl.pathname = basePath
92+
window.history.replaceState({}, '', normalizedUrl.toString())
93+
}
94+
8495
// 启用isShowWorkspace = true后,且当url中不包含id=xxx即应用id时自动打开workspace页
8596
const queryParams = new URLSearchParams(location.search)
8697
const isShowDefaultWorkspace = computed(() => {

packages/workspace/application-center/src/Main.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import { reactive, ref, onMounted } from 'vue'
9595
import { Button, Select, Pager, Grid, GridColumn, Divider, Search, Modal, Notify } from '@opentiny/vue'
9696
import { iconSearch } from '@opentiny/vue-icon'
9797
import { SearchEmpty } from '@opentiny/tiny-engine-common'
98-
import { getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
98+
import { getMetaApi, META_SERVICE, useEnv } from '@opentiny/tiny-engine-meta-register'
9999
import AppDialog from './AppDialog.vue'
100100
import { fetchApplicationList, createApplication, updateApplication, deleteApplication } from './js/http'
101101
@@ -210,8 +210,11 @@ export default {
210210
}
211211
212212
const openApplication = (template) => {
213-
const href = window.location.href.split('?')[0] || './'
214-
const newUrl = `${href}?type=app&id=${template.id}&tenant=${template.tenantId || queryParams.get('tenant')}`
213+
const { BASE_URL } = useEnv()
214+
const basePath = new URL(BASE_URL, window.location.origin).pathname
215+
const newUrl = `${window.location.origin}${basePath}?type=app&id=${template.id}&tenant=${
216+
template.tenantId || queryParams.get('tenant')
217+
}`
215218
if (window.self !== window.top) {
216219
window.parent.postMessage(
217220
{

0 commit comments

Comments
 (0)