小程序模板網(wǎng)

小程序如何獲取群聊的openGid以及名稱

發(fā)布時(shí)間:2018-08-01 09:42 所屬欄目:小程序開發(fā)教程

背景:由于公司可能需要在微信群里面使用打卡功能,因此做了個(gè)技術(shù)調(diào)研。

方案:微信在更新分享接口后,原有的在onShareAppMessage中直接拿shareTicket已不復(fù)存在。根據(jù)最新文檔顯示,需要在App.onLaunch()跟App.onShow()中獲取。

Demo核心代碼:

index.js

 

				
  1. Page({
  2.  
  3. /**
  4. * 頁面的初始數(shù)據(jù)
  5. */
  6. data: {
  7. openGid: ''
  8. },
  9.  
  10. /**
  11. * 生命周期函數(shù)--監(jiān)聽頁面加載
  12. */
  13. onLoad: function (options) {
  14. let that = this
  15. wx.showShareMenu({
  16. withShareTicket: true
  17. })
  18. app.getShareTiket(function (globalData) {
  19. console.log('clickReload---globalData-->' + JSON.stringify(globalData))
  20. that.setData({
  21. openGid: globalData.openGid
  22. })
  23. })
  24. },
  25. clickReload: function () {
  26. let that = this
  27. app.getShareTiket(function (globalData) {
  28. console.log('clickReload---globalData-->' + JSON.stringify(globalData))
  29. that.setData({
  30. openGid: globalData.openGid
  31. })
  32. })
  33. }
  34. })

index.wxml

 

				
  1. <!--index.wxml-->
  2. <view wx:if="{{openGid}}" class='groupName'>
  3. 群名稱:<open-data type="groupName" open-gid="{{openGid}}"></open-data>
  4. </view>
  5.  
  6. <view wx:else>
  7. <button bindtap='clickReload'>點(diǎn)擊加載群名稱</button>
  8. </view>
  9.  
  10. <view>{{openGid ? openGid : '無'}}</view>

app.js

 

				
  1. //app.js
  2. App({
  3. globalData: {
  4. shareTicket: '',
  5. openGid: ''
  6. },
  7. onLaunch: function (options) {
  8.  
  9. },
  10. onShow: function (options) {
  11. let that = this
  12. if (options && options.scene == 1044) {
  13. that.globalData.shareTicket = options.shareTicket
  14. }
  15. console.log('onShow---options=--->' + JSON.stringify(options))
  16. },
  17. getShareTiket: function (cb) {
  18. let that = this
  19. // 展示本地存儲(chǔ)能力
  20. if (that.globalData.shareTicket) {
  21. wx.getShareInfo({
  22. shareTicket: that.globalData.shareTicket,
  23. success: function (res) {
  24. console.log('getShareTiket---shareTicket-->' + JSON.stringify(res))
  25. let js_encryptedData = res.encryptedData
  26. let js_iv = res.iv
  27. wx.login({
  28. success: function (res) {
  29. let js_code = res.code
  30. console.log('code-->' + js_code)
  31. wx.request({
  32. url: 'xxxxxxxx',
  33. method: 'POST',
  34. data: {
  35. code: js_code,
  36. appId: 'xxxxx',
  37. encryptedData: js_encryptedData,
  38. iv: js_iv
  39. },
  40. success: function (res) {
  41. that.globalData.openGid = res.data.openGId
  42. console.log('getShareTiket---openGid' + that.globalData.openGid)
  43. typeof cb == "function" && cb(that.globalData)
  44. },
  45. fail: function (err) {
  46. console.log('getShareTiket---err' + JSON.stringify(err))
  47. }
  48. })
  49. }
  50. })
  51. }
  52. })
  53. } else {
  54. console.log('不存在shareTicket')
  55. }
  56. }
  57. })

注意事項(xiàng)

1:必須調(diào)用這個(gè)接口wx.showShareMenu({withShareTicket: true}),否則在App.onLaunch()跟App.onShow()時(shí),你拿不到shareTicket.



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://22321a.com/wxmini/doc/course/24673.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