浏览代码

商品分享页修改上传

viczhq 1 周之前
父节点
当前提交
22f7eb27c9

+ 5 - 19
src/components/index/ProductList/index.jsx

@@ -7,8 +7,9 @@ import React, {
7 7
 import { View, Text, Image, Button } from "@tarojs/components";
8 8
 import { AtCheckbox, AtActivityIndicator } from "taro-ui";
9 9
 import { setShareContent } from "../../../common/share";
10
-import { getShareJump, getProductDetail } from "../../../service";
10
+import { getShareJump } from "../../../service";
11 11
 import { handleLog } from "../../../common/track";
12
+import { isDebounce } from "../../../common/debounce";
12 13
 import Taro from "@tarojs/taro";
13 14
 import "./index.less";
14 15
 import ShareModal from "../../shareModal";
@@ -101,27 +102,12 @@ const ProductList = forwardRef((props, ref) => {
101 102
   };
102 103
   // 分享商品
103 104
   const shareProduct = async (product, e) => {
105
+    if (!isDebounce(500)) return; // 如果在500ms内重复触发,直接返回
104 106
     e.stopPropagation(); // 防止事件穿透
105 107
     setCurrentShareProduct(product);
106
-    const productRes = await getProductDetail({
107
-      goods_id: product.id,
108
+    Taro.navigateTo({
109
+      url: `/pages/indexSub/sharePage/index?isSeckill=${isSeckill}&&tagTitle=${tagTitle}&&shareUserId=${shareUserId}&id=${product.id}`,
108 110
     });
109
-    // 使用 encodeURIComponent 编码商品详情数据
110
-    const encodedProductDetail = encodeURIComponent(JSON.stringify(productRes));
111
-    // 设置分享路径
112
-    const share_extend = JSON.stringify({
113
-      path: `pages/indexSub/productDetail/index?id=${product.id}&isShare=true&isSeckill=${isSeckill}&shareType=11&&shareUserId=${shareUserId}`,
114
-    });
115
-    getShareJump({
116
-      share_type: 11,
117
-      share_id: product.id,
118
-      share_extend,
119
-    }).then((res) => {
120
-      Taro.navigateTo({
121
-        url: `/pages/indexSub/sharePage/index?isSeckill=${isSeckill}&&tagTitle=${tagTitle}&&productDetail=${encodedProductDetail}&&shareJump=${res.share_unique_value}&&share_scheme_link=${encodeURIComponent(res.share_scheme_link)}&&shareUserId=${shareUserId}`,
122
-      });
123
-    });
124
-    return;
125 111
     // 埋点
126 112
     if (isSeckill) {
127 113
       handleLog({

+ 3 - 11
src/pages/indexSub/productDetail/index.jsx

@@ -9,6 +9,7 @@ import {
9 9
   bindShareJump,
10 10
 } from "../../../service";
11 11
 import { handleLog } from "../../../common/track";
12
+import { isDebounce } from "../../../common/debounce";
12 13
 import "./index.less";
13 14
 import Taro from "@tarojs/taro";
14 15
 import productDetailIcon from "../../../images/productDetail/productDetailIcon.png";
@@ -173,26 +174,17 @@ export default class Index extends Component {
173 174
   };
174 175
   // 分享
175 176
   openSharePopup = async () => {
177
+    if (!isDebounce(500)) return; // 如果在500ms内重复触发,直接返回
176 178
     let shareUserId = "";
177 179
     if (Taro.getStorageSync("loginInfo")) {
178 180
       shareUserId = Taro.getStorageSync("loginInfo").id;
179 181
     }
180
-    // 设置分享路径
181
-    const share_extend = JSON.stringify({
182
-      path: `pages/indexSub/productDetail/index?id=${this.state.id}&isShare=true&isSeckill=${this.state.isSeckill}&shareType=11&&shareUserId=${shareUserId}`,
183
-    });
184
-    // 获取分享跳转标识
185
-    const res = await getShareJump({
186
-      share_type: 11,
187
-      share_id: this.state.id,
188
-      share_extend,
189
-    });
190 182
     // 使用 encodeURIComponent 编码商品详情数据
191 183
     const encodedProductDetail = encodeURIComponent(
192 184
       JSON.stringify(this.state.productDetail)
193 185
     );
194 186
     Taro.navigateTo({
195
-      url: `/pages/indexSub/sharePage/index?isSeckill=${this.state.isSeckill}&shareJump=${res.share_unique_value}&tagTitle=${this.state.tagTitle}&productDetail=${encodedProductDetail}&shareUserId=${shareUserId}&&share_scheme_link=${encodeURIComponent(res.share_scheme_link)}`,
187
+      url: `/pages/indexSub/sharePage/index?isSeckill=${this.state.isSeckill}&tagTitle=${this.state.tagTitle}&productDetail=${encodedProductDetail}&shareUserId=${shareUserId}`,
196 188
     });
197 189
     // 埋点
198 190
     if (this.state.isSeckill) {

+ 88 - 29
src/pages/indexSub/sharePage/index.jsx

@@ -1,10 +1,12 @@
1 1
 import { Component } from "react";
2
-import { View, Image, Text, Button, Input } from "@tarojs/components";
2
+import { View, Image, Button, Input } from "@tarojs/components";
3
+import { getShareJump, getProductDetail } from "../../../service/index";
3 4
 import Taro from "@tarojs/taro";
4 5
 import "./index.less";
5 6
 
6 7
 export default class Index extends Component {
7 8
   state = {
9
+    id: "", //商品id(专题列表分享过来的)
8 10
     shareTitle: "", //分享标题
9 11
     productDetail: {}, //商品详情
10 12
     isSeckill: false, //是否是秒杀商品
@@ -17,18 +19,58 @@ export default class Index extends Component {
17 19
     Taro.hideShareMenu({
18 20
       menus: ["shareAppMessage", "shareTimeline"],
19 21
     });
20
-    const { isSeckill, shareJump, tagTitle, productDetail,shareUserId,share_scheme_link } =
22
+    const { isSeckill, tagTitle, productDetail, shareUserId, id } =
21 23
       Taro.getCurrentInstance().router.params;
24
+    this.setState(
25
+      {
26
+        isSeckill: isSeckill === "true",
27
+        tagTitle,
28
+        productDetail: productDetail ? JSON.parse(decodeURIComponent(productDetail)) : {},
29
+        shareTitle: productDetail ? JSON.parse(decodeURIComponent(productDetail)).item_title : "",
30
+        shareUserId,
31
+        id: id,
32
+      },
33
+      async () => {
34
+        Taro.showLoading({
35
+          title: "加载中...",
36
+        });
37
+        try {
38
+          await Promise.all([
39
+            this.getProductDetail(id),
40
+            this.getShareJump(id)
41
+          ]);
42
+        } finally {
43
+          Taro.hideLoading();
44
+        }
45
+      }
46
+    );
47
+  }
48
+  // 获取商品详情
49
+  getProductDetail = async (id='') => {
50
+    const res = await getProductDetail({
51
+      goods_id: id||this.state.productDetail.id,
52
+    });
22 53
     this.setState({
23
-      isSeckill: isSeckill === "true",
24
-      shareJump,
25
-      tagTitle,
26
-      productDetail: JSON.parse(decodeURIComponent(productDetail)),
27
-      shareTitle: JSON.parse(decodeURIComponent(productDetail)).item_title,
28
-      shareUserId,
29
-      share_scheme_link:decodeURIComponent(share_scheme_link)
54
+      productDetail: res,
55
+      shareTitle: res.item_title,
30 56
     });
31 57
   }
58
+  // 获取分享跳转标识
59
+  getShareJump = async (id='') => {
60
+    // 设置分享路径
61
+    const share_extend = JSON.stringify({
62
+      path: `pages/indexSub/productDetail/index?id=${ id||this.state.productDetail.id}&isShare=true&isSeckill=${this.state.isSeckill}&shareType=11&&shareUserId=${this.state.shareUserId}`,
63
+    });
64
+    const res = await getShareJump({
65
+      share_type: 11,
66
+      share_id: id||this.state.productDetail.id,
67
+      share_extend,
68
+    });
69
+    this.setState({
70
+      shareJump: res.share_unique_value,
71
+      share_scheme_link: res.share_scheme_link,
72
+    });
73
+  };
32 74
   // 设置分享标题
33 75
   handleShareTitleChange = (newTitle) => {
34 76
     this.setState({
@@ -66,33 +108,35 @@ export default class Index extends Component {
66 108
 
67 109
   saveImage = async () => {
68 110
     Taro.showLoading({
69
-      title: '保存中...',
111
+      title: "保存中...",
70 112
     });
71 113
     const { images } = this.state.productDetail;
72 114
     try {
73 115
       for (const image of images) {
74
-        // 下载图片
75
-        const downloadRes = await Taro.downloadFile({
76
-          url: image.img
116
+        // 转换图片链接从 http 到 https
117
+        const imgUrl = image.img.startsWith("http:")
118
+          ? image.img.replace("http:", "https:")
119
+          : image.img;
120
+        // 先获取图片信息
121
+        const imageInfo = await Taro.getImageInfo({
122
+          src: imgUrl,
77 123
         });
78
-
79
-        // 保存到相册
124
+        // 使用图片路径保存
80 125
         await Taro.saveImageToPhotosAlbum({
81
-          filePath: downloadRes.tempFilePath
126
+          filePath: imageInfo.path,
82 127
         });
83 128
       }
84
-
85 129
       // 全部保存成功后提示
86 130
       Taro.showToast({
87 131
         title: "所有图片保存成功",
88 132
         icon: "success",
89
-        duration: 2000
133
+        duration: 2000,
90 134
       });
91 135
     } catch (error) {
92 136
       Taro.showToast({
93 137
         title: "保存失败",
94 138
         icon: "none",
95
-        duration: 2000
139
+        duration: 2000,
96 140
       });
97 141
     }
98 142
   };
@@ -110,9 +154,9 @@ export default class Index extends Component {
110 154
         Taro.showToast({
111 155
           title: "标题复制成功",
112 156
           icon: "none",
113
-          duration: 1500
157
+          duration: 1500,
114 158
         });
115
-      }
159
+      },
116 160
     });
117 161
   };
118 162
   // 复制链接
@@ -123,9 +167,9 @@ export default class Index extends Component {
123 167
         Taro.showToast({
124 168
           title: "链接复制成功",
125 169
           icon: "none",
126
-          duration: 1500
170
+          duration: 1500,
127 171
         });
128
-      }
172
+      },
129 173
     });
130 174
   };
131 175
   // 配置分享内容
@@ -165,7 +209,9 @@ export default class Index extends Component {
165 209
         <View className="text-box">
166 210
           <View className="text-box-top">
167 211
             <View className="text-box-title">文案素材</View>
168
-            <View onClick={this.handleCopyAll} className="text-box-save">复制全部内容</View>
212
+            <View onClick={this.handleCopyAll} className="text-box-save">
213
+              复制全部内容
214
+            </View>
169 215
           </View>
170 216
           <View className="text-box-content">
171 217
             {/* 商品标题盒子 */}
@@ -174,7 +220,10 @@ export default class Index extends Component {
174 220
                 <View className="text-box-content-title-top-title">
175 221
                   商品标题
176 222
                 </View>
177
-                <View onClick={this.handleCopyTitle} className="text-box-content-title-top-copy">
223
+                <View
224
+                  onClick={this.handleCopyTitle}
225
+                  className="text-box-content-title-top-copy"
226
+                >
178 227
                   复制标题
179 228
                 </View>
180 229
               </View>
@@ -204,16 +253,26 @@ export default class Index extends Component {
204 253
                 />
205 254
                 <View className="copy-box">
206 255
                   <View className="copy-box-line"></View>
207
-                  <Image onClick={this.handleCopyLink} className="copy-box-icon" src='https://yushi.tos-cn-beijing.volces.com/productDetail/copy.png' />
256
+                  <Image
257
+                    onClick={this.handleCopyLink}
258
+                    className="copy-box-icon"
259
+                    src="https://yushi.tos-cn-beijing.volces.com/productDetail/copy.png"
260
+                  />
208 261
                 </View>
209 262
               </View>
210 263
             </View>
211 264
           </View>
212 265
         </View>
213 266
         {/* 分享按钮 */}
214
-        <Button className="share-button" openType="share" onClick={this.handleShare}>
215
-          直接分享商品卡片到聊天框
216
-        </Button>
267
+        {this.state.shareJump && (
268
+          <Button
269
+            className="share-button"
270
+            openType="share"
271
+            onClick={this.handleShare}
272
+          >
273
+            直接分享商品卡片到聊天框
274
+          </Button>
275
+        )}
217 276
       </View>
218 277
     );
219 278
   }

+ 3 - 1
src/pages/indexSub/sharePage/index.less

@@ -42,13 +42,15 @@
42 42
         .image-box-content {
43 43
             display: flex;
44 44
             flex-wrap: nowrap;
45
-            overflow-x: auto;
45
+            overflow-x: scroll;
46 46
 
47 47
             .image-box-content-item {
48 48
                 width: 240px;
49 49
                 height: 240px;
50 50
                 border-radius: 40px;
51 51
                 margin-right: 20px;
52
+                background-color: #f7f8fa;
53
+                flex-shrink: 0;
52 54
             }
53 55
         }
54 56
     }