H5微信分享功能封装

学习分享1年前 (2022)更新 bestcyt
274 0 0

话不多说直接上代码

公共方法

// 分享功能
static wechatShare(shareData, url) {
	fetch(api.createJsapiSignature + url, {}, 'get').then((res) => {
		let that = this
		let obja = res[1].data.data
		let b = {
			debug: false,
			jsApiList: [
				'checkJsApi', //检测接口权限
				'updateTimelineShareData',
				'updateAppMessageShareData',
				'onMenuShareAppMessage', //老版本分享接口(注意这两个旧版本的必须要放的不然会有问题)
				'onMenuShareTimeline' //老版本分享接口。
			]
		}
		jWeixin.config(Object.assign(obja, b));
		jWeixin.ready(() => {
			that.share(shareData);
		});
		jWeixin.error(function(res) {
			console.log(res.errMsg);
		})
		jWeixin.checkJsApi({
			jsApiList,
			success: function(res) {
				// return res
				console.log('判断客户端支持js接口', res)
			},
		})
	})
};
static share(param) {
	var title = param.title
	var imgUrl = param.image
	var description = param.desc
	var url = param.url
	var tempParam = {
		title: title,
		desc: description,
		link: url,
		imgUrl: imgUrl,
		success: (res) => {
			console.log(res)
		},
		cancel: function(res) {
			console.log('分享失败', res)
		},
	}
	// //分享给朋友
	jWeixin.onMenuShareAppMessage(tempParam)
	// //分享给朋友圈
	jWeixin.onMenuShareTimeline(tempParam)
	// // 分享给朋友
	jWeixin.updateAppMessageShareData(tempParam)
	jWeixin.updateTimelineShareData(tempParam)
}

 

页面使用

onLoad(options) {
    this.share()
},

methods: {
    share() {
      this.$myUtils.wechatShare({
	title:'小剧场',
	desc:'这里有让你爽的剧,一起来关注吧',
	url: window.location.href,
	image: 'https://qxxxxncs.com/static/ph.png', // 分享图标
	},window.location.href);

    }
}

 

© 版权声明

相关文章