作者 郭文星

123

... ... @@ -8,7 +8,6 @@
namespace app\api\controller\v1;
use app\admin\controller\Car;
use app\api\controller\inspection\Task;
use lib\WXBizDataCrypt;
use think\Db;
... ... @@ -107,8 +106,7 @@ class Login extends Base
&end_address=".$route['end_address']."
&price=".$route['price'];
$url_data=urlencode($urldata);
$carc=new Car();
$place_image=$carc->getQRCode($url_data);
$place_image=$this->getQRCode($url_data);
//encodeURIComponent
$result = Db::name('driver')->where('id','=',$driver["id"])->update(['place_image'=>$place_image['fileurl']]);
}
... ... @@ -121,8 +119,68 @@ class Login extends Base
}
public function getQRCode($url_data){
$accessToken = $this->getAccessToken();
if ($accessToken) {
$qrCodeData = $this->createMiniProgramQRCode($accessToken, $url_data);
return $qrCodeData;
}
}
function getAccessToken() {
$appId = "wx58ceff4e93cfc523";
$appSecret = "baf744d21875280a5e98611f66adaf91";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$result = json_decode(file_get_contents($url), true);
return $result["access_token"] ?? null;
}
function createMiniProgramQRCode($accessToken, $path, $width = 430) {
$path = 'pages/module/intercityOrder?url='.$path; // 小程序内的页面路径
$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
$data = json_encode([
'path' => $path,
'width' => $width
]);
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-type:application/json',
'content' => $data,
],
];
//POST参数
$result = $this->httpRequest($url,$data,"POST");
$code_name=rand().time() . '.png';
$filename= './uploads/store_qrcode/' . $code_name;
$ret = file_put_contents($filename, $result, true);
$task=new Task();
$res=$task->fileUpload($code_name);
return $res;
}
//把请求发送到微信服务器换取二维码
public function httpRequest($url,$data='',$method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data !='')
{
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
//获取accesstoken
function getwxAccessToken($appid,$AppSecret){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$AppSecret."";
... ...