作者 郭文星

123

@@ -8,7 +8,6 @@ @@ -8,7 +8,6 @@
8 8
9 namespace app\api\controller\v1; 9 namespace app\api\controller\v1;
10 10
11 -use app\admin\controller\Car;  
12 use app\api\controller\inspection\Task; 11 use app\api\controller\inspection\Task;
13 use lib\WXBizDataCrypt; 12 use lib\WXBizDataCrypt;
14 use think\Db; 13 use think\Db;
@@ -107,8 +106,7 @@ class Login extends Base @@ -107,8 +106,7 @@ class Login extends Base
107 &end_address=".$route['end_address']." 106 &end_address=".$route['end_address']."
108 &price=".$route['price']; 107 &price=".$route['price'];
109 $url_data=urlencode($urldata); 108 $url_data=urlencode($urldata);
110 - $carc=new Car();  
111 - $place_image=$carc->getQRCode($url_data); 109 + $place_image=$this->getQRCode($url_data);
112 //encodeURIComponent 110 //encodeURIComponent
113 $result = Db::name('driver')->where('id','=',$driver["id"])->update(['place_image'=>$place_image['fileurl']]); 111 $result = Db::name('driver')->where('id','=',$driver["id"])->update(['place_image'=>$place_image['fileurl']]);
114 } 112 }
@@ -121,8 +119,68 @@ class Login extends Base @@ -121,8 +119,68 @@ class Login extends Base
121 } 119 }
122 120
123 121
  122 + public function getQRCode($url_data){
  123 + $accessToken = $this->getAccessToken();
  124 + if ($accessToken) {
  125 + $qrCodeData = $this->createMiniProgramQRCode($accessToken, $url_data);
  126 + return $qrCodeData;
  127 + }
  128 + }
  129 + function getAccessToken() {
  130 + $appId = "wx58ceff4e93cfc523";
  131 + $appSecret = "baf744d21875280a5e98611f66adaf91";
  132 + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
  133 + $result = json_decode(file_get_contents($url), true);
  134 + return $result["access_token"] ?? null;
  135 + }
  136 + function createMiniProgramQRCode($accessToken, $path, $width = 430) {
  137 + $path = 'pages/module/intercityOrder?url='.$path; // 小程序内的页面路径
  138 + $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
  139 + $data = json_encode([
  140 + 'path' => $path,
  141 + 'width' => $width
  142 + ]);
  143 + $options = [
  144 + 'http' => [
  145 + 'method' => 'POST',
  146 + 'header' => 'Content-type:application/json',
  147 + 'content' => $data,
  148 + ],
  149 + ];
  150 + //POST参数
  151 + $result = $this->httpRequest($url,$data,"POST");
  152 + $code_name=rand().time() . '.png';
  153 + $filename= './uploads/store_qrcode/' . $code_name;
  154 + $ret = file_put_contents($filename, $result, true);
  155 + $task=new Task();
  156 + $res=$task->fileUpload($code_name);
  157 + return $res;
  158 + }
  159 + //把请求发送到微信服务器换取二维码
  160 + public function httpRequest($url,$data='',$method='GET'){
  161 + $curl = curl_init();
  162 + curl_setopt($curl, CURLOPT_URL,$url);
  163 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  164 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  165 + curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
  166 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  167 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  168 + if($method=='POST')
  169 + {
  170 + curl_setopt($curl, CURLOPT_POST, 1);
  171 + if ($data !='')
  172 + {
  173 + curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
  174 + }
  175 + }
124 176
125 - 177 + curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  178 + curl_setopt($curl, CURLOPT_HEADER, 0);
  179 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  180 + $result = curl_exec($curl);
  181 + curl_close($curl);
  182 + return $result;
  183 + }
126 //获取accesstoken 184 //获取accesstoken
127 function getwxAccessToken($appid,$AppSecret){ 185 function getwxAccessToken($appid,$AppSecret){
128 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$AppSecret.""; 186 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$AppSecret."";