From e39eebbf1cb5c3bf99e46faee99d2b6b04e4025c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Mon, 13 Jul 2026 08:22:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(share):=20=E8=A1=A5=E5=81=9A=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E5=88=86=E4=BA=AB=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=85=E5=88=86=E4=BA=AB=E5=88=B0=E5=A5=BD=E5=8F=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useShare 增加 enableTimeline 配置,默认启用朋友圈分享 - 首页配置 enableShareAppMessage 并调用 useShare,enableTimeline 设为 false - 解决 tabBar 页微信不支持朋友圈分享的问题,避免无效注册 - 分享路径添加裂变参数,增强邀请下级功能 - 保持类型检查零错误,改动兼容现有历史 TS2688 错误 --- .workbuddy/memory/2026-07-13.md | 6 ++++++ src/hooks/useShare.ts | 4 ++++ src/pages/index/index.tsx | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/.workbuddy/memory/2026-07-13.md b/.workbuddy/memory/2026-07-13.md index b204234..4f1971e 100644 --- a/.workbuddy/memory/2026-07-13.md +++ b/.workbuddy/memory/2026-07-13.md @@ -8,3 +8,9 @@ - 接入 10 个页面:商品/积分/拼团/秒杀/活动/赛事详情(带 canvas 海报);邀请下级/分销推广/升级VIP/分享赚佣金(仅 enableShare + useShare)。 - 类型检查:本次改动零错误,仅余 1 个历史既有错误(TS2688 @tarojs/taro 类型解析,非本次引入)。 - 待办/注意:朋友圈分享必须真机测试;后端需支持按 page 生成指定落地页小程序码;单页模式降级(避免强制登录跳转)可调用 `isTimelineSinglePage()`。 + +## 首页接入分享(补做) +- `src/hooks/useShare.ts` 增加 `enableTimeline?: boolean` 选项(默认 true),tabBar 页传 false 避免注册无效的朋友圈回调(React hook 规则下用内部 `if` 判断返回空对象,始终调用 hook)。 +- `src/pages/index/index.tsx`:config 加 `enableShareAppMessage: true`,组件内调用 `useShare({ title, path:'/pages/index/index', enableTimeline:false })`。 +- 说明:首页为 tabBar 页,微信不允许分享到朋友圈,故仅启用「分享给好友」,路径带 inviter 裂变参数。 +- 类型检查:零错误(历史既有 1 个 TS2688 仍在)。 diff --git a/src/hooks/useShare.ts b/src/hooks/useShare.ts index 23f5cd9..c87eb25 100644 --- a/src/hooks/useShare.ts +++ b/src/hooks/useShare.ts @@ -11,6 +11,8 @@ export interface UseShareOptions { query?: string /** 封面图(海报临时路径或网络图地址),不传则微信截取页面 */ imageUrl?: string + /** 是否注册朋友圈分享(仅非 tabBar 页生效)。tabBar 页请传 false,避免注册无效的圈友回调 */ + enableTimeline?: boolean } /** 当前是否处于朋友圈单页模式(scene=1154) */ @@ -59,6 +61,8 @@ export function useShare(options: UseShareOptions) { }) useShareTimeline(() => { + // tabBar 页不支持朋友圈,config 也不会开启对应入口,这里返回空对象避免无效注册 + if (optionsRef.current.enableTimeline === false) return {} const o = optionsRef.current return { title: o.title, diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index cc5d82c..7c03945 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react' import { View, Text, ScrollView, Swiper, SwiperItem, Image } from '@tarojs/components' import Taro from '@tarojs/taro' import { useUser } from '@/hooks/useUser' +import { useShare } from '@/hooks/useShare' import { useScrollHeight } from '@/hooks/useScrollHeight' import { listCmsAd } from '@/api/cms/cmsAd' import { listCmsArticle } from '@/api/cms/cmsArticle' @@ -14,6 +15,7 @@ import { getCompressedImageUrl } from '@/utils/image' definePageConfig({ navigationBarTitleText: '首页', + enableShareAppMessage: true, }) const IndexPage: React.FC = () => { @@ -24,6 +26,13 @@ const IndexPage: React.FC = () => { const [loading, setLoading] = useState(true) const scrollHeight = useScrollHeight(44) + // 首页分享给好友(tabBar 页不支持朋友圈,仅启用好友分享) + useShare({ + title: '鑫龙家电 - 精选好物,实惠到家,等你来逛~', + path: '/pages/index/index', + enableTimeline: false, + }) + const categories = ['全部', '球拍', '球鞋', '服装', '配件']