Skip to content

分享

作者:Yuan Tang
更新于:5 个月前
字数统计:1.8k 字
阅读时长:7 分钟

小程序拥有分享给好友和分享到朋友圈的功能,通过点击右上角选择功能实现。

iSaimz.png

但是分享功能的实现需要开发者在对应页面设置 onShareAppMessageonShareTimeline 事件,如果不设置,则无法正常使用分享功能,例如:

iSaDqL.png

分享给好友

分享给好友需要用到 onShareAppMessage 方法,可获取三个参数:

参数类型说明最低版本
fromString转发事件来源。 button:页面内转发按钮; menu:右上角转发菜单
1.2.4
https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html
targetObject如果 from 值是 button,则 target 是触发这次转发事件的 button,否则为 undefined
1.2.4
https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html
webViewUrlString页面中包含
web-view
https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
组件时,返回当前
web-view
https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
的url
1.6.4
https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html

此事件处理函数需要 返回 一个 对象,用于自定义转发内容,返回内容如下:

字段说明默认值最低版本
title转发标题当前小程序名称
path转发路径当前页面 path ,必须是以 / 开头的完整路径
imageUrl自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。使用默认截图
1.5.0
https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html
promise如果该参数存在,则以 resolve 结果为准,如果三秒内不 resolve,分享会使用上面传入的默认参数
2.12.0
https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html

示例代码

获取本地存储的用户信息数据对象中的邀请码。

js
onShareAppMessage() {
	return {
		title: '抓猫带货精选',
		path: `/pages/tabbar/Index/Index?code=${uni.getStorageSync('userInfo').code}`,
	}
},

分享效果

iSuift.png

分享朋友圈

分享朋友圈与分享给好友做法大同小异,需要使用到 onShareTimeline 事件方法,需要返回一个对象,包括自定义标题、携带的参数以及自定义图片路径。

字段说明默认值最低版本
title自定义标题,即朋友圈列表页上显示的标题当前小程序名称
query自定义页面路径中携带的参数,如 path?a=1&b=2 的 “?” 后面部分当前页面路径携带的参数
imageUrl自定义图片路径,可以是本地文件或者网络图片。支持 PNG 及 JPG,显示图片长宽比是 1:1。默认使用小程序 Logo

示例代码

js
onShareTimeline() {
	return {
		title: '抓猫带货精选',
		query: `code=${uni.getStorageSync('userInfo').code}`,
	}
},

效果实现

iSuCfz.jpeg

注意事项

分享到朋友圈使用的是单页模式,即用户在朋友圈打开分享的小程序页面,并不会真正打开小程序,而是进入一个“小程序单页模式”的页面。打开的单页模式是不允许有交互的。

因此,分享朋友圈不支持自定义路径,query 参数只能传参数。

单页模式下的限制

小程序“单页模式”适用于纯内容展示场景,可实现的交互与接口能力有限,因此存在如下限制:

  1. 页面无登录态,与登录相关的接口,如 wx.login 均不可用;云开发资源需开启未登录访问方可在单页模式下使用,详见
    未登录模式
    https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/identityless.html
  2. 不允许跳转到其它页面,包括任何跳小程序页面、跳其它小程序、跳微信原生页面
  3. 不允许横屏使用
  4. 若页面包含 tabBar,tabBar 不会渲染,包括自定义 tabBar
  5. 本地存储与小程序普通模式不共用

对于一些会产生交互的组件或接口,在点击后调用时,会弹 toast 提示“请前往小程序使用完整服务”。为达到良好的用户体验,请注意适配单页模式的接口能力,请勿大量使用被禁用的接口或组件。

禁用能力列表:

