用户端mini-user界面优化

This commit is contained in:
2026-07-15 17:33:46 +08:00
parent 1ad49f1acf
commit d6ac4b5e1c
@@ -1,4 +1,4 @@
import Taro, { useDidShow } from '@tarojs/taro';
import Taro from '@tarojs/taro';
import { captureIosJssdkEntryUrl } from '@dukang/weixin-sdk';
import { useEffect, useRef } from 'react';
import { finishLoginNavigate, goLogin } from '../lib/auth-nav';
@@ -29,25 +29,25 @@ function currentPagePathWithQuery(): string {
}
/**
* H5 全局:
* 1. 默认分享卡片
* 2. 公众号 OAuth ?code= 统一回调(避免各页重复消费 code)
* H5 App 根节点专用:不可用 useDidShowApp 无页面 Context)。
* - 首次进页:捕获 iOS 签名 URL + 默认分享 + OAuth code 回调
* - 路由/回前台:刷新默认分享卡片
*/
export default function WechatShareBootstrap() {
const handlingCode = useRef(false);
useEffect(() => {
if (process.env.TARO_ENV === 'h5') {
captureIosJssdkEntryUrl();
}
}, []);
useDidShow(() => {
if (process.env.TARO_ENV !== 'h5') return;
void applyWechatShare().catch(() => {});
if (!isWechatEnv()) return;
if (typeof window === 'undefined') return;
captureIosJssdkEntryUrl();
function refreshShare() {
void applyWechatShare().catch(() => {});
}
function tryHandleOAuth() {
if (!isWechatEnv()) return;
const params = new URLSearchParams(window.location.search);
if (!params.get('code')) return;
if (handlingCode.current) return;
@@ -77,7 +77,10 @@ export default function WechatShareBootstrap() {
}
if (saveWechatLoginResult(result)) {
toast('微信授权成功', 'success');
if (returnFromLogin !== undefined || currentPagePathWithQuery().includes('/pages/login/')) {
if (
returnFromLogin !== undefined ||
currentPagePathWithQuery().includes('/pages/login/')
) {
finishLoginNavigate(returnFromLogin || params.get('return') || undefined);
}
}
@@ -88,7 +91,43 @@ export default function WechatShareBootstrap() {
.finally(() => {
handlingCode.current = false;
});
});
}
refreshShare();
tryHandleOAuth();
const onVisible = () => {
if (document.visibilityState === 'visible') refreshShare();
};
const onLocation = () => {
refreshShare();
tryHandleOAuth();
};
document.addEventListener('visibilitychange', onVisible);
window.addEventListener('popstate', onLocation);
const { pushState, replaceState } = window.history;
window.history.pushState = function (...args) {
const ret = pushState.apply(this, args);
window.dispatchEvent(new Event('dukang-h5-route'));
return ret;
};
window.history.replaceState = function (...args) {
const ret = replaceState.apply(this, args);
window.dispatchEvent(new Event('dukang-h5-route'));
return ret;
};
window.addEventListener('dukang-h5-route', onLocation);
return () => {
document.removeEventListener('visibilitychange', onVisible);
window.removeEventListener('popstate', onLocation);
window.removeEventListener('dukang-h5-route', onLocation);
window.history.pushState = pushState;
window.history.replaceState = replaceState;
};
}, []);
return null;
}