Procházet zdrojové kódy

✨ feat(category/goods-list/payment-success): 增强页面分享与返回逻辑

- 在商品分类页和商品列表页新增分享到朋友圈功能
- 优化导航栏返回逻辑,支持根据来源页面(如分享)跳转至首页
- 在支付成功页移除订阅状态检查,始终显示订阅按钮以提升订阅率
- 统一从Taro导入多个API以优化代码结构
- 修复商品详情页返回逻辑的注释描述

🐛 fix(payment-success): 修复订阅状态更新条件

- 修复用户订阅状态更新逻辑,仅在未订阅时向数据库提交更新,避免重复操作
yourname před 1 měsícem
rodič
revize
348cf0c6a0

+ 47 - 17
mini/src/pages/category/index.tsx

@@ -7,11 +7,10 @@ import CategorySidebarItem from '@/components/category/CategorySidebarItem';
 import CategoryTabbar, { TabItem } from '@/components/category/CategoryTabbar';
 import CategoryTabbar, { TabItem } from '@/components/category/CategoryTabbar';
 import { Image } from '@/components/ui/image';
 import { Image } from '@/components/ui/image';
 import TDesignToast from '@/components/tdesign/toast';
 import TDesignToast from '@/components/tdesign/toast';
-import { navigateTo,useShareAppMessage } from '@tarojs/taro';
+import Taro,{ useRouter, navigateTo,useShareAppMessage,useShareTimeline } from '@tarojs/taro';
 import { InferResponseType } from 'hono';
 import { InferResponseType } from 'hono';
 import { TabBarLayout } from '@/layouts/tab-bar-layout';
 import { TabBarLayout } from '@/layouts/tab-bar-layout';
 import { Navbar } from '@/components/ui/navbar';
 import { Navbar } from '@/components/ui/navbar';
-import Taro from '@tarojs/taro';
 import TDesignIcon from '@/components/tdesign/icon';
 import TDesignIcon from '@/components/tdesign/icon';
 import './index.css';
 import './index.css';
 
 
@@ -27,6 +26,13 @@ const CategoryPage: React.FC = () => {
   const [toastVisible, setToastVisible] = useState<boolean>(false);
   const [toastVisible, setToastVisible] = useState<boolean>(false);
   const [toastMessage, setToastMessage] = useState<string>('');
   const [toastMessage, setToastMessage] = useState<string>('');
 
 
+
+    // 使用useRouter钩子获取路由参数
+    const router = useRouter()
+    const params = router.params
+    const goodsId = params?.id ? parseInt(params.id) : 0
+    const fromPage = params?.from || ''
+
   // 动态设置导航栏和tabbar高度
   // 动态设置导航栏和tabbar高度
   useEffect(() => {
   useEffect(() => {
     const setDynamicHeights = () => {
     const setDynamicHeights = () => {
@@ -61,20 +67,33 @@ const CategoryPage: React.FC = () => {
     setDynamicHeights();
     setDynamicHeights();
   }, []);
   }, []);
 
 
-    // 分享功能
-    useShareAppMessage(() => {
-      // 如果有当前选中的分类,使用分类图片作为分享图片
-      // 如果没有,使用默认图片或空字符串
-      const shareImageUrl = currentCategory?.imageFile?.fullUrl ||
-                           subCategories?.[0]?.imageFile?.fullUrl ||
-                           '';
-
-      return {
-        title: currentCategory ? `${currentCategory.name} - 发现好物` : '商品分类 - 发现好物',
-        path: `/pages/category/index?from=share`,
-        imageUrl: shareImageUrl
-      }
-    })
+  // 分享功能
+  useShareAppMessage(() => {
+    // 如果有当前选中的分类,使用分类图片作为分享图片
+    // 如果没有,使用默认图片或空字符串
+    const shareImageUrl = currentCategory?.imageFile?.fullUrl ||
+      subCategories?.[0]?.imageFile?.fullUrl ||
+      '';
+
+    return {
+      title: currentCategory ? `${currentCategory.name} - 发现好物` : '商品分类 - 发现好物',
+      path: `/pages/category/index?from=share`,
+      imageUrl: shareImageUrl
+    }
+  })
+
+  // 分享到朋友圈功能
+  useShareTimeline(() => {
+    const shareImageUrl = currentCategory?.imageFile?.fullUrl ||
+      subCategories?.[0]?.imageFile?.fullUrl ||
+      '';
+
+    return {
+      title: currentCategory ? `${currentCategory.name} - 发现好物` : '商品分类 - 发现好物',
+      path: `/pages/category/index?from=share`,
+      imageUrl: shareImageUrl
+    }
+  })
 
 
   // 获取分类数据
   // 获取分类数据
   const { data: categoryData, isLoading, error } = useQuery({
   const { data: categoryData, isLoading, error } = useQuery({
@@ -189,7 +208,18 @@ const CategoryPage: React.FC = () => {
       <Navbar
       <Navbar
         title="商品分类"
         title="商品分类"
         leftIcon=""
         leftIcon=""
-        onClickLeft={() => Taro.navigateBack()}
+         onClickLeft={() => {
+                  // 根据来源页面决定返回逻辑
+                  //  分享,返回首页
+                  if (fromPage === 'share') {
+                     Taro.switchTab({
+                      url: `/pages/index/index`
+                    })
+                  } else {
+                   // 从其他页面来的
+                   Taro.navigateBack()
+                  }
+                }}
         rightIcon=""
         rightIcon=""
         onClickRight={() => {}}
         onClickRight={() => {}}
       />
       />

+ 1 - 1
mini/src/pages/goods-detail/index.tsx

@@ -239,7 +239,7 @@ export default function GoodsDetailPage() {
               url: `/pages/index/index`
               url: `/pages/index/index`
             })
             })
           } else {
           } else {
-           // 从其他页面来的,返回购物车
+           // 从其他页面来的,返回上一页
            Taro.navigateBack()
            Taro.navigateBack()
           }
           }
         }}
         }}

