Skip to content

Commit

Permalink
Add redirect to sub official for wechat (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: AceDataCloud <[email protected]>
  • Loading branch information
Germey and AceDataCloud authored Aug 30, 2024
1 parent e453968 commit 0874e19
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add support for redirect to sub offcial",
"packageName": "@acedatacloud/nexior",
"email": "[email protected]",
"dependentChangeType": "patch"
}
5 changes: 3 additions & 2 deletions src/components/common/Logo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<script lang="ts">
import { getCookie } from 'typescript-cookie';
import { BASE_URL_HUB } from '@/constants';
import { BASE_HOST_HUB } from '@/constants';
import { isOfficial } from '@/utils';
export default {
emits: ['click'],
Expand All @@ -14,7 +15,7 @@ export default {
url2: 'https://cdn.acedata.cloud/logo2.png/thumb_450x_',
dark: getCookie('THEME') === 'dark',
url: '',
isOfficial: window.location.origin === BASE_URL_HUB,
isOfficial: isOfficial(),
interval: undefined as number | undefined
};
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/console/SidePanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
ROUTE_INDEX
} from '@/router';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { BASE_URL_HUB } from '@/constants';
import { BASE_HOST_HUB } from '@/constants';
import { isOfficial } from '@/utils';
interface ILink {
key: string;
Expand All @@ -51,7 +52,7 @@ export default defineComponent({
},
computed: {
isOfficial() {
return window.location.origin === BASE_URL_HUB;
return isOfficial();
},
active() {
return this.$route.matched[0].path;
Expand Down
5 changes: 5 additions & 0 deletions src/constants/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const BASE_URL_PLATFORM = isTest ? 'https://platform-test.acedata.cloud'
export const BASE_URL_HUB = isTest ? 'https://hub-test.acedata.cloud' : 'https://hub.acedata.cloud';
export const BASE_URL_AUTH = isTest ? 'https://auth-test.acedata.cloud' : 'https://auth.acedata.cloud';
export const BASE_URL_API = isTest ? 'https://api-test.acedata.cloud' : 'https://api.acedata.cloud';

export const BASE_HOST_PLATFORM = new URL(BASE_URL_PLATFORM).host;
export const BASE_HOST_HUB = new URL(BASE_URL_HUB).host;
export const BASE_HOST_AUTH = new URL(BASE_URL_AUTH).host;
export const BASE_HOST_API = new URL(BASE_URL_API).host;
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import {
initializeSite,
initializeTitle,
initializeCurrency,
initializeExchangeRate
initializeExchangeRate,
initializeRedirect
} from './utils/initializer';

const main = async () => {
// async and need to await
await initializeRedirect();
await initializeCookies();
await initializeToken();
await initializeUser();
Expand Down
14 changes: 13 additions & 1 deletion src/utils/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { getLocale } from '@/i18n';
import store from '@/store';
import { IToken } from '@/models';
import psl from 'psl';
import { LOCALE_CURRENCY_MAPPING } from '@/constants';
import { BASE_HOST_HUB, LOCALE_CURRENCY_MAPPING } from '@/constants';
import { isOfficial, isSubOfficial, isWechatBrowser } from './is';

export const getDomain = (host: string = window.location.hostname) => {
const parsed = psl.parse(host);
Expand Down Expand Up @@ -202,3 +203,14 @@ export const initializeExchangeRate = async () => {
console.debug('initialize exchange rate', payload);
await store.dispatch('getExchangeRate', payload);
};

export const initializeRedirect = async () => {
if (isOfficial() && !isSubOfficial() && isWechatBrowser()) {
console.debug('redirect to sub domain with prefix');
// redirect from hub.acedata.cloud to dynamic date like 20240802.hub.acedata.cloud
const date = new Date().toISOString().split('T')[0].replace(/-/g, '');
const newUrl = window.location.href.replace(BASE_HOST_HUB, `${date}.${BASE_HOST_HUB}`);
console.debug('redirect to', newUrl);
window.location.href = newUrl;
}
};
23 changes: 23 additions & 0 deletions src/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Js JSON string
*/

import { BASE_HOST_HUB } from '@/constants';

export const isJSONString = (str: string): boolean => {
try {
JSON.parse(str);
Expand All @@ -10,3 +12,24 @@ export const isJSONString = (str: string): boolean => {
}
return true;
};

/**
* Check if the string is wechat browser
*/
export const isWechatBrowser = (): boolean => {
return /micromessenger/i.test(navigator.userAgent);
};

/**
* isOfficial
*/
export const isOfficial = (): boolean => {
return window.location.host.includes(BASE_HOST_HUB);
};

/**
* isSubOfficial
*/
export const isSubOfficial = (): boolean => {
return isOfficial() && window.location.host !== BASE_HOST_HUB;
};
5 changes: 5 additions & 0 deletions src/utils/site.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ISite } from '@/models';
import { v4 as uuid } from 'uuid';
import { isOfficial } from './is';
import { BASE_URL_HUB } from '@/constants';

export const getSiteOrigin = (site?: ISite) => {
if (site?.origin) {
return site?.origin;
}
if (isOfficial()) {
return BASE_URL_HUB;
}
const host = window.location.host;
// if localhost, try to generate uuid
if (host.includes('localhost')) {
Expand Down

0 comments on commit 0874e19

Please sign in to comment.