微信小程序 分享 onShareAppMessage onShareTimeline 使用 async promies 的坑和方式

开发环境:uniapp window uview

开发背景:分享的链接参数是接口生成的,所以每次分享的时候需要进行调用一个接口。为了实现这个目标,想到用async 来等待接口请求完了,在分享

实际测试:

测试分享好友:测试结果是成功,分享的url,确实会带上接口返回的参数

async onShareAppMessage(res) {
			const {data} = await this.$u.api.getShareOther();
			return {
				title: '分享文本',
				path: `/pages/post/post-details?scene=${data.shareScene}`,
				imageUrl: ''
			}
}

测试分享朋友圈:测试结果是失败了,接口有调用,但是分享的url,没有带上接口返回参数,

async onShareTimeline(res) {
			const {data} = await this.$u.api.getShareOther();
			return {
				title: '分享朋友圈文本',
				path: `/pages/post/post-details?scene=${data.shareScene}`,
				imageUrl: ''
			}
}

分析原因:

查看微信小程序官网的文档

虽然onShareAppMessage分享朋友和onShareTimeline分享朋友圈的写法是一样的,但onShareAppMessage api 官网分享朋友api有提供promies的例子,而onShareTimeline是没有的,微信分享朋友圈的api还是beta版本的,暂不支持这个写法。

解决办法:

分享朋友是可以分享时候去调用接口,在得到参数再拼接分享链接,有个缺点是,这会导致分享的朋友的时候,会停顿一下。分享朋友圈,这种做法就不行了。我这边做的时候,给的最佳方案就是在页面进来的时候就去请求接口,把要分享的url拼接好。

猜你喜欢

转载自blog.csdn.net/web_ding/article/details/123527665