157 lines
4.4 KiB
JavaScript
157 lines
4.4 KiB
JavaScript
// config.js
|
|
window.ENV = {
|
|
// 开发环境配置
|
|
development: {
|
|
API_BASE_URL: 'http://47.108.24.205:9100',
|
|
// API_BASE_URL: 'http://192.168.10.119:9100',
|
|
// 后端租户标识,会作为 header tenantId 发送;后面更换租户只改这里。
|
|
TENANT_ID: '531923',
|
|
// TENANT_ID: '000000',
|
|
CLIENT_ID: '209ef407810e3856f40870a9f0e769d7',
|
|
DEBUG: true,
|
|
APP_NAME: 'Lottery App Dev'
|
|
},
|
|
// 生产环境配置
|
|
production: {
|
|
API_BASE_URL: 'https://api-3d.3dyjs.cn',
|
|
// 后端租户标识,会作为 header tenantId 发送;后面更换租户只改这里。
|
|
TENANT_ID: '531923',
|
|
CLIENT_ID: '209ef407810e3856f40870a9f0e769d7',
|
|
DEBUG: false,
|
|
APP_NAME: 'Lottery App'
|
|
}
|
|
};
|
|
|
|
// 根据域名自动判断环境
|
|
window.CURRENT_ENV = (function () {
|
|
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1' || location.port !== '') {
|
|
return 'development';
|
|
} else {
|
|
return 'production';
|
|
}
|
|
})();
|
|
|
|
// 获取当前环境配置
|
|
window.CURRENT_CONFIG = window.ENV[window.CURRENT_ENV];
|
|
|
|
window.NavigationUtil = (function (window, document) {
|
|
const blockedProtocols = /^(?:javascript|data|vbscript|mailto|tel):/i;
|
|
|
|
function inviteCodeFromUrl(url) {
|
|
try {
|
|
return String(new URL(url || window.location.href, window.location.href).searchParams.get('inviteCode') || '').trim();
|
|
} catch (error) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
function isRegisterUrl(url) {
|
|
try {
|
|
return /(?:^|\/)reg\.html$/i.test(new URL(url, window.location.href).pathname);
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function withReferralInviteCode(url, inviteCode) {
|
|
const destination = String(url || '').trim();
|
|
if (!destination || !isRegisterUrl(destination)) return destination;
|
|
const code = String(inviteCode || inviteCodeFromUrl(window.location.href)).trim();
|
|
if (!code) return destination;
|
|
const target = new URL(destination, window.location.href);
|
|
if (!target.searchParams.get('inviteCode')) target.searchParams.set('inviteCode', code);
|
|
return target.href;
|
|
}
|
|
|
|
function referralRegisterUrl(referralUrl, inviteCode) {
|
|
const code = String(inviteCode || inviteCodeFromUrl(referralUrl)).trim();
|
|
return code ? withReferralInviteCode('reg.html', code) : '';
|
|
}
|
|
|
|
function shouldOpenInNewTab(anchor) {
|
|
const href = (anchor.getAttribute('href') || '').trim();
|
|
|
|
return href && href !== '#' && !href.startsWith('#') &&
|
|
!anchor.hasAttribute('download') && !blockedProtocols.test(href);
|
|
}
|
|
|
|
function prepareAnchor(anchor) {
|
|
if (!anchor || anchor.tagName !== 'A' || !shouldOpenInNewTab(anchor)) {
|
|
return;
|
|
}
|
|
|
|
const href = anchor.getAttribute('href') || '';
|
|
const referralHref = withReferralInviteCode(href);
|
|
if (referralHref !== href) anchor.setAttribute('href', referralHref);
|
|
|
|
if (anchor.target !== '_blank') {
|
|
anchor.target = '_blank';
|
|
}
|
|
|
|
const relTokens = (anchor.getAttribute('rel') || '').split(/\s+/).filter(Boolean);
|
|
|
|
if (!relTokens.includes('noopener')) {
|
|
anchor.setAttribute('rel', [...relTokens, 'noopener'].join(' '));
|
|
}
|
|
}
|
|
|
|
function prepareAnchors(root) {
|
|
if (root.nodeType === 1 && root.matches('a[href]')) {
|
|
prepareAnchor(root);
|
|
}
|
|
|
|
root.querySelectorAll('a[href]').forEach(prepareAnchor);
|
|
}
|
|
|
|
function installLinkTargeting() {
|
|
prepareAnchors(document);
|
|
|
|
new MutationObserver((mutations) => {
|
|
mutations.forEach((mutation) => {
|
|
if (mutation.type === 'attributes') {
|
|
prepareAnchor(mutation.target);
|
|
return;
|
|
}
|
|
|
|
mutation.addedNodes.forEach((node) => {
|
|
if (node.nodeType === 1) {
|
|
prepareAnchors(node);
|
|
}
|
|
});
|
|
});
|
|
}).observe(document.documentElement, {
|
|
subtree: true,
|
|
childList: true,
|
|
attributes: true,
|
|
attributeFilter: ['href', 'target', 'download']
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', installLinkTargeting, { once: true });
|
|
} else {
|
|
installLinkTargeting();
|
|
}
|
|
|
|
return {
|
|
open(url) {
|
|
const destination = withReferralInviteCode(url);
|
|
|
|
if (!destination) {
|
|
return null;
|
|
}
|
|
|
|
const openedWindow = window.open(destination, '_blank', 'noopener');
|
|
|
|
if (openedWindow) {
|
|
openedWindow.opener = null;
|
|
}
|
|
|
|
return openedWindow;
|
|
},
|
|
|
|
withReferralInviteCode,
|
|
referralRegisterUrl
|
|
};
|
|
})(window, document);
|