Selaa lähdekoodia

修改生成二维码

moonsflyer 5 kuukautta sitten
vanhempi
commit
ca6cf39b2c
1 muutettua tiedostoa jossa 92 lisäystä ja 81 poistoa
  1. 92 81
      app/common/service/GiftCardQrCodeService.php

+ 92 - 81
app/common/service/GiftCardQrCodeService.php

@@ -23,10 +23,10 @@ class GiftCardQrCodeService
     {
         try {
             // 小程序页面路径(使用首页,通过场景值处理)
-            $page = 'pages/card-exchange/card-exchange?cardPass='.$cardPass;
+            $page = 'pages/card-exchange/card-exchange';
 
             // 优化场景值格式,确保不超过32字符限制
-            $scene = $cardNo . '_' . $cardPass;
+            $scene = $cardPass;
 
             // 如果场景值超过32字符,使用base64编码压缩
             if (strlen($scene) > 32) {
@@ -60,96 +60,65 @@ class GiftCardQrCodeService
                 'cardPass' => $cardPass,
             ]);
 
-            $ch = curl_init();
-            curl_setopt($ch, CURLOPT_URL, $url);
-            curl_setopt($ch, CURLOPT_POST, true);
-            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-            curl_setopt($ch, CURLOPT_HTTPHEADER, [
-                'Content-Type: application/json; charset=utf-8'
-            ]);
-            curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
-
-            $response = curl_exec($ch);
-            outFileLog($response,'qr_code','$response');
-            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-            curl_close($ch);
-            outFileLog($httpCode,'qr_code','$httpCode');
-            if ($httpCode === 200) {
-                // 检查是否为JSON错误响应
-                $jsonData = json_decode($response, true);
-                outFileLog($jsonData,'qr_code','$jsonData');
-                if ($jsonData && isset($jsonData['errcode'])) {
-                    Log::error('微信API错误', $jsonData);
-                    return false;
-                }
 
-                // 创建保存目录
-                $saveDir = 'resource/image/gift_card/qr_code/';
-                if (!is_dir($saveDir)) {
-                    mkdir($saveDir, 0755, true);
-                }
+            // 调用微信接口获取二维码图片Buffer
+            $response = self::httpRequest($url, json_encode($data), 'POST');
 
-                $fileName = 'gift_card_' . $cardNo . '_' . time() . '.png';
-                $localPath = $saveDir . $fileName;
+            // 检查响应是否为图片数据
+            if (empty($response)) {
+                Log::error('微信接口返回空响应');
+                return false;
+            }
 
-                // 保存二进制数据到本地
-                $result = file_put_contents($localPath, $response);
+            // 检查是否返回错误信息(JSON格式)
+            $jsonData = json_decode($response, true);
+            if (json_last_error() === JSON_ERROR_NONE && isset($jsonData['errcode'])) {
+                Log::error('微信接口返回错误', $jsonData);
+                return false;
+            }
 
-                if ($result === false) {
-                    Log::error('保存二维码文件失败', ['path' => $localPath]);
-                    return false;
-                }
+            // 生成本地临时文件名
+            $tempDir = runtime_path() . 'temp' . DIRECTORY_SEPARATOR;
+            if (!is_dir($tempDir)) {
+                mkdir($tempDir, 0755, true);
+            }
 
-                // 验证文件是否为有效的PNG图片
-                $imageInfo = getimagesize($localPath);
-                if ($imageInfo === false || $imageInfo['mime'] !== 'image/png') {
-                    Log::error('生成的文件不是有效的PNG图片', [
-                        'path' => $localPath,
-                        'imageInfo' => $imageInfo
-                    ]);
-                    if (file_exists($localPath)) {
-                        unlink($localPath);
-                    }
-                    return false;
-                }
+            $tempFileName = 'qrcode_' . $cardNo . '_' . time() . '.jpg';
+            $tempFilePath = $tempDir . $tempFileName;
 
-                Log::info('二维码文件保存成功', [
-                    'localPath' => $localPath,
-                    'fileSize' => filesize($localPath)
-                ]);
+            // 将图片Buffer保存到本地临时文件
+            if (file_put_contents($tempFilePath, $response) === false) {
+                Log::error('保存临时文件失败', ['path' => $tempFilePath]);
+                return false;
+            }
 
-                // 上传到七牛云
-                $remotePath = 'gift_card/qr_code/' . $fileName;
-                outFileLog($remotePath,'qr_code','$remotePath');
-                $uploadResult = self::uploadToQiniu($localPath, $remotePath);
-                outFileLog($uploadResult,'qr_code','$uploadResult');
-                if ($uploadResult) {
-                    Log::info('二维码上传七牛云成功', [
-                        'localPath' => $localPath,
-                        'remotePath' => $remotePath
-                    ]);
+            Log::info('临时文件保存成功', ['path' => $tempFilePath, 'size' => filesize($tempFilePath)]);
 
-                    // 删除本地临时文件
-                    if (file_exists($localPath)) {
-                        unlink($localPath);
-                    }
+            // 生成七牛云存储路径
+            $remotePath = 'gift_card_qrcode/' . date('Ymd') . '/' . $tempFileName;
 
-                    return $remotePath;
-                } else {
-                    Log::error('上传到七牛云失败', [
-                        'localPath' => $localPath,
-                        'remotePath' => $remotePath
-                    ]);
-                    // 保留本地文件作为备份
-                    return false;
-                }
+            // 上传到七牛云
+            if (self::uploadToQiniu($tempFilePath, $remotePath)) {
+                // 删除临时文件
+                unlink($tempFilePath);
 
-            } else {
-                Log::error('HTTP请求失败', [
-                    'code' => $httpCode,
-                    'response' => substr($response, 0, 500)
+                // 返回七牛云文件路径
+                $config = ConfigService::get('storage', 'qiniu', []);
+                $domain = rtrim($config['domain'] ?? '', '/');
+                $fullUrl = $domain . '/' . $remotePath;
+
+                Log::info('二维码生成并上传成功', [
+                    'cardNo' => $cardNo,
+                    'remotePath' => $remotePath,
+                    'fullUrl' => $fullUrl
                 ]);
+                outFileLog($fullUrl,'qr_code','$fullUrl');
+                return $fullUrl;
+            } else {
+                // 上传失败,删除临时文件
+                if (file_exists($tempFilePath)) {
+                    unlink($tempFilePath);
+                }
                 return false;
             }
 
@@ -162,6 +131,48 @@ class GiftCardQrCodeService
         }
     }
 
+    public static 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'] ?? 'Mozilla/5.0');
+        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_HTTPHEADER, [
+                    'Content-Type: application/json',
+                    'Content-Length: ' . strlen($data)
+                ]);
+            }
+        }
+
+        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
+        curl_setopt($curl, CURLOPT_HEADER, 0);
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
+        $result = curl_exec($curl);
+
+        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+        $error = curl_error($curl);
+        curl_close($curl);
+
+        if ($error) {
+            Log::error('CURL请求失败', ['error' => $error, 'url' => $url]);
+            return false;
+        }
+
+        if ($httpCode !== 200) {
+            Log::error('HTTP请求失败', ['code' => $httpCode, 'url' => $url]);
+            return false;
+        }
+
+        return $result;
+    }
+
     /**
      * @notes 上传文件到七牛云
      * @param string $localPath 本地文件路径