//获取小程序qrcodes
public function qrcodes(Request $request)
{
$goods_id = $request->input('goods_id');//商品ID
$invite_id = $request->input('invite_id');//邀请人ID
if(!$goods_id){
return response()->json($this->errors('缺少参数'));
}
$AccessToken = Cache::get('access_token');//获取access_token
if(!$AccessToken){
$AccessToken = $this->get_access_token();
if(!$AccessToken){
return response()->json($this->errors());
}
}
$data['page'] = 'pages/eventProductDetails/eventProductDetails';//小程序目标页面
$data['scene'] = "goods_id=".$goods_id."&type=0";//页面需要携带的参数
if($invite_id){
$data['scene'] = $data['scene']."&parent_id=".$invite_id;
}
$data['width'] = '280';
$data = json_encode($data);
$res = $this->curl($this->QRCodeUrl."?access_token=$AccessToken",$data,1,1);//post方式发送请求
//返回结果是二进制内容。转成base64格式
if(is_null(json_decode($res))){
$imgData = $this->data_uri($res,'image/png');
return response()->json($this->success($imgData));
}else{
//没有接收到数据流
return response()->json($this->errors());
}
}
//请求
public static function curl($url, $params = false, $ispost = 0, $https = 0){
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
//获取access_token
public function get_access_token()
{
$access_token = $this->getSslPage($this->accesstokenUrl);//get
if($access_token){
$access_token = json_decode($access_token)->access_token;
//存入缓存
Cache::store('file')->put('access_token', $access_token,119);
return $access_token;
} else {
return false;
}
}
//GET请求
public function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//二进制转图片image/png
public function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
|