appid = $serviceConfig['userappid']; $this->secret = $serviceConfig['usersecret']; } // 获取ACCESS_TOKEN public function getAccessToken() { $key = 'user_wx_access_token'; $token = Cache::get($key); if(!$token) { $url = $this->accessUrl . $this->appid . '&secret=' . $this->secret; $res = json_decode(\fast\Http::get($url), true); $token = $res['access_token']; Cache::set($key,$token,7000); } return $token; } // 生成小程序二维码 public function createMiniProgramQRCode($path, $width = 430,$scene='') { $accessToken = $this->getAccessToken(); $url = $this->url.$accessToken; $data = json_encode(['scene'=>$scene,'path' => $path, 'width' => $width]); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data))); $result = curl_exec($ch); if (curl_errno($ch)) { curl_close($ch); return false; } $path ='/public/uploads/qrcode/'.date("Y-m-d").'/'; if (!is_dir(ROOT_PATH.$path)) { mkdir(ROOT_PATH.$path,0777,true); } $file = "/uploads/qrcode/".date("Y-m-d").'/'.time().Random::alnum().".png"; $fileName = $_SERVER['DOCUMENT_ROOT'].$file; file_put_contents($fileName, $result); curl_close($ch); return $file; } }