微信小程序获取open-gid和群名称

来源:互联网 发布:高斯金字塔算法 matlab 编辑:程序博客网 时间:2024/06/07 01:15

小程序开放了微信群能力,获取groupid是第一步,相关组件在组件的开放数据中,获取open-gid(微信群的groupid)就会自动获取群名称。

  1. <open-data type="groupName" open-gid="xxxxxx"></open-data>

open-gid通过wx.getShareInfo带参数shareTicket(页面内分享可获取)的callback得到加密数据encryptedData,经解密后即可获得。encryptedData解密需要appid,sessionKey,iv 。sessionKey需要通过

https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

其中的JSCODE需要调用wx.login获取。解密需要在服务器端进行。


App.js代码如下:定义全局变量sessionKey

  1. App({

  2.  globalData: {

  3.    sessionKey: null,

  4.  }

  5. ,

  6.  onLaunch: function (ops) {

  7.    console.log(ops)

  8.    var that=this

  9.    wx.login({

  10.      success: function (res) {

  11.          console.log(res) // 使用这个 code 向微信换取 session_key

  12.        var js_code=res.code

  13.        wx.request({

  14.          url: 'http://www.st122.cn/xcx/php/getsec.php', //仅为示例,并非真实的接口地址

  15.          data: {

  16.            code: js_code,

  17.          },

  18.          success: function (res) {

  19.            console.log(res.data)

  20.            that.globalData.sessionKey = res.data

  21.          }

  22.        })

  23.      }

  24.    })

  25.  }

  26. })


hello.js文件如下,注意解密后的数据类型为str需转为obj。


  1. var app = getApp()

  2. Page({  

  3.  onShareAppMessage: function (res) {

  4.    var that=this    

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

  6.      // 来自页面内转发按钮

  7.      console.log(res)

  8.      console.log(app.globalData.sessionKey)

  9.    }

  10.    wx.showShareMenu({

  11.      withShareTicket: true

  12.    })

  13.    return {

  14.      title: '获取本群名称',

  15.      path: '/pages/hello/hello',

  16.      success: function (res) {

  17.        // 转发成功

  18.        console.log()

  19.        var ticket = res.shareTickets[0]

  20.        wx.getShareInfo({

  21.          shareTicket: ticket,

  22.           success: function (res) {            

  23.            console.log(res)

  24.            var encrypt = res.encryptedData

  25.            var iv=res.iv          

  26.            wx.request({

  27.              url: 'http://www.st122.cn/xcx/php/demo.php', //仅为示例,并非真实的接口地址

  28.              data: {

  29.                encrypt: encrypt,

  30.                iv:iv,

  31.                sessionKey: app.globalData.sessionKey

  32.              },

  33.              success: function (res) {

  34.                console.log(res)

  35.                console.log(res.data)

  36.                var x = res.data;

  37.                console.log(typeof(x))

  38.                var y = eval('(' + x + ')');

  39.                console.log(typeof (y))                

  40.                var openGId = y.openGId

  41.                console.log(y.openGId)              

  42.                that.setData({

  43.                  id: openGId,

  44.                  gid: openGId,              

  45.                })

  46.              },

  47.            })

  48.          },

  49.        })

  50.      },

  51.    }

  52.  }

  53. })

服务器端getsec.php

  1. $appid = '';

  2. $secret='';

  3. $js_code=$_GET["code"];

  4. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$js_code}&grant_type=authorization_code";

  5. $ch = curl_init();

  6. curl_setopt($ch, CURLOPT_URL,$url);

  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  8. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//无需https校验

  9. $a = curl_exec($ch);

  10. $strjson=json_decode($a);

  11. $sessionKey = $strjson->session_key;

  12. echo $sessionKey;

demo.php

  1. include_once "wxBizDataCrypt.php";

  2. $appid = '';

  3. $secret='';

  4. $sessionKey = $_GET["sessionKey"];

  5. $encryptedData=$_GET["encrypt"];

  6. $iv = $_GET["iv"];

  7. $pc = new WXBizDataCrypt($appid, $sessionKey);

  8. $errCode = $pc->decryptData($encryptedData, $iv, $data );

  9. if ($errCode == 0) {

  10.    print($data);

  11. } else {

  12.    print($errCode . "\n");

  13. }



原创粉丝点击