由于微信官方对分享接口的调整,导致之前写的分享出现了bug。现在我们就来说下分享接口调整之后群分享功能的实现吧!
接口调整:https://developers.weixin.qq.com/community/develop/doc/0000447a5b431807af57249a551408
接口调整过后实现代码如下:
detail.js
onShareAppMessage: function(res) { var that = this; var url = that.data.id + '&type=' + that.data.type + '&article_type=' + that.data.article_type + '&title=' + that.data.arr.title + '&types=' + types + '&user_id=' + app.globalData.uid; return { title: that.data.arr.title, path: '/pages/index/index?source=detail&id=' + url, // 当打开分享链接的时候跳转到小程序首页,并设置参数 } }index.js
var redirect = require('../../utils/redirect.js')const app = getApp()Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { wx.showShareMenu({ withShareTicket: true }) //从分享链接点进来的用户 if (app.globalData.uid && app.globalData.userInfo.medlive_id){ if (options.source == 'deil') { //朋友圈分享图片识别小程序码进入 var url = options.id + '&type=' + options.type + '&article_type=' + options.article_type; redirect.navigateTo('../detail/detail?id=' + url); } }else{//如果是新用户点击分享的链接则先跳转到微信登录页面 redirect.reLaunch('/pages/login/wx_login') } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { var that = this; return { title: '医知源肿瘤版', path: '/pages/index/index?id=0000&types=index&title=首页页面&user_id=' + app.globalData.uid, } },})app.js
onShow: function (options) { var that = this; that.globalData.shareTicket = options.shareTicket that.globalData.scene = options.scene console.log(options.query); var types = options.query.types; var id = options.query.id; var title = options.query.title; var userId = options.query.user_id; if (options.shareTicket){ console.log(options.query, 'options'); wx.getShareInfo({ shareTicket: options.shareTicket, success: function (res) { console.log(res,'detailes') var url_share = config.DOMAIN_API.shareWechat; // //请求内容 var data_share = { user_id: that.globalData.uid, content_id: id, title: title, type: types, share_user_id: userId, iv: res.iv, encryptedData: res.encryptedData, code: that.globalData.code, } util.ajaxPost(url_share, data_share).then((res) => { //成功处理 console.log('分享群成功', res) }).catch((errMsg) => { //错误处理,已统一处理,此处可以不需要 console.log(errMsg); }); } }) } },里面有的页面携带的参数未去掉,你们自动免疫吧。太累了,摸索了好长时间才走通。好累!注意有的不必要的代码是需要看看自己的项目需不需要再添加,有的代码是为了对其他的功能进行操作的(未去掉)。
微信小程序













