32) { $scene = $cardPass; $scene = base64_encode($cardNo . '|' . $cardPass); if (strlen($scene) > 32) { $scene = substr($scene, 0, 32); } } // 生成小程序码 $config = WeChatConfigService::getMnpConfig(); $app = Factory::miniProgram($config); $accessToken = $app->access_token->getToken()['access_token']; outFileLog($accessToken,'qr_code','$accessToken'); // $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}"; $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken}"; $data = [ 'page' => $page.'?cardPass='. '?cardPass=' . urlencode($cardPass), 'width' => 430 // 方形尺寸 ]; // $data = [ // 'scene' => $scene, // 'page' => $page, // 'width' => 430, // 推荐使用430像素,确保方形 // 'auto_color' => false, // 不自动配色 // 'line_color' => ['r' => 0, 'g' => 0, 'b' => 0], // 黑色线条 // 'is_hyaline' => true, // 透明背景,去除logo区域 // 'env_version' => 'release' // 添加版本参数 // ]; Log::info('生成小程序码参数', [ 'scene' => $scene, 'scene_length' => strlen($scene), 'page' => $page, 'cardPass' => $cardPass, ]); // 调用微信接口获取二维码图片Buffer $response = self::httpRequest($url, json_encode($data), 'POST'); // 检查响应是否为图片数据 if (empty($response)) { Log::error('微信接口返回空响应'); return false; } // 检查是否返回错误信息(JSON格式) $jsonData = json_decode($response, true); if (json_last_error() === JSON_ERROR_NONE && isset($jsonData['errcode'])) { Log::error('微信接口返回错误', $jsonData); return false; } // 生成本地临时文件名 $tempDir = runtime_path() . 'temp' . DIRECTORY_SEPARATOR; if (!is_dir($tempDir)) { mkdir($tempDir, 0755, true); } $tempFileName = 'qrcode_' . $cardNo . '_' . time() . '.jpg'; $tempFilePath = $tempDir . $tempFileName; // 将图片Buffer保存到本地临时文件 if (file_put_contents($tempFilePath, $response) === false) { Log::error('保存临时文件失败', ['path' => $tempFilePath]); return false; } Log::info('临时文件保存成功', ['path' => $tempFilePath, 'size' => filesize($tempFilePath)]); // 生成七牛云存储路径 $remotePath = 'gift_card_qrcode/' . date('Ymd') . '/' . $tempFileName; // 上传到七牛云 if (self::uploadToQiniu($tempFilePath, $remotePath)) { // 删除临时文件 unlink($tempFilePath); // 返回七牛云文件路径 $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; } } catch (\Exception $e) { Log::error('生成礼品卡二维码失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } 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 本地文件路径 * @param string $remotePath 远程文件路径 * @return bool */ private static function uploadToQiniu($localPath, $remotePath) { try { // 检查本地文件是否存在 if (!file_exists($localPath)) { Log::error('本地文件不存在', ['path' => $localPath]); return false; } $config = [ 'default' => ConfigService::get('storage', 'default', 'local'), 'engine' => ConfigService::get('storage_engine') ]; Log::info('存储配置', $config); if ($config['default'] === 'qiniu') { $storageDriver = new Driver($config); // 使用绝对路径进行上传 $absolutePath = realpath($localPath); $result = $storageDriver->fetch($absolutePath, $remotePath); if (!$result) { $error = $storageDriver->getError(); Log::error('七牛云上传失败', [ 'error' => $error, 'localPath' => $absolutePath, 'remotePath' => $remotePath ]); return false; } Log::info('七牛云上传成功', [ 'localPath' => $absolutePath, 'remotePath' => $remotePath ]); return true; } else { // 如果不是七牛云存储,返回本地路径 Log::info('使用本地存储', ['path' => $localPath]); return true; } } catch (\Exception $e) { Log::error('上传到七牛云失败', [ 'error' => $e->getMessage(), 'localPath' => $localPath, 'remotePath' => $remotePath ]); return false; } } /** * @notes 批量生成二维码 * @param array $giftCards 礼品卡数组 * @return array */ public static function batchGenerateQrCode($giftCards) { $result = [ 'success' => 0, 'failed' => 0, 'total' => count($giftCards), 'details' => [] ]; foreach ($giftCards as $card) { $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']); if ($qrCodePath) { // 更新数据库中的二维码路径 \app\common\model\GiftCardInfo::where('id', $card['id']) ->update(['qr_code_path' => $qrCodePath]); $result['success']++; $result['details'][] = [ 'card_no' => $card['card_no'], 'status' => 'success', 'qr_code_path' => $qrCodePath ]; } else { $result['failed']++; $result['details'][] = [ 'card_no' => $card['card_no'], 'status' => 'failed', 'error' => '生成或上传失败' ]; } } return $result; } }