1.申請注冊微信開放平台 open.weixin.qq.com
2.綁定公眾號或者小程序到微信開放平台
3.微信公眾號的話,使用微信網頁授權獲取 unionid
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
4.小程序的話,獲取unionid的幾種方式
https://developers.weixin.qq.com/miniprogram/dev/api/unionID.html
其中小程序獲取的第一種方法解密 示例參考 丹溪運動小程序的處理方式
注:unionid 是同一用戶在使用微信平台下的不同產品所產生的id,用於跨產品區分用戶 。
解密數據(appid sessionKey encryptedData iv )
/**
* 檢驗數據的真實性,並且獲取解密后的明文.
* @param $encryptedData string 加密的用戶數據
* @param $iv string 與用戶數據一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失敗返回對應的錯誤碼
*/
public function xiaoDecryptData( Request $request )
{
$data = $request->all();
$sessionKey = $data['sessionKey'];
$encryptedData = $data['encryptedData'];
$appid = $data['appid'];
$iv = $data['iv'];
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
$OK = 0;
if (strlen($sessionKey) != 24) {
return ['code' => 1002, 'data' => ['message' => $IllegalAesKey]];
}
$aesKey=base64_decode($sessionKey);
if (strlen($iv) != 24) {
return ['code' => 1002, 'data' => ['message' => $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 ['code' => 1002, 'data' => ['message' => $IllegalBuffer]];
}
if( $dataObj->watermark->appid != $appid )
{
return ['code' => 1002, 'data' => ['message' => $IllegalBuffer]];
}
return ['code' => 1000, 'data' => $dataObj];
}