微信小程序 登录 获取unionID

首先先明确一点,用户的openID在微信体系的各个应用内是不同的,比如小程序和公众号里用户的openID是不一样的,所以openID不能用来确定用户的唯一性。那这时就会用到unionID。因为同一个微信开放平台下,相同主体的App、公众号、小程序的unionid是相同的,所以可以用unionID来锁定是不是同一个用户。

其次来说一下如何获取unionID,获取unionID需要解密encryptedData。需要调用wx.login来获取code,用code去换取session_key,以及调用wx.getUserInfo获取encryptedData和iv。

最后来贴一下代码:

我是将code 、encryptedData和iv一起发送到后端

.js

  wx.login({
    success: res => {
      console.log(res)
      wx.getUserInfo({
         success: function(result) {
            console.log(result)
            if (res.code) {
               this.login(result.encryptedData, result.iv,res.code)
            }
         }
      })
    }
})
//登录
  login: function (encryptedData, iv, code) {
    var that = this;
    var url = that.globalData.url;
    //解密用户信息
    wx.request({
      url: url + '/api.php',
      method: 'GET',
      data: {
        exec: 'get_unionid',
        chid: that.globalData.chid,
        skey: that.globalData.skey,
        sign: that.globalData.sign,
        encryptedData: encryptedData,
        iv: iv,
        code: code
      },
      header: {
        'Accept': 'application/json',
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        //从数据库获取用户信息
        console.log(res);
        if (typeof res.data != 'string') {
          return false
        }
        var msg = JSON.parse(res.data);
        that.globalData.openId = msg.openId,
         //从数据库获取用户信息
          wx.request({
            url: url + '/api2user.php',
            method: 'POST',
            data: {
              exec: 'login',
              chid: that.globalData.chid,
              skey: that.globalData.skey,
              sign: that.globalData.sign,
              ukey: msg.openId,
              unionId: msg.unionId,
            },
            header: {
              'Accept': 'application/json',
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log(res.data);
              if (!res.data.status) {
                var list = res.data.list;
                if (list) {
                  that.globalData.uniq = res.data.uniq,
                  that.globalData.userInfo = res.data.list[0]
                  console.log(that.globalData.userInfo)
                  console.log("小程序登录用户信息成功!");
                }
              }
            }
          })

      }
    })
  },

后端我使用的是PHP(请大家自行理解,摘取主要内容。)

class get_unionid {
    private $action;
    private $result;
    private $request;
    private $sessionKey;
    private $encryptedData ;
    private $iv ;
    private $appid = '你自己的APPID';
    private $url = 'https://api.weixin.qq.com/sns/jscode2session';
    private $secret = '你自己的secret';
    private $js_code ;


    public function __construct($basic = '') {

    }

    /**
     *
     * 接收参数
     *
     */
    public function put($input = '') {
        $this->request = $input;
        $this->encryptedData = $input['encryptedData'];
        $this->iv = $input['iv'];
        $this->js_code = $input['code'];
    }
    //主方法
    public function logic($logic = '') {
        //获取openID、session_key
        $info = $this->get_info();
        $info = json_decode($info,true);
        if ($info) {
            $this->sessionKey = $info['session_key'];
            $errCode = $this->decryptData($this->encryptedData, $this->iv);
            $this->result = $errCode;
        }
    }

    /**
     * 检验数据的真实性,并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0,失败返回对应的错误码
     */
    private function decryptData( $encryptedData, $iv )
    {
        if (strlen($this->sessionKey) != 24) {
            return $this->ErrorCode('IllegalAesKey');
        }
        $aesKey=base64_decode($this->sessionKey);
        if (strlen($iv) != 24) {
            return $this->ErrorCode('IllegalIv');
        }
        $aesIV=base64_decode($iv);
        $aesCipher=base64_decode($encryptedData);
        $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj=json_decode( $result );

        if( $dataObj  == NULL )
        {
            return $this->ErrorCode('IllegalBuffer');
        }
        if( $dataObj->watermark->appid != $this->appid )
        {
            return $this->ErrorCode('IllegalBuffer');
        }
        return $result;
    }
    /**
     * error code 说明.
     *
     *    -41001: encodingAesKey 非法
     *    -41003: aes 解密失败
     *    -41004: 解密后得到的buffer非法
     *    -41005: base64加密失败
     *    -41016: base64解密失败
     *
     */
    private function ErrorCode($msg)
    {
        switch($msg) {
            case 'OK':
                return 0;
                break;
            case 'IllegalAesKey':
                return -41001;
                break;
            case 'IllegalIv':
                return -41002;
                break;
            case 'IllegalBuffer':
                return -41003;
                break;
            case 'DecodeBase64Error':
                return -41004;
                break;

        }
    }
    //获取openID、session_key
    private function get_info(){
        $url = $this->url . '?appid='. $this->appid .'&secret='.$this->secret . '&js_code=' . $this->js_code .'&grant_type=authorization_code';

        $ch = curl_init();
        //设置选项,包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }

  //输出
    public function out($out = '') {
        // TODO Auto-generated method stub
        return $this->result;
    }

}

你可能感兴趣的:(微信小程序)