moonsflyer 6 months ago
parent
commit
c8aea32cf4
1 changed files with 31 additions and 27 deletions
  1. 31 27
      app/common/service/GiftCardQrCodeService.php

+ 31 - 27
app/common/service/GiftCardQrCodeService.php

@@ -31,34 +31,38 @@ class GiftCardQrCodeService
 
             $app = Factory::miniProgram($config);
 
-            // 修正:使用正确的API方法
-//            $response = $app->app_code->getUnlimited($scene, [
-//                'page' => $page,
-//                'width' => 280,
-//                'auto_color' => false,
-//                'line_color' => ['r' => 0, 'g' => 0, 'b' => 0],
-//            ]);
-
-            // 如果上面的方法不工作,尝试以下替代方案:
-            // 方案1:使用get方法
-            // $response = $app->app_code->get($page . '?' . $scene, [
-            //     'width' => 280,
-            // ]);
-
-
-            // 方案2:使用getQrCode方法
-//             $response = $app->app_code->getQrCode($page . '?' . $scene, 280
-//             );
-            // 备用方案:直接调用微信API
-            $accessToken = $app->access_token->getToken()['access_token'];
-            $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken}";
-
-            $data = [
-                'path' => $page . '?' . $scene,
-                'width' => 280
-            ];
+            try {
+                $accessToken = $app->access_token->getToken()['access_token'];
+                $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken}";
+
+                $data = [
+                    'path' => $page . '?' . $scene,
+                    'width' => 280
+                ];
+
+                // 使用Guzzle HTTP客户端
+                $client = new \GuzzleHttp\Client();
+                $response = $client->post($url, [
+                    'json' => $data,
+                    'headers' => [
+                        'Content-Type' => 'application/json'
+                    ]
+                ]);
+
+                // 检查响应
+                if ($response->getStatusCode() === 200) {
+                    $body = $response->getBody();
+                    // 这里$body就是二维码的二进制数据
+                } else {
+                    Log::error('微信API调用失败', ['status' => $response->getStatusCode()]);
+                    return false;
+                }
+
+            } catch (\Exception $e2) {
+                Log::error('HTTP客户端调用失败', ['error' => $e2->getMessage()]);
+                return false;
+            }
 
-            $response = $app->getClient()->postJson($url, $data);
             if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
                 Log::error('生成小程序码失败', [
                     'response' => $response,