前言:话说这个在微信小程序获取union_id真是一个大坑,前来记录一下,避免更多人的入坑;同时提供一段可运行的Golang语言的解密代码(官方提供的代码只有C++、NodeJS、PHP、Python – -)。
前期准备:去腾讯开放平台(open.weixin.qq.com)把相关应用绑定在一起,同时需要进行微信认证(300人民币/年)。
坑一:
小程序开发文档误导,如下图:
通过这种途径是无法获取到unionid的。(我的环境是:一个网页应用,一个小程序,已认证通过,已绑定在同一个主体下。通过调试是无法获取unionid。)
坑二:
如图,通过wx.login是无法获取到union_id的。
那么“正确”的做法是什么呢?请按照如下操作:
1、当通过wx.login获取到code后,发送到后台服务器,此时后台服务器通过请求地址“https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code”得到openid以及session_key。
2、在小程序端调用如下接口:
wx.getUserInfo({ withCredentials: true, success: function (res){ console.log(res) }})
在变量res中,会包括“encryptedData”,“iv”变量的值(当然也会包括“userInfo”,“signature”等值,那些不在本文讨论范围之内)。
3、将“encryptedData”,“iv”的值发送到后台服务器,此时后台服务器相当于得到这个用户的“session_key”,“encryptedData”,“iv”这三个值(Base64 编码)。
4、进行对称解密,得到unionId等值。参照如图:
附上Golang代码:
import ( "crypto/aes" "crypto/cipher" ) func getOriginalInformation(encryptedData []byte, sessionKey []byte, iv []byte)(string,error){ var aesBlockDecrypter cipher.Block aesBlockDecrypter, err := aes.NewCipher(sessionKey) if err != nil { return "", err } decrypted := make([]byte, len(encryptedData)) aesDecrypter := cipher.NewCBCDecrypter(aesBlockDecrypter, iv) aesDecrypter.CryptBlocks(decrypted,encryptedData) return string(decrypted), nil }
附言: 请在使用该函数之前,将对应值从Base64解出来哈;返回值为json编码的string,如果需要验证appid或者其他值,只需json解码对比即可。
<?php class OpenSSLAES { /** * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得 */ protected $method; /** * var string $secret_key 加解密的密钥 */ protected $secret_key; /** * var string $iv 加解密的向量,有些方法需要设置比如CBC */ protected $iv; /** * var string $options (不知道怎么解释,目前设置为0没什么问题) */ protected $options; /** * 构造函数 * * @param string $key 密钥 * @param string $method 加密方式 * @param string $iv iv向量 * @param mixed $options 还不是很清楚 * */ public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0) { // key是必须要设置的 $this->secret_key = isset($key) ? $key : exit('key为必须项'); $this->method = $method; $this->iv = $iv; $this->options = $options; } /** * 加密方法,对数据进行加密,返回加密后的数据 * * @param string $data 要加密的数据 * * @return string * */ public function encrypt($data) { return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } /** * 解密方法,对数据进行解密,返回解密后的数据 * * @param string $data 要解密的数据 * * @return string * */ public function decrypt($data) { return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } } $sessionKey = 'SSSSSSSSSS'; $encryptedData="JJJJJJJJJJJJ"; $iv = "AAAAAAA"; $aes = new OpenSSLAES(base64_decode($sessionKey),'AES-128-CBC',base64_decode($iv)); var_dump($aes->decrypt($encryptedData));
附言:之所以提供PHP示例代码,主要是因为用官方demo解密的时候,提示-41003错误,可能版本兼容问题?算了管它来。此段代码感谢:凭栏知潇雨(https://www.cnblogs.com/lantor/p/7522314.html)