浏览代码

refactor(miniprogram): 优化分享功能并添加自定义分享页面

- 修改学校和船厂页面的分享逻辑,增加自定义分享标题和路径
- 添加 share 方法以设置分享相关数据
- 更新 wxml 文件,将 share 方法绑定到分享按钮
- 为学校和船厂分别创建独立的分享页面,携带相关 ID 参数
wzg 8 月之前
父节点
当前提交
87e9f38ef3

+ 9 - 11
miniprogram/pages/school/school.js

@@ -55,20 +55,18 @@ Page({
   /**
    * 用户点击右上角分享
    */
-  onShareAppMessage(res) {
-    if (res.from === "button") {
-      const title = res.target.dataset.title || "海员培训学校通知";
-      return {
-        title: title,
-        path: "/pages/school/school",
-      };
-    }
+  share(e) {
+    this.setData({
+      shareNoticeId: e.currentTarget.dataset.id,
+      shareNoticeTitle: e.currentTarget.dataset.title,
+    });
+  },
+  onShareAppMessage() {
     return {
-      title: "海员培训学校通知",
-      path: "/pages/school/school",
+      title: this.data.shareNoticeTitle,
+      path: `/pages/school/sharePage/sharePage?noticeId=${this.data.shareNoticeId}`,
     };
   },
-
   makePhoneCall(e) {
     const phone = e.currentTarget.dataset.phone;
     if (phone) {

+ 1 - 1
miniprogram/pages/school/school.wxml

@@ -50,7 +50,7 @@
             <!-- <button class="contact-btn" bindtap="makePhoneCall" data-id="{{item.id}}" data-phone="{{item.contactPhone}}">
             拨打电话
           </button> -->
-            <button class="share-btn" open-type="share" data-title="{{item.trainingNoticeTitle}}">
+            <button class="share-btn" open-type="share" bindtap="share" data-id="{{item.id}}" data-title="{{item.trainingNoticeTitle}}">
               分享公告
             </button>
           </view>

+ 9 - 5
miniprogram/pages/shipyard/shipyard.js

@@ -54,14 +54,18 @@ Page({
   /**
    * 用户点击右上角分享
    */
-  onShareAppMessage(e) {
-    const title = e.target ? e.target.dataset.title : "船厂服务";
+  share(e) {
+    this.setData({
+      shipyardId: e.currentTarget.dataset.id,
+      shipyardName: e.currentTarget.dataset.title,
+    });
+  },
+  onShareAppMessage() {
     return {
-      title: title,
-      path: "/pages/shipyard/shipyard",
+      title: this.data.shipyardName,
+      path: `/pages/shipyard/sharePage/sharePage?shipyardId=${this.data.shipyardId}`,
     };
   },
-
   makePhoneCall(e) {
     const phoneNumber = e.currentTarget.dataset.phone;
     if (phoneNumber) {

+ 3 - 1
miniprogram/pages/shipyard/shipyard.wxml

@@ -42,7 +42,9 @@
           </button>
         </view>
       </view>
-      <button class="share-btn" open-type="share" data-title="{{item.shipyardName}}">分享船厂</button>
+      <button class="share-btn" open-type="share" bindtap="share" data-id="{{item.id}}" data-title="{{item.shipyardName}}">
+        分享船厂
+      </button>
     </view>
   </view>
   <view class="tac fs28 c7 p40" wx:else>暂无数据</view>