微信小程序-页面分享 onShareAppMessage

效果


js

let app = getApp();
Page({
  data: {
    img: "/images/1.jpg"
  },
  onLoad() {
  },
  showShareMenu() {
    wx.showShareMenu();
    console.log("显示了当前页面的转发按钮");
  },
  hideShareMenu() {
    wx.hideShareMenu();
    console.log("隐藏了当前页面的转发按钮");
  },
  onShareAppMessage: (res) => {
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    }
    else {
      console.log("来自右上角转发菜单")
    }
    return {
      title: '妹子图片',
      path: '/pages/share/share?id=123',
      imageUrl: "/images/1.jpg",
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})

html

<view class="view">
  <image class="cover-9" src="{{img}}" bindtap="img"></image>
  <view class="window-1">
    <button type="default" id="open" bindtap="showShareMenu">开启分享</button>
    <button type="warn" id="close" bindtap="hideShareMenu">关闭分享</button>
  </view>
  <button type="primary" open-type="share" data-name="pageShare" id="share">点击分享</button>
</view>

css

page{
  height: 100%;
}
.view{
  width: 100%;
  height: 100%;
}
.window-1{
  display: flex;
  flex-direction: row;
  margin: 20rpx 0;
}
.cover-9{
  width: 688rpx;
  height: 75%;
  margin: 0 30rpx;
  border:2rpx solid;
  border-radius:5px;  
}
button{
  margin: 0 10rpx;
  width: 100%;
}
#share{
  width: 730rpx;
}

意外金喜的博客:http://blog.csdn.net/zzwwjjdj1

更多小程序文章:http://blog.csdn.net/zzwwjjdj1/article/details/79351547
关注公众号:


### 微信小程序分享功能的封装方法 在微信小程序开发中,`onShareAppMessage` 和 `onShareTimeline` 是实现社交分享的核心 API。以下将详细介绍如何封装这两个方法以实现全局分享功能,并提供具体的代码示例。 #### 1. 封装 `onShareAppMessage` `onShareAppMessage` 是用于用户点击右上角菜单进行分享时触发的回调函数[^2]。通过封装该方法,可以在多个页面中复用分享逻辑。 ```javascript // share.js 文件内容 export function onShareAppMessageDefault(title, path, imageUrl) { return { title: title || '默认分享标题', // 分享标题 path: path || '/pages/index/index', // 分享路径 imageUrl: imageUrl || 'https://example.com/default-share-image.png', // 分享图片 }; } ``` 在主文件中引入并配置全局分享逻辑: ```javascript // main.js import { onShareAppMessageDefault } from './share'; App({ onLaunch() { // 全局混入分享逻辑 this.globalData = { shareConfig: onShareAppMessageDefault, }; }, }); ``` 在页面中使用封装的 `onShareAppMessage` 方法: ```javascript Page({ onShareAppMessage() { const { shareConfig } = getApp().globalData; return shareConfig('自定义标题', '/pages/custom/custom', 'https://example.com/custom-share-image.png'); }, }); ``` #### 2. 封装 `onShareTimeline` `onShareTimeline` 是用于用户点击“分享到朋友圈”按钮时触发的回调函数[^3]。与 `onShareAppMessage` 类似,可以通过封装实现全局复用。 ```javascript // share.js 文件内容 export function onShareTimelineDefault(title, query, imageUrl) { return { title: title || '默认朋友圈分享标题', // 分享标题 query: query || '', // 分享路径携带的参数 imageUrl: imageUrl || 'https://example.com/default-timeline-image.png', // 分享图片 }; } ``` 在主文件中引入并配置全局分享逻辑: ```javascript // main.js import { onShareTimelineDefault } from './share'; App({ onLaunch() { this.globalData = { timelineConfig: onShareTimelineDefault, }; }, }); ``` 在页面中使用封装的 `onShareTimeline` 方法: ```javascript Page({ onShareTimeline() { const { timelineConfig } = getApp().globalData; return timelineConfig('自定义朋友圈标题', 'customParam=123', 'https://example.com/custom-timeline-image.png'); }, }); ``` #### 3. 注意事项 - 在封装过程中,确保默认值能够满足大多数场景的需求,同时允许开发者根据具体需求动态修改分享内容。 - 使用 `app.mixin()` 方法可以进一步简化全局分享逻辑的配置[^2],但需要注意官方文档中的不推荐提示。 - 确保分享图片符合微信平台的要求,例如图片尺寸和清晰度[^4]。 #### 4. 实现效果 通过上述封装方法,开发者可以在多个页面中轻松实现一致的分享功能,同时支持动态调整分享内容[^5]。 ---
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值