+ 13 - 1
mini/src/pages/goods-list/index.tsx

@@ -21,6 +21,7 @@ export default function GoodsListPage() {
   const router = useRouter()
   const router = useRouter()
   const params = router.params
   const params = router.params
   const categoryId = params?.cateId || ''
   const categoryId = params?.cateId || ''
+  const fromPage = params?.from || ''
 
 
   const categories = [
   const categories = [
     { id: 'all', name: '全部' },
     { id: 'all', name: '全部' },
@@ -154,7 +155,18 @@ export default function GoodsListPage() {
       <Navbar
       <Navbar
         title="商品列表"
         title="商品列表"
         leftIcon="i-heroicons-chevron-left-20-solid"
         leftIcon="i-heroicons-chevron-left-20-solid"
-        onClickLeft={() => Taro.navigateBack()}
+        onClickLeft={() => {
+                          // 根据来源页面决定返回逻辑
+                          //  分享,返回首页
+                          if (fromPage === 'share') {
+                             Taro.switchTab({
+                              url: `/pages/index/index`
+                            })
+                          } else {
+                           // 从其他页面来的
+                           Taro.navigateBack()
+                          }
+                        }}
         className="bg-white shadow-sm"
         className="bg-white shadow-sm"
       />
       />
       
       

+ 16 - 28
mini/src/pages/payment-success/index.tsx

@@ -107,7 +107,7 @@ const PaymentSuccessPage = () => {
      // Taro.setStorageSync('hasSubscribedDeliveryNotice', subscribed)
      // Taro.setStorageSync('hasSubscribedDeliveryNotice', subscribed)
 
 
       // 异步更新到数据库
       // 异步更新到数据库
-      if (user?.id) {
+      if (user?.id && !checkHasSubscribed()) {
         updateUserSubscriptionMutation.mutate(subscribed, {
         updateUserSubscriptionMutation.mutate(subscribed, {
           onSuccess: () => {
           onSuccess: () => {
             console.log('用户订阅状态已更新到数据库:', subscribed)
             console.log('用户订阅状态已更新到数据库:', subscribed)
@@ -126,11 +126,11 @@ const PaymentSuccessPage = () => {
   // 引导用户订阅发货通知
   // 引导用户订阅发货通知
   const requestSubscribeMessage = async (): Promise<boolean> => {
   const requestSubscribeMessage = async (): Promise<boolean> => {
     try {
     try {
-      // 检查用户是否已经订阅过
-      if (checkHasSubscribed()) {
-        console.log('用户已订阅过发货通知,跳过引导')
-        return true
-      }
+      // // 检查用户是否已经订阅过
+      // if (checkHasSubscribed()) {
+      //   console.log('用户已订阅过发货通知,跳过引导')
+      //   return true
+      // }
 
 
       // 发货成功通知模板ID
       // 发货成功通知模板ID
       const templateId = 'T00N0Wq3ECjksXSvPWUBgOUukl1TCE7PhxqeDnFPfso'
       const templateId = 'T00N0Wq3ECjksXSvPWUBgOUukl1TCE7PhxqeDnFPfso'
@@ -339,28 +339,16 @@ const PaymentSuccessPage = () => {
         >
         >
           查看订单列表
           查看订单列表
         </Button>
         </Button>
-        {/* <Button
-          onClick={handleBackToHome}
-          className="w-full h-12"
-          variant="ghost"
-        >
-          返回首页
-        </Button> */}
-
-          {/* 订阅发货通知按钮 - 只在用户未订阅时显示 */}
-          {
-          //!checkHasSubscribed() && (
-            <Button
-              onClick={handleSubscribe}
-              className="w-full h-12"
-              variant="outline"
-              size="lg"
-            >
-              📦 订阅发货通知
-            </Button>
-         // )
-          }
-          
+
+          <Button
+            onClick={handleSubscribe}
+            className="w-full h-12"
+            variant="outline"
+            size="lg"
+          >
+            📦 订阅发货通知
+          </Button>
+         
       </View>
       </View>
 
 
       {/* 温馨提示 */}
       {/* 温馨提示 */}