微信小程序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]);
 	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值