小程序模板网

小程序如何获取群聊的openGid以及名称

发布时间:2018-08-01 09:42 所属栏目:小程序开发教程

背景:由于公司可能需要在微信群里面使用打卡功能,因此做了个技术调研。

方案:微信在更新分享接口后,原有的在onShareAppMessage中直接拿shareTicket已不复存在。根据最新文档显示,需要在App.onLaunch()跟App.onShow()中获取。

Demo核心代码:

index.js

 

				
  1. Page({
  2.  
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. openGid: ''
  8. },
  9.  
  10. /**
  11. * 生命周期函数--监听页面加载
  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'>点击加载群名称</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. // 展示本地存储能力
  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. })

注意事项

1:必须调用这个接口wx.showShareMenu({withShareTicket: true}),否则在App.onLaunch()跟App.onShow()时,你拿不到shareTicket.



易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/24673.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询