feat(shop): 添加商品分享邀请功能
- 切换API基础URL到生产环境地址 - 在商品详情页添加邀请参数解析和存储逻辑 - 实现分享链接携带邀请者ID和来源信息 - 新增商品分享来源类型标识 - 在短信登录成功后处理待绑定的邀请关系 - 添加邀请关系跟踪和统计功能
This commit is contained in:
@@ -15,6 +15,7 @@ import SpecSelector from "@/components/SpecSelector";
|
||||
import "./index.scss";
|
||||
import {useCart} from "@/hooks/useCart";
|
||||
import {useConfig} from "@/hooks/useConfig";
|
||||
import {parseInviteParams, saveInviteParams, trackInviteSource} from "@/utils/invite";
|
||||
|
||||
const GoodsDetail = () => {
|
||||
const [statusBarHeight, setStatusBarHeight] = useState<number>(44);
|
||||
@@ -39,6 +40,24 @@ const GoodsDetail = () => {
|
||||
const {cartCount, addToCart} = useCart()
|
||||
const {config} = useConfig()
|
||||
|
||||
// 如果从分享链接进入(携带 inviter/source/t),且当前未登录,则暂存邀请信息用于注册后绑定关系
|
||||
useEffect(() => {
|
||||
try {
|
||||
const currentUserId = Taro.getStorageSync('UserId')
|
||||
if (currentUserId) return
|
||||
|
||||
const inviteParams = parseInviteParams({query: router?.params})
|
||||
if (inviteParams?.inviter) {
|
||||
saveInviteParams(inviteParams)
|
||||
trackInviteSource(inviteParams.source || 'share', parseInt(inviteParams.inviter))
|
||||
}
|
||||
} catch (e) {
|
||||
// 邀请参数解析/存储失败不影响正常浏览商品
|
||||
console.error('商品详情页处理邀请参数失败:', e)
|
||||
}
|
||||
// router 在 Taro 中可能不稳定;这里仅在 goodsId 变化时尝试处理一次即可
|
||||
}, [goodsId])
|
||||
|
||||
// 处理加入购物车
|
||||
const handleAddToCart = () => {
|
||||
if (!goods) return;
|
||||
@@ -186,9 +205,15 @@ const GoodsDetail = () => {
|
||||
|
||||
// 分享给好友
|
||||
useShareAppMessage(() => {
|
||||
const inviter = Taro.getStorageSync('UserId')
|
||||
const sharePath =
|
||||
inviter
|
||||
? `/shop/goodsDetail/index?id=${goodsId}&inviter=${inviter}&source=goods_share&t=${Date.now()}`
|
||||
: `/shop/goodsDetail/index?id=${goodsId}`
|
||||
|
||||
return {
|
||||
title: goods?.name || '精选商品',
|
||||
path: `/shop/goodsDetail/index?id=${goodsId}`,
|
||||
path: sharePath,
|
||||
imageUrl: goods?.image ? `${goods.image}?x-oss-process=image/resize,w_500,h_400,m_fill` : undefined, // 分享图片,调整为5:4比例
|
||||
success: function (res: any) {
|
||||
console.log('分享成功', res);
|
||||
|
||||
Reference in New Issue
Block a user