在我調試的時候目前預定這兩種情況
第一種情況 session_key 過期了,這個值是變化的,你去拿已經存起來的 session_key 獲取手機號碼,會報41003錯錯誤
解決方案:讓前端傳小程序code碼。後臺服務端重新請求小程序官方接口,拿最新的session_key。
獲取session_key文檔地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
具體請看下面的我的實踐代碼
第二種情況,前端更換小程序的appid導致的
原因:一開始我們用的自己公司的appid在小程序的開發者工具配置,上線的時候換成了客戶的appid,這種情況去前端和服務端數據解密請求解密時一直返回41003。
解決方案:清空小程序開發者工具全部緩存,重新編輯運行就可以解決
有問題歡迎進裙 721200119 交流
樓主php很樂意爲你解答開發者遇到的任何問題
下面貼出第一種解決方案我用的代碼:
public function decryptdata()
{
if($this->request->isPost()){
$user = $this->auth->getUser();
$encryptedData = $this->request->post('encryptedData');
$iv = $this->request->post('iv');
$config = get_addon_config('cms');
$input = input();
if(empty($input['code'])){
$this->error('參數必傳');
}
if(empty($encryptedData) || empty($iv) || empty($config['wxappid'] || empty($sessionKey))){
$this->error('缺少參數');
}
$param['appid'] = $config['wxappid']; //小程序id
$param['secret'] = $config['wxappsecret']; //小程序密鑰
$param['js_code'] = define_str_replace($input['code']);
$param['grant_type'] = 'authorization_code';
$http_key = httpCurl('https://api.weixin.qq.com/sns/jscode2session', $param, 'GET');
$session_key = json_decode($http_key,true);
if (!empty($session_key['session_key'])) {
$pc = new WXBizDataCrypt($config['wxappid'], $session_key['session_key']);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode == 0) {
$data = json_decode($data,true);
if(!empty($data['phoneNumber'])){
$user->mobile = $data['phoneNumber'];
$user->save();
}
$this->success('請求成功',$data['phoneNumber']);
} else {
$this->error('獲取失敗'.$errCode);
}
} else{
$this->error('獲取失敗'.$http_key);
}
}
}
//用到的類文件
class WXBizDataCrypt
{
private $appid;
private $sessionKey;
/**
* 構造函數
* @param $sessionKey string 用戶在小程序登錄後獲取的會話密鑰
* @param $appid string 小程序的appid
*/
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
}
/**
* 檢驗數據的真實性,並且獲取解密後的明文.
* @param $encryptedData string 加密的用戶數據
* @param $iv string 與用戶數據一同返回的初始向量
* @param $data string 解密後的原文
*
* @return int 成功0,失敗返回對應的錯誤碼
*/
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->sessionKey) != 24) {
return ErrorCode::$IllegalAesKey;
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return 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 ErrorCode::$IllegalBuffer;
}
if( $dataObj->watermark->appid != $this->appid )
{
return ErrorCode::$IllegalBuffer;
}
$data = $result;
return ErrorCode::$OK;
}
}
class ErrorCode
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
}
//用到的兩個函數
function define_str_replace($data)
{
return str_replace(' ','+',$data);
}
function httpCurl($url, $params, $method = 'POST', $header = array(), $multi = false){
date_default_timezone_set('PRC');
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_COOKIE =>session_name().'='.session_id(),
);
/* 根據請求類型設置特定參數 */
switch(strtoupper($method)){
case 'GET':
// $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
// 鏈接後拼接參數 & 非?
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
//判斷是否傳輸文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的請求方式!');
}
/* 初始化並執行curl請求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception('請求發生錯誤:' . $error);
return $data;
}