ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
``` //生成二维码     public function poster(){         $path = $_REQUEST['path'];    //跳转路径 $pid = intval($_REQUEST['pid']);//参数 $scene = $pid;         $this->getAccessToken();         $result = $this->getXcxCode($scene,$path); if(!$result){ echo json_encode(array('status'=>0,'err'=>'生成失败!')); exit(); } $info = array(          'code'=>__DATAURL__.'UploadFiles/code/'.$result ); echo json_encode(array('status'=>1,'info'=>$info)); exit(); }     //获取token缓存起来     public function getAccessToken(){         $appId = C('weixin.appid');//小程序appid         $secret = C('weixin.secret');//小程序secret         //创建请求数据         $url_token="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$secret}";         $data_result = $this->curl_post_https($url_token);         $data = json_decode($data_result,TRUE);         $token = $data['access_token'];         session('expires_in',time() + $data['expires_in']);         session('token',$token);     }     //拿到token获取二维码     public function getXcxCode($scene,$path){         $token = session('token');         if ($token) { $expires_in = session('expires_in'); if($expires_in && $expires_in > time()){     $accessToken= $token; }else{     $accessToken = $this->getAccessToken(); }         }else{             $accessToken = $this->getAccessToken();         }         $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";         $data=[             'scene'=>$scene,             'page'=>$path,             'width'=>430,             'auto_color'=>false,         ];         $data=json_encode($data);         //拿到二维码         $result = $this->curl_post_https($url,$data); //var_dump($result); if(!$result || $this->is_json($result)){ return false; }         //把二维码存到服务器端         $res = $this->UploadImageQrCode($result);         return $res;     } public function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); }     // 图片上传     public function UploadImageQrCode($img){         $saveimgfile_1 = './Data/UploadFiles/code/';         $fileimgname = time()."-".rand(1000,9999).".png";         $filecachs = $saveimgfile_1.$fileimgname;         $fanhuistr = file_put_contents( $filecachs,$img);         return $fileimgname;     }     // 模拟post进行url请求     public function curl_post_https($url,$data){         $ch = curl_init();         $header = "Accept-Charset: utf-8";         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);         if (curl_errno($ch)) {             return false;         }else{             return $tmpInfo;         }     } ``` 要注意生成二维码的两个参数scene和path(新手必看、必踩的坑)     page:必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面     scene:最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()\*+,/:;=?@-.\_~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)     而且在小程序onLoad 的时候 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene ``` Page({   onLoad (query) {     // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene     const scene = decodeURIComponent(query.scene)   }}) ``` 有个麻烦的地方是,如果你传递的scene 参数是“a=1&b=1”,那么在encodeURIComponent出来后的结果也是“a=1&b=1”,获取a、b对应的值就需要自己对字符串去做处理了。