diff --git a/src/layout/components/NavHeader/Organization.vue b/src/layout/components/NavHeader/Organization.vue index dd437df42..c047ede7f 100644 --- a/src/layout/components/NavHeader/Organization.vue +++ b/src/layout/components/NavHeader/Organization.vue @@ -101,16 +101,19 @@ export default { return currentOrgId } }, - created() { - }, methods: { changeOrg(orgId) { - if (orgId === 'create') { - this.$router.push({ name: 'OrganizationCreate' }) - } else if (orgId === 'list') { - this.$router.push({ name: 'OrganizationList' }) - } else { - orgUtil.changeOrg(orgId) + const org = this.usingOrgs.find(item => item.id === orgId) + + switch (orgId) { + case 'create': + this.$router.push({ name: 'OrganizationCreate' }) + break + case 'list': + this.$router.push({ name: 'OrganizationList' }) + break + default: + orgUtil.changeOrg(org) } } } diff --git a/src/utils/common.js b/src/utils/common.js index 8e0e407ff..33f6cba83 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -159,6 +159,10 @@ export function hasUUID(s) { return s.search(uuidPattern) !== -1 } +export function replaceUUID(s, n) { + return s.replace(uuidPattern, n) +} + export function getDaysAgo(days, now) { if (!now) { now = new Date() diff --git a/src/utils/org.js b/src/utils/org.js index 53842ea14..e2808a99d 100644 --- a/src/utils/org.js +++ b/src/utils/org.js @@ -1,6 +1,5 @@ -import { hasUUID, BASE_URL } from '@/utils/common' -import { getOrgDetail } from '@/api/orgs' import store from '@/store' +import { hasUUID, replaceUUID } from '@/utils/common' export const DEFAULT_ORG_ID = '00000000-0000-0000-0000-000000000002' @@ -10,12 +9,25 @@ function getPropOrg() { if (defaultOrg) { return defaultOrg } - return orgs.filter(item => !item.is_root)[0] + return orgs.filter(item => !item['is_root'])[0] } -function change2PropOrg() { +async function change2PropOrg() { const org = getPropOrg() - setTimeout(() => changeOrg(org.id), 100) + await changeOrg(org) +} + +async function changeOrg(org) { + await store.dispatch('users/setCurrentOrg', org) + await store.dispatch('app/reset') + let path = location.href + if (hasUUID(path)) { + path = replaceUUID(path, '') + path = _.trimEnd(path, '/') + location.href = path + } else { + location.reload() + } } function hasCurrentOrgPermission() { @@ -25,27 +37,10 @@ function hasCurrentOrgPermission() { return orgs.find((item) => item.id === currentOrgId) } -async function changeOrg(orgId) { - const org = await getOrgDetail(orgId) - if (!org) { - console.debug('Error: org not found') - } else { - console.debug('Change to org: ', org) - } - - store.dispatch('users/setCurrentOrg', org).then(() => { - // console.log('Set current org to: ', org) - if (hasUUID(location.href)) { - location.href = BASE_URL - } else { - window.location.reload(true) - } - }) -} - export default { hasCurrentOrgPermission, - changeOrg, DEFAULT_ORG_ID, - change2PropOrg + change2PropOrg, + changeOrg, + getPropOrg } diff --git a/src/utils/startup.js b/src/utils/startup.js index 4a4056643..d9481a1dd 100644 --- a/src/utils/startup.js +++ b/src/utils/startup.js @@ -67,14 +67,12 @@ async function changeCurrentOrgIfNeed({ to, from, next }) { await refreshCurrentOrg() const currentOrg = store.getters.currentOrg if (!currentOrg || typeof currentOrg !== 'object') { - Vue.$log.error('Current org is null or not a object') - orgUtil.change2PropOrg() - return reject('Change prop org') + Vue.$log.error('Current org is null or not a object: ', currentOrg) + await orgUtil.change2PropOrg({ to, from, next }) } if (!orgUtil.hasCurrentOrgPermission()) { - Vue.$log.error('Not has current org permission') - orgUtil.change2PropOrg() - return reject('Change prop org') + Vue.$log.error('Not has current org permission: ', currentOrg) + await orgUtil.change2PropOrg({ to, from, next }) } } @@ -126,7 +124,7 @@ export async function changeCurrentViewIfNeed({ to, from, next }) { } const has = isViewHasOrgs(viewName) - Vue.$log.debug('Change has current view, has perm: ', has) + Vue.$log.debug('Change has current view, has perm: ', viewName, '=>', has) if (has) { await store.dispatch('users/changeToView', viewName) return