|
|
@@ -4,9 +4,11 @@ namespace app\common\service;
|
|
|
|
|
|
use app\common\service\storage\Driver;
|
|
|
use app\common\service\ConfigService;
|
|
|
+use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
|
|
+use think\facade\Log;
|
|
|
use app\common\service\WeChatConfigService;
|
|
|
use EasyWeChat\Factory;
|
|
|
-use think\facade\Log;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 礼品卡二维码生成服务
|
|
|
@@ -22,110 +24,53 @@ class GiftCardQrCodeService
|
|
|
public static function generateAndUploadQrCode($cardNo, $cardPass)
|
|
|
{
|
|
|
try {
|
|
|
- // 小程序页面路径(使用首页,通过场景值处理)
|
|
|
- $page = 'pages/card-exchange/card-exchange';
|
|
|
-
|
|
|
- // 优化场景值格式,确保不超过32字符限制
|
|
|
- $scene = $cardPass;
|
|
|
-
|
|
|
- // 如果场景值超过32字符,使用base64编码压缩
|
|
|
- if (strlen($scene) > 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}";
|
|
|
- $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
|
|
|
- $data = [
|
|
|
- 'path' => $page . '?scene=' . $cardPass,
|
|
|
- 'width' => 430, // 方形尺寸
|
|
|
- 'auto_color' => false, // 不自动配色
|
|
|
- 'line_color' => ['r' => 0, 'g' => 0, 'b' => 0], // 黑色线条
|
|
|
- 'is_hyaline' => false, // 透明背景,去除logo区域
|
|
|
- 'env_version' => 'release' // 添加版本参数
|
|
|
- ];
|
|
|
- // $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;
|
|
|
- }
|
|
|
-
|
|
|
- // 生成本地临时文件名
|
|
|
+ // Generate regular QR code with cardPass as content
|
|
|
+ $qrContent = $cardPass;
|
|
|
+
|
|
|
+ // Generate local temp file path
|
|
|
$tempDir = runtime_path() . 'temp' . DIRECTORY_SEPARATOR;
|
|
|
if (!is_dir($tempDir)) {
|
|
|
mkdir($tempDir, 0755, true);
|
|
|
}
|
|
|
|
|
|
- $tempFileName = 'qrcode_' . $cardNo . '_' . time() . '.jpg';
|
|
|
+ $tempFileName = 'qrcode_' . $cardNo . '_' . time() . '.png';
|
|
|
$tempFilePath = $tempDir . $tempFileName;
|
|
|
|
|
|
- // 将图片Buffer保存到本地临时文件
|
|
|
- if (file_put_contents($tempFilePath, $response) === false) {
|
|
|
- Log::error('保存临时文件失败', ['path' => $tempFilePath]);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Log::info('临时文件保存成功', ['path' => $tempFilePath, 'size' => filesize($tempFilePath)]);
|
|
|
+ // Generate 430x430 square QR code
|
|
|
+ QrCode::format('png')
|
|
|
+ ->size(430)
|
|
|
+ ->margin(2)
|
|
|
+ ->generate($qrContent, $tempFilePath);
|
|
|
+
|
|
|
+ Log::info('Regular QR code generated successfully', [
|
|
|
+ 'cardNo' => $cardNo,
|
|
|
+ 'scene' => $cardPass,
|
|
|
+ 'tempPath' => $tempFilePath,
|
|
|
+ 'size' => filesize($tempFilePath)
|
|
|
+ ]);
|
|
|
|
|
|
- // 生成七牛云存储路径
|
|
|
+ // Generate Qiniu cloud storage path
|
|
|
$remotePath = 'gift_card_qrcode/' . date('Ymd') . '/' . $tempFileName;
|
|
|
|
|
|
- // 上传到七牛云
|
|
|
+ // Upload to Qiniu cloud
|
|
|
if (self::uploadToQiniu($tempFilePath, $remotePath)) {
|
|
|
- // 删除临时文件
|
|
|
+ // Delete temp file
|
|
|
unlink($tempFilePath);
|
|
|
|
|
|
- // 返回七牛云文件路径
|
|
|
+ // Return Qiniu cloud file path
|
|
|
$config = ConfigService::get('storage', 'qiniu', []);
|
|
|
$domain = rtrim($config['domain'] ?? '', '/');
|
|
|
$fullUrl = $domain . '/' . $remotePath;
|
|
|
|
|
|
- Log::info('二维码生成并上传成功', [
|
|
|
+ Log::info('Regular QR code generated and uploaded successfully', [
|
|
|
'cardNo' => $cardNo,
|
|
|
'remotePath' => $remotePath,
|
|
|
'fullUrl' => $fullUrl
|
|
|
]);
|
|
|
- outFileLog($fullUrl, 'qr_code', '$fullUrl');
|
|
|
+
|
|
|
return $fullUrl;
|
|
|
} else {
|
|
|
- // 上传失败,删除临时文件
|
|
|
+ // Upload failed, delete temp file
|
|
|
if (file_exists($tempFilePath)) {
|
|
|
unlink($tempFilePath);
|
|
|
}
|
|
|
@@ -133,7 +78,7 @@ class GiftCardQrCodeService
|
|
|
}
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('生成礼品卡二维码失败', [
|
|
|
+ Log::error('Failed to generate regular QR code', [
|
|
|
'error' => $e->getMessage(),
|
|
|
'trace' => $e->getTraceAsString()
|
|
|
]);
|