一、前言
1、官方开发文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
2、项目要上线才可以生成哦
二、生成二维码方法(Tp5.0版)
//生成企业二维码 public function createCompanyEwm(){ $post =input(); if(!isset($post['id'])||empty($post['token'])){ $param = array( 'code' => 0, 'msg' =>'参数错误!', ); return json($param); } $peizhi = []; //获取配置 $peizhi['appid']=Db::name('config')->where(['name'=>'appid'])->value('value'); $peizhi['appsecret']=Db::name('config')->where(['name'=>'appsercet'])->value('value'); $peizhi['qn_domain']=Db::name('config')->where(['name'=>'qn_domain'])->value('value'); $token_content=file_get_contents("common_token.txt");//用于存储access_token if($token_content==''){//获取新的access_token $token_result=$this ->get_new_token($peizhi); $res= $token_result['res']; $new = $token_result['new']; //判断失败 if(!$new['access_token']){ $param = array( 'code' => 0, 'msg' =>'获取token失败!', 'new'=>$new ); return json($param); } $access_token=$new['access_token']; //更新token缓存 $maxTime=time()+intval($new['expires_in']); $access_arr=str_replace('}', ",\"maxTime\":".$maxTime."}", $res); file_put_contents("common_token.txt",$access_arr); }else{ $token_arr=json_decode($token_content,true); if(intval($token_arr['maxTime'])<time()){ $token_result=$this ->get_new_token($peizhi); $res= $token_result['res']; $new = $token_result['new']; if(!$new['access_token']){ $param = array( 'code' => 0, 'msg' =>'获取token失败!', 'errcode'=>$new['errcode'] ); return json($param); } $access_token=$new['access_token']; $maxTime=time()+intval($new['expires_in']); $access_arr=str_replace('}', ",\"maxTime\":".$maxTime."}", $res); file_put_contents("common_token.txt",$access_arr); }else{ $access_token=$token_arr['access_token']; } } if($access_token==''||strlen($access_token)<10){ $param = array( 'code' => 0, 'msg' =>'获取token失败!', ); return json($param); } $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token; $data=array( 'scene'=>intval($post['id']).','.trim($post['token']), 'page'=>'pages/details/details', 'width'=>430, 'auto_color'=>true, 'is_hyaline'=>false ); //header("Content-type: image/jpg"); $ewm_res= $this->api_notice_increment($url,json_encode($data)); $dejson_ewm_res=json_decode($ewm_res,1); if($dejson_ewm_res['errcode']==41030){ $param = array( 'code' => 0, 'msg' =>'所传page页面不存在,或者小程序没有发布', 'dejson_ewm_res'=>$dejson_ewm_res ); return json($param); } if($dejson_ewm_res['errcode']==45009){ $param = array( 'code' => 0, 'msg' =>'调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。', 'dejson_ewm_res'=>$dejson_ewm_res ); return json($param); } if($dejson_ewm_res['errcode']==40001){//token无效 $token_result=$this ->get_new_token($peizhi); $res= $token_result['res']; $new = $token_result['new']; $access_token=$new['access_token']; $maxTime=time()+intval($new['expires_in']); $access_arr=str_replace('}', ",\"maxTime\":".$maxTime."}", $res); file_put_contents("common_token.txt",$access_arr); $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token; //header("Content-type: image/jpg"); $ewm_res= $this->api_notice_increment($url,json_encode($data)); } $img=$this->change_picture($ewm_res); $base64_img=$this->base64EncodeImage($img); $this->delDirAndFile('./public/uploads/temporary/'); error_reporting(0); //上传到七牛 $Qiniu=new Qiniu(); $data=json_decode($Qiniu->liuUpload($base64_img),true); $data['url']='http://'.$peizhi['qn_domain'].'/'.$data['hash']; $file_exists=file_exists_get($data['url']); if(!$file_exists){ return json(['code'=>0,'msg'=>'生成失败!']); } //数据存入数据库 $companyObj = new CompanyModel(); $flag = $companyObj->editCompany(['id'=>$post['id'],'ewm'=>$data['url'],'addtime'=>time()]); $param = array( 'code' => 1, 'msg' =>'获取成功', 'data'=>$data, ); return json($param); } //获取新的token function get_new_token($peizhi){ $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$peizhi['appid']."&secret=".$peizhi['appsecret']; $res= httpRequest($url); $new = json_decode($res,1); $result=array('res'=>$res,'new'=>$new); return $result; } //网络requst function httpRequest($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //执行 $output = curl_exec($curl); curl_close($curl); return $output; } /** * 网络请求-用于获取二维码函数 * @param [type] $url [description] * @param [type] $data [description] * @return [type] [description] */ function api_notice_increment($url, $data){ $ch = curl_init(); $header = ["Content-type: image/jpg"]; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); curl_close($ch); return $tmpInfo; } //二进制二维码图片buffer,保存为临时图片 public function change_picture($imgs){ $path="./public/uploads/temporary/"; if(!is_dir($path)){ mkdir($path); } $new_file = "./public/uploads/temporary/" .'ewm'.date('Y-m-d',time()).getRandomString(8).".jpg"; //生成图片的名字 if(!empty($imgs)){ $file = fopen($new_file,"w");//打开文件准备写入 fwrite($file,$imgs);//写入 fclose($file);//关闭 } return $new_file; }
3、小程序获取参数
Page({ onLoad(query) { // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene const scene = decodeURIComponent(query.scene) } })