微信小程序php後臺登錄

public function wxlogin1(){
 		$get = input('post.');
 		$param['appid'] = 'wxbaa36a7944e7a062';    //小程序id
 		$param['secret'] = '43216487422d4a4fa045aeba49976ae1';    //小程序密鑰
 		$param['js_code'] = str_replace(' ','+',$get['code']);
 		$param['grant_type'] = 'authorization_code';

 		/**
		 * 發送HTTP請求方法
		 * @param  string $url    請求URL
		 * @param  array  $params 請求參數
		 * @param  string $method 請求方法GET/POST
		 * @return array  $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;
		}
 		$http_key = httpCurl('https://api.weixin.qq.com/sns/jscode2session', $param, 'GET');
	    $session_key = json_decode($http_key,true);
	    //print_r(http_build_query($param));
	    if (!empty($session_key['session_key'])) {
    	$appid = $param['appid'];
    	$encrypteData = urldecode($get['encrypteData']);
    	$iv = str_replace(' ','+',$get['iv']);
    	function decryptData( $appid , $sessionKey, $encryptedData, $iv ){
		    $OK = 0;
		    $IllegalAesKey = -41001;
		    $IllegalIv = -41002;
		    $IllegalBuffer = -41003;
		    $DecodeBase64Error = -41004;
		 
		    if (strlen($sessionKey) != 24) {
		        return $IllegalAesKey;
		    }
		    $aesKey=base64_decode($sessionKey);
		 
		    if (strlen($iv) != 24) {
		        return $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 $IllegalBuffer;
		    }
		    if( $dataObj->watermark->appid != $appid )
		    {
		        return $DecodeBase64Error;
		    }
		    $data = json_decode($result,true);
		 
		    return $data;
		}
    	$errCode = decryptData($appid, $session_key['session_key'], $encrypteData, $iv);
    	//把appid寫入到數據庫中
    	$data['appid'] = $errCode['openId'];
    	$data['nicheng'] = $errCode['nickName'];
    	$data['publishtime'] = time();
    	$data['sex'] = $errCode['gender'];
    	$data['avatarUrl'] = $errCode['avatarUrl'];
    	// $data['platform'] = $get['platform'];
    	if (false == Db::name('message_user')->where(['appid' => $data['appid']])->find()) {
    		Db::name('message_user')->insert($data);
    		$value = Db::name('message_user')->where(['appid' => $data['appid']])->field('uId,appid,nicheng,publishtime,sex,platform,avatarUrl')->select();
    	}else{
    		$value = Db::name('message_user')->where(['appid' => $data['appid']])->field('uId,appid,nicheng,publishtime,sex,platform,avatarUrl')->select();
    	}
    	$names = Db::name('message_url')->field('name')->select();
    		return json_encode(['data'=>$data,'status'=>1,'names'=>$names]);
	    }else{
	    	$names = Db::name('message_url')->field('name')->select();
	    	return json_encode(['status'=>0,'names'=>$names]);
	    }
	   		// return json(['status' =>1]);
 	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章