1. 程式人生 > >小程式頁面內轉發到群並獲取微信群標識

小程式頁面內轉發到群並獲取微信群標識

1.首先需要在分享方法裡面設定

wx.showShareMenu({

withShareTicket: true

});

2.在分享成功返回後呼叫微信獲取群資訊方法來獲取必要的引數

if (res.from === 'button') {//頁面內轉發

return {

title: "標題",

path: "pages/sqpage/sqpage",

success: function (res) {

var shareTickets = res.shareTickets;

if (shareTickets.length == 0) {

return false;

}

wx.getShareInfo({

shareTicket: shareTickets[0],

success: function (res) {

console.log(res);

var encryptedData = res.encryptedData;

encryptedData = decodeURIComponent(encryptedData);//解密獲取的encryptedData

var iv = res.iv;

iv = decodeURIComponent(iv);//解密獲取的iv

}

3.呼叫微信登入方法來獲取code值然後和後臺配合來解析出Gopenid

wx.login({

success: function (res) {

var code = res.code;

var signature = signa.signaturetik('token=' + token, 'userAccessToken=' + userAccessToken, 'code=' + code, 'encryptedData=' + encryptedData, 'iv=' + iv);

wx.request({

url: "------------",//寫上你後臺伺服器路徑(用途:解析出Gopenid)

header: {

'content-type': 'application/x-www-form-urlencoded'

},

method: 'POST',

data: { 'code': code, 'encryptedData': encryptedData, "iv": iv, 'userAccessToken': userAccessToken, 'signature': signature, 'token': token},

success: function (res) {

console.log(res);

}

})

}

})

4.整體流程效果

onShareAppMessage: function (res) {

console.log(res);

var that = this;

wx.showShareMenu({

withShareTicket: true

});

if (res.from === 'button') {

return {

title: "電力釋出小程式",

path: "pages/sqpage/sqpage",

success: function (res) {

console.log(res);

var shareTickets = res.shareTickets;

if (shareTickets.length == 0) {

return false;

}

wx.getShareInfo({

shareTicket: shareTickets[0],

success: function (res) {

console.log(res);

var encryptedData = res.encryptedData;

encryptedData = decodeURIComponent(encryptedData);

var iv = res.iv;

iv = decodeURIComponent(iv);

var token = signa.tokenmake();

wx.login({

success: function (res) {

var code = res.code;

var signature = signa.signaturetik('token=' + token, 'userAccessToken=' + userAccessToken, 'code=' + code, 'encryptedData=' + encryptedData, 'iv=' + iv);

wx.request({

url: "後臺伺服器路徑",//寫上你後臺伺服器路徑(用途:解析出Gopenid)

header: {

'content-type': 'application/x-www-form-urlencoded'

},

method: 'POST',

data: { 'code': code, 'encryptedData': encryptedData, "iv": iv, 'userAccessToken': userAccessToken, 'signature': signature, 'token': token},

success: function (res) {

console.log(res);

}

})

}

})

}

})

},

fail: function () {

 

}

}

}

}