背景:由于公司可能需要在微信群里面使用打卡功能,因此做了個(gè)技術(shù)調(diào)研。
方案:微信在更新分享接口后,原有的在onShareAppMessage中直接拿shareTicket已不復(fù)存在。根據(jù)最新文檔顯示,需要在App.onLaunch()跟App.onShow()中獲取。
Demo核心代碼:
index.js
-
Page({
-
-
/**
-
* 頁面的初始數(shù)據(jù)
-
*/
-
data: {
-
openGid: ''
-
},
-
-
/**
-
* 生命周期函數(shù)--監(jiān)聽頁面加載
-
*/
-
onLoad: function (options) {
-
let that = this
-
wx.showShareMenu({
-
withShareTicket: true
-
})
-
app.getShareTiket(function (globalData) {
-
console.log('clickReload---globalData-->' + JSON.stringify(globalData))
-
that.setData({
-
openGid: globalData.openGid
-
})
-
})
-
},
-
clickReload: function () {
-
let that = this
-
app.getShareTiket(function (globalData) {
-
console.log('clickReload---globalData-->' + JSON.stringify(globalData))
-
that.setData({
-
openGid: globalData.openGid
-
})
-
})
-
}
-
})
index.wxml
-
<!--index.wxml-->
-
<view wx:if="{{openGid}}" class='groupName'>
-
群名稱:<open-data type="groupName" open-gid="{{openGid}}"></open-data>
-
</view>
-
-
<view wx:else>
-
<button bindtap='clickReload'>點(diǎn)擊加載群名稱</button>
-
</view>
-
-
<view>{{openGid ? openGid : '無'}}</view>
app.js
-
//app.js
-
App({
-
globalData: {
-
shareTicket: '',
-
openGid: ''
-
},
-
onLaunch: function (options) {
-
-
},
-
onShow: function (options) {
-
let that = this
-
if (options && options.scene == 1044) {
-
that.globalData.shareTicket = options.shareTicket
-
}
-
console.log('onShow---options=--->' + JSON.stringify(options))
-
},
-
getShareTiket: function (cb) {
-
let that = this
-
// 展示本地存儲(chǔ)能力
-
if (that.globalData.shareTicket) {
-
wx.getShareInfo({
-
shareTicket: that.globalData.shareTicket,
-
success: function (res) {
-
console.log('getShareTiket---shareTicket-->' + JSON.stringify(res))
-
let js_encryptedData = res.encryptedData
-
let js_iv = res.iv
-
wx.login({
-
success: function (res) {
-
let js_code = res.code
-
console.log('code-->' + js_code)
-
wx.request({
-
url: 'xxxxxxxx',
-
method: 'POST',
-
data: {
-
code: js_code,
-
appId: 'xxxxx',
-
encryptedData: js_encryptedData,
-
iv: js_iv
-
},
-
success: function (res) {
-
that.globalData.openGid = res.data.openGId
-
console.log('getShareTiket---openGid' + that.globalData.openGid)
-
typeof cb == "function" && cb(that.globalData)
-
},
-
fail: function (err) {
-
console.log('getShareTiket---err' + JSON.stringify(err))
-
}
-
})
-
}
-
})
-
}
-
})
-
} else {
-
console.log('不存在shareTicket')
-
}
-
}
-
})
注意事項(xiàng)
1:必須調(diào)用這個(gè)接口wx.showShareMenu({withShareTicket: true}),否則在App.onLaunch()跟App.onShow()時(shí),你拿不到shareTicket.
|