首先我们先获取到token,拿到token后再去调获取二维码的接口
这里需要注意的是,返回的图片是一堆乱码,需要使用base65_encode来解密,输出成图片即可
/* * author:咔咔 * wechat:fangkangfk * time:2018.9.13 * 获取小程序二维码 * */ /** * 获取 access_tonken * @param string $token_file 用来存储token的临时文件 */ private function _getToken($token_file='./access_token') { // 考虑过期问题,将获取的access_token存储到某个文件中 $life_time = 7200; if (file_exists($token_file) && time()-filemtime($token_file)<$life_time) { // 存在有效的access_token return file_get_contents($token_file); } // 目标URL: $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".Config::get('appid')."&secret=".Config::get('appsecret'); //向该URL,发送GET请求 $result = $this->request($url); if (!$result) { return false; } // 存在返回响应结果 $result_obj = json_decode($result); // 写入 file_put_contents($token_file, $result_obj->access_token); return $result_obj->access_token; } /* 获取二维码 */ public function getQrcode(){ $userId = $this->request->param('user_id'); $member_app_id = $this->request->param('appid'); $token = $this->getToken($member_app_id); $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token; $data = [ 'path' =>"pages/network/network?userId=".$userId, 'scene'=>'1001', 'width'=>430, 'auto_color'=>false, ]; $data=json_encode($data); $res = $this->request($url,$data); $data='image/png;base64,'.base64_encode($res); $imageName = rand(1111,9999).'.png'; if (strstr($data,",")){ $image = explode(',',$data); $image = $image[1]; } $path = "/attachs/uploads/".date("Ymd",time()); if (!is_dir($path)){ //判断目录是否存在 不存在就创建 mkdir($path,0777,true); } $imageSrc= $path."/". $imageName; //图片名字 $r = file_put_contents(ROOT_PATH .$imageSrc, base64_decode($image));//返回的是字节数 if (!$r) { // return json(['data'=>null,"code"=>1,"msg"=>"图片生成失败"]); }else{ $send = ['code'=>200,'msg'=>'图片生成成功','path'=>'https://'.$_SERVER['HTTP_HOST'].$imageSrc]; // return json(['data'=>1,"code"=>0,"msg"=>"图片生成成功"]); } $this->result($send,200,'获取二维码成功','json'); } //发起请求 public static function request($url, $data=array()){ $ch = curl_init();//初始化 //curl_setopt();//设置 //设置 curl_setopt($ch,CURLOPT_URL,$url); //需要获取的 URL 地址 curl_setopt($ch,CURLOPT_HEADER,0); //启用时会将头文件的信息作为数据流输出, 此处禁止输出头信息 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //获取的信息以字符串返回,而不是直接输出 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); //连接超时时间 curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //避免https 的ssl验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if($data){ curl_setopt($ch, CURLOPT_POST, 1); //post请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post参数 } //执行 $data = curl_exec($ch);//执行 不输出 内容返回给它 //判断是否请求成功 if(curl_errno($ch)){//错误码 echo 'curl error: '.curl_error($ch);//错误信息 } $response = curl_getinfo($ch); switch($response['http_code']){ case 200: return $data; break; default: exit('程序异常'); } curl_close($ch);//关闭 } //发起请求 public static function request($url, $data=array()){ $ch = curl_init();//初始化 //curl_setopt();//设置 //设置 curl_setopt($ch,CURLOPT_URL,$url); //需要获取的 URL 地址 curl_setopt($ch,CURLOPT_HEADER,0); //启用时会将头文件的信息作为数据流输出, 此处禁止输出头信息 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //获取的信息以字符串返回,而不是直接输出 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); //连接超时时间 curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //避免https 的ssl验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if($data){ curl_setopt($ch, CURLOPT_POST, 1); //post请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post参数 } //执行 $data = curl_exec($ch);//执行 不输出 内容返回给它 //判断是否请求成功 if(curl_errno($ch)){//错误码 echo 'curl error: '.curl_error($ch);//错误信息 } $response = curl_getinfo($ch); switch($response['http_code']){ case 200: return $data; break; default: exit('程序异常'); } curl_close($ch);//关闭 }
那么问题来了怎么将获取二维码显示在小程序界面呢!
var array = wx.base64ToArrayBuffer(data); var base64 = wx.arrayBufferToBase64(array); that.setData({ kaka: "data:image/png;base64," + base64 });
上边的三段代码特别重要
saveImageToAlbum: function() { var user_id = wx.getStorageSync('user_id'); var that = this; wxb.Post('/api/automatic.manage/getQrcode',{ user_id: user_id },function(data){ console.log(data) var array = wx.base64ToArrayBuffer(data); var base64 = wx.arrayBufferToBase64(array); that.setData({ kaka: "data:image/png;base64," + base64 }); // that.setData({ // qrcode: data.data // }) }); },