diff --git a/src/game_pages/detail/components/SharePopup/index.tsx b/src/game_pages/detail/components/SharePopup/index.tsx index 69e594d..53f2fda 100644 --- a/src/game_pages/detail/components/SharePopup/index.tsx +++ b/src/game_pages/detail/components/SharePopup/index.tsx @@ -25,9 +25,10 @@ dayjs.locale("zh-cn"); // 分享弹窗 export default forwardRef(({ id, from, detail, userInfo }, ref) => { const [visible, setVisible] = useState(false); - const [publishFlag, setPublishFlag] = useState(false); const [shareImageUrl, setShareImageUrl] = useState(""); const { fetchUserInfo } = useUserActions(); + + const publishFlag = from === "publish"; // const posterRef = useRef(); const { max_participants, participant_count } = detail || {}; @@ -57,18 +58,20 @@ export default forwardRef(({ id, from, detail, userInfo }, ref) => { } useImperativeHandle(ref, () => ({ - show: async (publish_flag = false) => { - setPublishFlag(publish_flag); - if (publish_flag) { - try { - const url = await generateShareImageUrl(); - setShareImageUrl(url); - } catch (e) {} - } + show: async () => { setVisible(true); }, })); + useEffect(() => { + if (from === "publish") { + generateShareImageUrl().then((url) => { + setShareImageUrl(url); + setVisible(true); + }); + } + }, [from]); + async function generateShareImageUrl() { const { play_type, @@ -183,7 +186,6 @@ export default forwardRef(({ id, from, detail, userInfo }, ref) => { function onClose() { setVisible(false); - setPublishFlag(false); } return ( diff --git a/src/game_pages/detail/index.tsx b/src/game_pages/detail/index.tsx index 2c1aaf5..3644cfe 100644 --- a/src/game_pages/detail/index.tsx +++ b/src/game_pages/detail/index.tsx @@ -54,12 +54,6 @@ function Index() { await waitForAuthInit(); // 然后再获取用户信息 await fetchUserInfo(); - - await delay(1000); - - if (from === "publish") { - handleShare(true); - } }; init(); }, []); @@ -126,8 +120,12 @@ function Index() { } } - function handleShare(flag = false) { - sharePopupRef.current.show(flag); + function handleShare() { + if (!detail.id) { + toast("球局未加载完成,请稍后再试"); + return false; + } + sharePopupRef.current.show(); } const handleJoinGame = async () => { @@ -293,13 +291,15 @@ function Index() { currentUserInfo={myInfo} /> {/* share popup */} - + {detail.id && myInfo.id && ( + + )} ); diff --git a/src/utils/genPoster.ts b/src/utils/genPoster.ts index a1ec2c4..ba9a72a 100644 --- a/src/utils/genPoster.ts +++ b/src/utils/genPoster.ts @@ -35,12 +35,21 @@ export function base64ToTempFilePath(base64Data: string): Promise { } +interface TaroGetImageInfo { + getImageInfo(option: { + src: string; + success?: (res: { width: number; height: number }) => void; + fail?: (err: unknown) => void; + }): void; +} + /** 获取图片宽高 */ function getImageWh(src: string): Promise<{ width: number; height: number }> { - return new Promise((resolve) => { - Taro.getImageInfo({ + return new Promise((resolve, reject) => { + (Taro as TaroGetImageInfo).getImageInfo({ src, success: ({ width, height }) => resolve({ width, height }), + fail: (e) => reject(e), }); }); }