Add redirect to sub official for wechat (#132)

Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
GermeyandAceDataCloud authored and GitHub committed 2024-08-31 01:04:02 +08:00
1 parent e453968abe
commit 0874e193d8
8 files changed
+62 -6

No files matched your search

+13 -1
View File
@@ -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);
@@ -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
View File
@@ -2,6 +2,8 @@
* Js JSON string
*/
import { BASE_HOST_HUB } from '@/constants';
export const isJSONString = (str: string): boolean => {
try {
JSON.parse(str);
@@ -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
View File
@@ -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')) {