From 4cf2b959b56df857e9b68b41041a147b39b7bd00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Tue, 10 Feb 2026 12:42:42 +0800 Subject: [PATCH] 1 --- src/main_pages/index.config.ts | 9 +++++---- src/main_pages/index.tsx | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main_pages/index.config.ts b/src/main_pages/index.config.ts index 719c08f..35e4828 100644 --- a/src/main_pages/index.config.ts +++ b/src/main_pages/index.config.ts @@ -1,6 +1,7 @@ export default definePageConfig({ - navigationBarTitleText: '首页', - navigationStyle: 'custom', - navigationBarBackgroundColor: '#FAFAFA' - }) + navigationBarTitleText: '首页', + navigationStyle: 'custom', + navigationBarBackgroundColor: '#FAFAFA', + enableShareAppMessage: true, +}) diff --git a/src/main_pages/index.tsx b/src/main_pages/index.tsx index 114d516..65a5479 100644 --- a/src/main_pages/index.tsx +++ b/src/main_pages/index.tsx @@ -1,6 +1,7 @@ import React, { useState, useEffect, useCallback } from "react"; import { View } from "@tarojs/components"; -import Taro from "@tarojs/taro"; +import Taro, { useRouter, useShareAppMessage } from "@tarojs/taro"; +import { OSS_BASE } from "@/config/api"; import { wechat_auth_login, save_login_state } from "@/services/loginService"; import { useUserActions } from "@/store/userStore"; import { useGlobalState } from "@/store/global"; @@ -18,7 +19,11 @@ import { useDictionaryStore } from "@/store/dictionaryStore"; type TabType = "list" | "message" | "personal"; const MainPage: React.FC = () => { - const [currentTab, setCurrentTab] = useState("list"); + const { params } = useRouter(); + const [currentTab, setCurrentTab] = useState(() => { + const tab = params?.tab as TabType | undefined; + return tab === "list" || tab === "message" || tab === "personal" ? tab : "list"; + }); const [isPublishMenuVisible, setIsPublishMenuVisible] = useState(false); const [isDistanceFilterVisible, setIsDistanceFilterVisible] = useState(false); const [isCityPickerVisible, setIsCityPickerVisible] = useState(false); @@ -35,6 +40,14 @@ const MainPage: React.FC = () => { const { showGuideBar, guideBarZIndex, setShowGuideBar, setGuideBarZIndex } = useGlobalState(); + // 从分享链接进入时根据 ?tab= 定位到对应 tab + useEffect(() => { + const tab = params?.tab as TabType | undefined; + if (tab === "list" || tab === "message" || tab === "personal") { + setCurrentTab(tab); + } + }, [params?.tab]); + // 初始化:自动微信授权并获取用户信息 useEffect(() => { const init = async () => { @@ -153,6 +166,21 @@ const MainPage: React.FC = () => { [] ); + // 分享:首页、个人页均支持转发 + // 分享图:配置 OSS 地址 + 路径(不含 ? 后参数),首页用 share_home.png,个人页用 share_self.png + useShareAppMessage(() => { + const isList = currentTab === "list"; + const isPersonal = currentTab === "personal"; + const title = isList ? "约球 - 发现身边的球局" : isPersonal ? "约球 - 我的约球" : "约球"; + const image_path = isPersonal ? "system/share_self.png" : "system/share_home.png"; + const imageUrl = OSS_BASE ? `${OSS_BASE.replace(/\/$/, "")}/${image_path}` : ""; + return { + title, + path: "/main_pages/index" + (isList ? "?tab=list" : isPersonal ? "?tab=personal" : ""), + imageUrl: imageUrl, + }; + }); + // 滚动到顶部 const scrollToTop = useCallback(() => { // 如果当前是列表页,触发列表页内部滚动