Uniapp 微信小程序分享 - 自定义卡片内容 + 参数携带 接收

技术栈:Uniapp + Vue3

简介

因实际需求,需要实现微信小程序自定义分享(自定义分享卡片标题、图片、小程序路径、参数等)。
为了实现自定义分享,需要

  1. 本微信小程序开通分享功能,开启uni.showShareMenu(二次封装的wx.setShareMenu
  2. 分享按钮设置 open-type=“share”
  3. onShareAppMessage 回调函数中配置参数
  4. onLoad内接收参数
  5. onShow内接收参数(如果需要)

关于onShareAppMessage内入参、出参,可查看微信官方文档

实现

DOM

<button open-type="share" class="share-btn">
	<image class="share-img" mode="scaleToFill" src="@/static/images/share-icon.png"></image>
</button>

JS

下面分享配置分两种情况:

  1. 普通情况(纯手写分享配置)
  2. 使用了uview-plus mixin mpShare情况

因本小程序使用了uview-plus组件,其内部封装了 share对象,外部 onShareAppMessage 内返回的配置对象不会生效,需要设置uview-plus内的share对象。

普通情况

import { onLoad, onShow, onShareAppMessage } from '@dcloudio/uni-app';

// options 路由参数
onLoad((options) => {
	// 开启分享菜单
	uni.showShareMenu({
		withShareTicket: true,
		// shareAppMessage 分享到好友
		// shareTimeline 分享到朋友圈
		menus: ['shareAppMessage', 'shareTimeline']
	});
	const inviteCode = options.inviteCode // 接收参数
})


// 分享
onShareAppMessage((res) =>{
	const inviteCode = '7788';
	return {
		title: '外卖单单省,5元点大牌', // 标题
		path: '/pages/tabbar/home/index '+ `?inviteCode=` + inviteCode, // 小程序路径
		imageUrl: 'https://zj-biz-gov-free-eat.oss-cn-hangzhou.aliyuncs.com/free-eat/static-example/share-default.png' // 图片
	}
});

使用了uview-plus 的mpShare

import { getCurrentInstance } from 'vue';
import { onLoad, onShow, onShareAppMessage } from '@dcloudio/uni-app';

const that = getCurrentInstance();

// options 路由参数
onLoad((options) => {
	// 开启分享菜单
	uni.showShareMenu({
		withShareTicket: true,
		// shareAppMessage 分享到好友
		// shareTimeline 分享到朋友圈
		menus: ['shareAppMessage', 'shareTimeline']
	});
	const inviteCode = options.inviteCode // 接收参数
})


// 分享
onShareAppMessage((res) =>{
	const inviteCode = '7788';
	that.proxy.mpShare.title = '外卖单单省,5元点大牌';
	that.proxy.mpShare.path = '/pages/tabbar/home/index';
	that.proxy.mpShare.path = that.proxy.mpShare.path + `?inviteCode=` + inviteCode;
	that.proxy.mpShare.imageUrl = 'https://zj-biz-gov-free-eat.oss-cn-hangzhou.aliyuncs.com/free-eat/static-example/share-default.png';
});

onShow接受参数

wx.getLaunchOptionsSync:获取小程序启动时的参数。这个用于获取小程序启动时的参数,例如通过点击某个小程序卡片启动的,可以通过这个API获取到卡片的相关信息。

import { ref } from 'vue';
import { onShow } from '@dcloudio/uni-app';

const inviteCode = ref();

onShow(() => {
	let launchOptions = wx.getLaunchOptionsSync();
	if(launchOptions.query?.inviteCode) 
		inviteCode.value = launchOptions.query?.inviteCode;
})

效果

分享卡片

### 实现 Uniapp 微信小程序分享功能 在 Uniapp 开发环境中实现微信小程序分享功能主要涉及 `onShareAppMessage` 方法的应用。此方法用于自定义页面转发的内容,当用户点击右上角菜单中的“转发”选项时触发。 对于希望传递动态参数的情况,在绑定带有 `open-type="share"` 属性的按钮时需要注意事件处理逻辑[^4]。具体来说: #### 自定义分享内容 为了使分享更具有吸引力并能携带特定数据,可以通过设置返回对象来指定标题、路径以及缩略图等信息。以下是具体的代码示例: ```javascript // pages/index/index.vue 或者其他页面文件内 export default { methods: { onShareAppMessage(res) { if (res.from === 'button') { // 来源于底部或顶部转发按钮 console.log('来自页面内转发按钮'); } return { title: '这是分享标题', // 可以根据业务需求调整 path: '/pages/index/index?param=value', // 带有查询字符串的目标页面地址 imageUrl: 'https://example.com/image.png' // 分享卡片图片链接 }; }, customShare() { uni.share({ provider: "weixin", scene: "WXSceneSession", // WXSceneTimeline 表示朋友圈;默认为会话聊天窗口 type: 0, href: "http://yourdomain.com/page.html", title: "这是一个测试分享", summary: "这里是描述文字", success: function () { console.log("success"); }, fail: function (err) { console.error(err); } }); } } } ``` 上述代码展示了两种不同的方式来进行分享操作:一种是通过监听 `onShareAppMessage` 函数响应用户的常规分享行为;另一种则是利用 `uni.share()` API 调用来主动发起分享动作,适用于某些场景下需要由程序控制而非用户手动触发展开的操作。 值得注意的是,如果要让接收方能够接收到被分享者的身份或其他附加信息,则可以在构建目标 URL 的时候加入相应的参数,并确保该页面具备解析这些参数的能力以便后续使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值