分类功能点
组件
button open-type
https://developers.weixin.qq.com/miniprogram/dev/component/button.html
camera
https://developers.weixin.qq.com/miniprogram/dev/component/camera.html
editor
https://developers.weixin.qq.com/miniprogram/dev/component/editor.html
form
https://developers.weixin.qq.com/miniprogram/dev/component/form.html
functional-page-navigator
https://developers.weixin.qq.com/miniprogram/dev/component/functional-page-navigator.html
live-pusher
https://developers.weixin.qq.com/miniprogram/dev/component/live-pusher.html
navigator
https://developers.weixin.qq.com/miniprogram/dev/component/navigator.html
navigation-bar
https://developers.weixin.qq.com/miniprogram/dev/component/navigation-bar.html
official-account
https://developers.weixin.qq.com/miniprogram/dev/component/official-account.html
open-data
https://developers.weixin.qq.com/miniprogram/dev/component/open-data.html
web-view
https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
路由
wx.redirectTo
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.redirectTo.html
wx.reLaunch
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.reLaunch.html
wx.navigateTo
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
wx.switchTab
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.switchTab.html
wx.navigateBack
https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
界面
导航栏
https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.showNavigationBarLoading.html
Tab Bar
https://developers.weixin.qq.com/miniprogram/dev/api/ui/tab-bar/wx.showTabBarRedDot.html
网络
mDNS
https://developers.weixin.qq.com/miniprogram/dev/api/network/mdns/wx.startLocalServiceDiscovery.html
UDP 通信
https://developers.weixin.qq.com/miniprogram/dev/api/network/udp/wx.createUDPSocket.html
数据缓存
周期性更新
https://developers.weixin.qq.com/miniprogram/dev/api/storage/background-fetch/wx.getBackgroundFetchData.html
媒体
VoIP
https://developers.weixin.qq.com/miniprogram/dev/api/media/voip/wx.joinVoIPChat.html
wx.chooseMedia
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseMedia.html
wx.chooseImage
https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html
wx.saveImageToPhotosAlbum
https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.saveImageToPhotosAlbum.html
wx.chooseVideo
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseVideo.html
wx.saveVideoToPhotosAlbum
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.saveVideoToPhotosAlbum.html
wx.getVideoInfo
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.getVideoInfo.html
wx.compressVideo
https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.compressVideo.html
位置
wx.openLocation
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.openLocation.html
wx.chooseLocation
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html
wx.startLocationUpdateBackground
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.startLocationUpdateBackground.html
wx.startLocationUpdate
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.startLocationUpdate.html
转发
wx.getShareInfo
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.getShareInfo.html
wx.showShareMenu
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.showShareMenu.html
wx.hideShareMenu
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.hideShareMenu.html
wx.updateShareMenu
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.updateShareMenu.html
文件
wx.openDocument
https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html
开放接口
登录
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
小程序跳转
https://developers.weixin.qq.com/miniprogram/dev/api/navigate/wx.navigateToMiniProgram.html
用户信息
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
支付
https://developers.weixin.qq.com/miniprogram/dev/api/payment/wx.requestPayment.html
授权
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
设置
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/setting/wx.openSetting.html
收货地址
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/address/wx.chooseAddress.html
卡券
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/card/wx.openCard.html
发票
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/invoice/wx.chooseInvoiceTitle.html
生物认证
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/soter/wx.startSoterAuthentication.html
微信运动
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html
微信红包
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/redpackage/wx.showRedPackage.html
设备
蓝牙
https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth/wx.startBluetoothDevicesDiscovery.html
iBeacon
https://developers.weixin.qq.com/miniprogram/dev/api/device/ibeacon/wx.startBeaconDiscovery.html
Wi-Fi
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/(wx.startWiFi
) 、
NFC
https://developers.weixin.qq.com/miniprogram/dev/api/device/nfc-hce/wx.startHCE.html
联系人
https://developers.weixin.qq.com/miniprogram/dev/api/device/contact/wx.addPhoneContact.html
剪贴板
https://developers.weixin.qq.com/miniprogram/dev/api/device/clipboard/wx.setClipboardData.html
电话
https://developers.weixin.qq.com/miniprogram/dev/api/device/phone/wx.makePhoneCall.html
扫码
https://developers.weixin.qq.com/miniprogram/dev/api/device/scan/wx.scanCode.html
广告
ad
https://developers.weixin.qq.com/miniprogram/dev/component/ad.html
wx.createRewardedVideoAd
https://developers.weixin.qq.com/miniprogram/dev/api/ad/wx.createRewardedVideoAd.html
wx.createInterstitialAd
https://developers.weixin.qq.com/miniprogram/dev/api/ad/wx.createInterstitialAd.html

注意事项

  1. 低版本微信客户端打开时,会进入一个升级提示页面
  2. 不支持在小程序页面内直接发起分享
  3. 自定义分享内容时不支持自定义页面路径
  4. 存在 web-view 组件的页面不支持发起分享
  5. 支持打开开发版、体验版,无权限人员进入时页面会提示无权限

Contributors

Yuan Tang