微信小程序如何獲取群聊的openGid以及名稱詳解
背景:由于公司可能需要在微信群里面使用打卡功能,因此做了個(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.
2:微信開發(fā)者工具可以模擬1044的場(chǎng)景,但是不會(huì)顯示群名稱,因?yàn)槟悴辉谌豪?。所以測(cè)試的時(shí)候,自己拉個(gè)微信群,然后分享到測(cè)試群,就能拿到群名稱。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用electron實(shí)現(xiàn)百度網(wǎng)盤懸浮窗口功能的示例代碼
這篇文章主要介紹了使用electron實(shí)現(xiàn)百度網(wǎng)盤懸浮窗口功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10web3.js調(diào)用鏈上的方法操作NFT區(qū)塊鏈MetaMask詳解
這篇文章主要為大家介紹了web3.js調(diào)用鏈上的方法操作NFT區(qū)塊鏈MetaMask詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08JavaScript實(shí)現(xiàn)瀏覽器網(wǎng)頁自動(dòng)滾動(dòng)并點(diǎn)擊的示例代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)瀏覽器網(wǎng)頁的自動(dòng)滾動(dòng)并點(diǎn)擊的示例代碼,幫助大家更好的理解和學(xué)習(xí)JavaScript的使用,感興趣的朋友可以了解下2020-12-12一文總結(jié)JavaScript中Promise遇到的問題
Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函數(shù)和事件——更合理和更強(qiáng)大。本文將總結(jié)一下在Promise中容易遇到的一些問題,感興趣的同學(xué)可以參考下2023-05-05