feat: 添加催一催按钮、应用宝跳转配置

This commit is contained in:
2026-08-12 13:15:50 +08:00
parent 6a1f4f66ac
commit 9a30b0efde
10 changed files with 744 additions and 54 deletions
+249 -7
View File
@@ -15,6 +15,214 @@
import {
ShoproInit
} from './sheep';
const SCHEME_TAG = '[Scheme]';
const HOME_PATH = '/pages/index/index';
const SCHEME_PROTOCOL = 'yougou://';
const schemeState = {
lastScheme: '',
lastHandleAt: 0,
launching: false,
schemeHandledInLaunch: false,
};
const isSameSchemeDebounce = (scheme) => {
const now = Date.now();
const same = schemeState.lastScheme === scheme && now - schemeState.lastHandleAt < 800;
schemeState.lastScheme = scheme;
schemeState.lastHandleAt = now;
return same;
};
const parseSchemeQuery = (query = {}) => {
try {
const result = {};
Object.keys(query || {}).forEach((k) => {
const v = query[k];
if (v === undefined || v === null) return;
if (typeof v === 'string' && v.length > 512) {
result[k] = v.slice(0, 512);
return;
}
result[k] = v;
});
return result;
} catch (e) {
console.warn(SCHEME_TAG, 'parseSchemeQuery error', e);
return {};
}
};
const resolveTargetPath = (scheme = '', query = {}) => {
try {
const url = String(scheme || '').trim();
if (!url) return { path: HOME_PATH, query: parseSchemeQuery(query) };
const withoutProtocol = url.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//, '');
if (!withoutProtocol) return { path: HOME_PATH, query: parseSchemeQuery(query) };
const [pathRaw = '', queryRaw = ''] = withoutProtocol.split('?');
const cleanPath = pathRaw.replace(/^\/+/, '/').trim();
const path = cleanPath.startsWith('/pages/') ? cleanPath : HOME_PATH;
const merged = { ...parseSchemeQuery(query) };
if (queryRaw) {
queryRaw.split('&').forEach((pair) => {
const [k, v] = pair.split('=');
if (!k) return;
try { merged[decodeURIComponent(k)] = v === undefined ? '' : decodeURIComponent(v); } catch (_) {}
});
}
return { path, query: merged };
} catch (e) {
console.warn(SCHEME_TAG, 'resolveTargetPath error, fallback to home', e);
return { path: HOME_PATH, query: parseSchemeQuery(query) };
}
};
const buildUrl = (path, query) => {
try {
const q = query || {};
const keys = Object.keys(q).filter((k) => q[k] !== undefined && q[k] !== null && q[k] !== '');
if (!keys.length) return path;
const qs = keys
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(String(q[k]))}`)
.join('&');
return `${path}${path.includes('?') ? '&' : '?'}${qs}`;
} catch (e) {
return path;
}
};
const safeReLaunch = (path, query, source) => {
const finalUrl = buildUrl(path, query);
console.log(SCHEME_TAG, `${source} → reLaunch`, finalUrl);
try {
uni.reLaunch({
url: finalUrl,
fail: (err) => {
console.warn(SCHEME_TAG, 'reLaunch fail, fallback to switchTab/home', err);
if (path === HOME_PATH) {
try {
uni.switchTab({
url: HOME_PATH,
fail: (err2) => {
console.warn(SCHEME_TAG, 'switchTab also fail', err2);
uni.navigateTo({ url: HOME_PATH, fail: () => {} });
},
});
} catch (e) {
console.warn(SCHEME_TAG, 'switchTab catch', e);
}
} else {
uni.navigateTo({ url: finalUrl, fail: () => uni.reLaunch({ url: HOME_PATH }) });
}
},
});
} catch (e) {
console.error(SCHEME_TAG, 'safeReLaunch catch error', e);
try { uni.reLaunch({ url: HOME_PATH }); } catch (_) {}
}
};
const isSchemeTrigger = (options) => {
if (!options) return false;
if (String(options?.path || '').toLowerCase() === 'scheme') return true;
const scheme = String(options?.scheme || options?.argv?.scheme || '');
if (scheme.startsWith(SCHEME_PROTOCOL)) return true;
const referrer = String(options?.referrer || '');
if (referrer.startsWith(SCHEME_PROTOCOL)) return true;
return false;
};
const handleSchemeByOptions = (options, source) => {
const scheme = String(options?.scheme || options?.argv?.scheme || options?.referrer || '');
if (isSameSchemeDebounce(scheme)) {
console.log(SCHEME_TAG, `${source} 防抖跳过`, scheme);
return;
}
console.log(SCHEME_TAG, `${source} 触发`, options);
const query = { ...(options?.query || {}), ...(options?.argv?.query || {}) };
const { path, query: resolvedQuery } = resolveTargetPath(scheme, query);
safeReLaunch(path, resolvedQuery, source);
};
let schemeEventListenerRegistered = false;
const registerSchemeEventListener = () => {
if (schemeEventListenerRegistered) return;
if (typeof plus === 'undefined' || !plus?.globalEvent) return;
try {
plus.globalEvent.addEventListener('newintent', (e) => {
const intent = e?.intent || e;
let scheme = '';
let query = {};
try {
if (typeof plus?.runtime?.arguments === 'function' || typeof plus?.runtime?.arguments !== 'undefined') {
const args = plus.runtime.arguments;
if (args && typeof args === 'object') {
scheme = String(args.scheme || args.url || args.action || '');
query = args.query || {};
} else if (typeof args === 'string') {
scheme = args;
}
}
if (!scheme && intent?.data) {
const data = String(intent.data || '');
scheme = data;
const i = data.indexOf('?');
if (i > -1) {
data.slice(i + 1).split('&').forEach((pair) => {
const [k, v] = pair.split('=');
if (!k) return;
try { query[decodeURIComponent(k)] = v === undefined ? '' : decodeURIComponent(v); } catch (_) {}
});
}
}
if (!scheme && intent?.extras) {
scheme = String(intent.extras.scheme || intent.extras.url || intent.extras['android.intent.extra.TEXT'] || '');
}
} catch (err) {
console.warn(SCHEME_TAG, 'newintent parse error', err);
}
if (!scheme) return;
if (isSameSchemeDebounce(scheme)) {
console.log(SCHEME_TAG, 'newintent 防抖跳过', scheme);
return;
}
console.log(SCHEME_TAG, 'newintent(plus.globalEvent) 触发', { scheme, query, intent });
const { path, query: resolvedQuery } = resolveTargetPath(scheme, query);
safeReLaunch(path, resolvedQuery, 'newintent后台唤起');
});
schemeEventListenerRegistered = true;
console.log(SCHEME_TAG, 'plus.globalEvent newintent 监听已注册');
} catch (e) {
console.warn(SCHEME_TAG, 'registerSchemeEventListener catch', e);
}
};
const registerSchemeRuntimeArguments = () => {
if (typeof plus === 'undefined' || !plus?.runtime) return;
try {
const args = plus.runtime.arguments;
let scheme = '';
let query = {};
if (args && typeof args === 'object') {
scheme = String(args.scheme || args.url || '');
query = args.query || {};
} else if (typeof args === 'string' && args) {
scheme = args;
}
if (!scheme) return;
if (isSameSchemeDebounce(scheme)) {
console.log(SCHEME_TAG, 'runtime.arguments 防抖跳过', scheme);
return;
}
console.log(SCHEME_TAG, 'plus.runtime.arguments 读取', { scheme, query });
const { path, query: resolvedQuery } = resolveTargetPath(scheme, query);
safeReLaunch(path, resolvedQuery, 'plus.runtime.arguments冷启动');
} catch (e) {
console.warn(SCHEME_TAG, 'registerSchemeRuntimeArguments catch', e);
}
};
let globalData = ref({
statusBarHeight: 0, // 状态导航栏高度
navHeight: 0, // 总体高度
@@ -194,7 +402,7 @@
ensureGlobalHotUpdateChecked();
// socket()
});
onLaunch(() => {
onLaunch((options = {}) => {
try {
uni.hideTabBar({
fail: () => {},
@@ -219,20 +427,54 @@
uni.setStorageSync('globalData', globalData.value);
// 加载Shopro底层依赖
ShoproInit();
console.log(SCHEME_TAG, 'onLaunch', options);
schemeState.launching = true;
schemeState.schemeHandledInLaunch = false;
// #ifdef APP-PLUS
const boot = () => {
registerSchemeEventListener();
const optionsHit = isSchemeTrigger(options);
if (optionsHit) {
schemeState.schemeHandledInLaunch = true;
handleSchemeByOptions(options, 'onLaunch冷启动scheme');
} else {
registerSchemeRuntimeArguments();
}
};
if (typeof plus === 'undefined' || !plus?.runtime) {
const start = Date.now();
const t = setInterval(() => {
if (typeof plus !== 'undefined' && plus?.runtime) {
clearInterval(t);
boot();
} else if (Date.now() - start > 4000) {
clearInterval(t);
boot();
}
}, 50);
} else {
boot();
}
setTimeout(() => { schemeState.launching = false; }, 1500);
// #endif
});
onShow(() => {
onShow((options = {}) => {
try {
uni.hideTabBar({
fail: () => {},
});
} catch (e) {}
// #ifdef APP-PLUS
// 获取urlSchemes参数
const args =
typeof plus !== 'undefined' && plus?.runtime ? plus.runtime.arguments : '';
if (args) {}
if (
!schemeState.launching &&
!schemeState.schemeHandledInLaunch &&
isSchemeTrigger(options)
) {
handleSchemeByOptions(options, 'onShow兜底scheme');
}
schemeState.schemeHandledInLaunch = false;
// 获取剪贴板
uni.getClipboardData({
success: (res) => {},