uniapp微信小程序 分享功能

uniapp

https://zh.uniapp.dcloud.io/api/plugins/share.html#onshareappmessage

export default {
  onShareAppMessage(res) {
    if (res.from === 'button') {// 来自页面内分享按钮
      console.log(res.target)
    }
    return {
      title: '自定义分享标题',
      path: '/pages/test/test?id=123'
    }
  }
}

需要再真机上看具体效果(以下是我的代码)
在这里插入图片描述

utils页面(封装起来了)

function share(image, title = "你好呀", path = this.getCurrentPageUrlWithArgs()) {
	if (!image) {
		image = "https://static.taidoukeji.cn/wxchat/yww/oilBgc.jpg"
	}
	return {
		title: title,
		path: path,
		imageUrl: image
	}
}
function shareApplets(image, title = "你好呀", path = this.getCurrentPageUrlWithArgs()) {
	if (!image) {
		image = "https://static.taidoukeji.cn/wxchat/yww/oilBgc.jpg"
	}
	return {
		title: title,
		query: path,
		imageUrl: image
	}
}
function getCurrentPageUrlWithArgs(defUrl = null) {
	var pages = getCurrentPages() //获取加载的页面 
	var currentPage = pages[pages.length - 1] //获取当前页面的对象 
	var url = "/" + currentPage.route //当前页面url 
	if (defUrl) url = defUrl;
	var options = currentPage.options //如果要获取url中所带的参数可以查看option
	if (getApp().globalData.memberId) {
		options.shareId = getApp().globalData.memberId
	}
	//拼接url的参数 
	var urlWithArgs = url + "?"
	for (var key in options) {
		var value = options[key]
		urlWithArgs += key + "=" + value + "&"
	}
	urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
	return urlWithArgs
}
export default {
	getCurrentPageUrlWithArgs,
	share,
	shareApplets
}

需要使用页面

onShareAppMessage() {
		return this.$utils.share('https://static.taidoukeji.cn/wxchat/yww/oilBgc.jpg?', "您的好友邀请您优惠加油啦!")
	},
//获取你的昵称getApp().globalData.userInfo.nickName
// onShareAppMessage() {
// 	return this.$utils.share('https://static.taidoukeji.cn/wxchat/yww/oilBgc.jpg?', "您的好友" + getApp().globalData.userInfo.nickName + "邀请您优惠加油啦!")
// },
onShareTimeline(){
	return this.$utils.shareApplets();
},
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值