作者 郭文星

123

@@ -203,4 +203,51 @@ class Car extends Backend @@ -203,4 +203,51 @@ class Car extends Backend
203 $this->success(); 203 $this->success();
204 } 204 }
205 205
206 -} 206 + function getAccessToken() {
  207 + $appId = "wx58ceff4e93cfc523";
  208 + $appSecret = "baf744d21875280a5e98611f66adaf91";
  209 + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
  210 + $result = json_decode(file_get_contents($url), true);
  211 + return $result["access_token"] ?? null;
  212 + }
  213 +
  214 + function createMiniProgramQRCode($accessToken, $path, $width = 430) {
  215 + $url = "https://api.weixin.qq.com/shop/funds/qrcode/gen?access_token={$accessToken}";
  216 +
  217 + $data = json_encode([
  218 + 'path' => $path,
  219 + 'width' => $width
  220 + ]);
  221 + $options = [
  222 + 'http' => [
  223 + 'method' => 'POST',
  224 + 'header' => 'Content-type:application/json',
  225 + 'content' => $data,
  226 + ],
  227 + ];
  228 + $context = stream_context_create($options);
  229 + $result = file_get_contents($url, false, $context);
  230 + print_r($result);return;
  231 +
  232 + if ($result === false) {
  233 + return false;
  234 + }
  235 + return $result;
  236 + }
  237 + public function test(){
  238 + $accessToken = $this->getAccessToken();
  239 + if ($accessToken) {
  240 + $path = 'pages/index/index'; // 小程序内的页面路径
  241 + $qrCodeData = $this->createMiniProgramQRCode($accessToken, $path);
  242 +
  243 + if ($qrCodeData) {
  244 + header('Content-Type: image/png');
  245 + echo $qrCodeData;
  246 + } else {
  247 + echo "Failed to generate QR code.";
  248 + }
  249 + } else {
  250 + echo "Failed to get access token.";
  251 + }
  252 + }
  253 +}