GiftCardQrCodeService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\common\service;
  3. use app\common\service\storage\Driver;
  4. use app\common\service\ConfigService;
  5. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  6. use think\facade\Log;
  7. use app\common\service\WeChatConfigService;
  8. use EasyWeChat\Factory;
  9. /**
  10. * 礼品卡二维码生成服务
  11. */
  12. class GiftCardQrCodeService
  13. {
  14. /**
  15. * @notes 生成礼品卡小程序二维码并上传到七牛云
  16. * @param string $cardNo 礼品卡卡号
  17. * @param string $cardPass 礼品卡密码
  18. * @return string|false 返回二维码文件路径或false
  19. */
  20. public static function generateAndUploadQrCode($cardNo, $cardPass)
  21. {
  22. try {
  23. // Generate regular QR code with cardPass as content
  24. $qrContent = $cardPass;
  25. // Generate local temp file path
  26. $tempDir = runtime_path() . 'temp' . DIRECTORY_SEPARATOR;
  27. if (!is_dir($tempDir)) {
  28. mkdir($tempDir, 0755, true);
  29. }
  30. $tempFileName = 'qrcode_' . $cardNo . '_' . time() . '.png';
  31. $tempFilePath = $tempDir . $tempFileName;
  32. // Generate 430x430 square QR code
  33. QrCode::format('png')
  34. ->size(430)
  35. ->margin(2)
  36. ->generate($qrContent, $tempFilePath);
  37. Log::info('Regular QR code generated successfully', [
  38. 'cardNo' => $cardNo,
  39. 'scene' => $cardPass,
  40. 'tempPath' => $tempFilePath,
  41. 'size' => filesize($tempFilePath)
  42. ]);
  43. // Generate Qiniu cloud storage path
  44. $remotePath = 'gift_card_qrcode/' . date('Ymd') . '/' . $tempFileName;
  45. // Upload to Qiniu cloud
  46. if (self::uploadToQiniu($tempFilePath, $remotePath)) {
  47. // Delete temp file
  48. unlink($tempFilePath);
  49. // Return Qiniu cloud file path
  50. $config = ConfigService::get('storage', 'qiniu', []);
  51. $domain = rtrim($config['domain'] ?? '', '/');
  52. $fullUrl = $domain . '/' . $remotePath;
  53. Log::info('Regular QR code generated and uploaded successfully', [
  54. 'cardNo' => $cardNo,
  55. 'remotePath' => $remotePath,
  56. 'fullUrl' => $fullUrl
  57. ]);
  58. return $fullUrl;
  59. } else {
  60. // Upload failed, delete temp file
  61. if (file_exists($tempFilePath)) {
  62. unlink($tempFilePath);
  63. }
  64. return false;
  65. }
  66. } catch (\Exception $e) {
  67. Log::error('Failed to generate regular QR code', [
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. return false;
  72. }
  73. }
  74. public static function httpRequest($url, $data='', $method='GET'){
  75. $curl = curl_init();
  76. curl_setopt($curl, CURLOPT_URL, $url);
  77. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  78. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  79. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0');
  80. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  81. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  82. if($method=='POST') {
  83. curl_setopt($curl, CURLOPT_POST, 1);
  84. if ($data != '') {
  85. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  86. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  87. 'Content-Type: application/json',
  88. 'Content-Length: ' . strlen($data)
  89. ]);
  90. }
  91. }
  92. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  93. curl_setopt($curl, CURLOPT_HEADER, 0);
  94. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  95. $result = curl_exec($curl);
  96. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  97. $error = curl_error($curl);
  98. curl_close($curl);
  99. if ($error) {
  100. Log::error('CURL请求失败', ['error' => $error, 'url' => $url]);
  101. return false;
  102. }
  103. if ($httpCode !== 200) {
  104. Log::error('HTTP请求失败', ['code' => $httpCode, 'url' => $url]);
  105. return false;
  106. }
  107. return $result;
  108. }
  109. /**
  110. * @notes 上传文件到七牛云
  111. * @param string $localPath 本地文件路径
  112. * @param string $remotePath 远程文件路径
  113. * @return bool
  114. */
  115. private static function uploadToQiniu($localPath, $remotePath)
  116. {
  117. try {
  118. // 检查本地文件是否存在
  119. if (!file_exists($localPath)) {
  120. Log::error('本地文件不存在', ['path' => $localPath]);
  121. return false;
  122. }
  123. $config = [
  124. 'default' => ConfigService::get('storage', 'default', 'local'),
  125. 'engine' => ConfigService::get('storage_engine')
  126. ];
  127. Log::info('存储配置', $config);
  128. if ($config['default'] === 'qiniu') {
  129. $storageDriver = new Driver($config);
  130. // 使用绝对路径进行上传
  131. $absolutePath = realpath($localPath);
  132. $result = $storageDriver->fetch($absolutePath, $remotePath);
  133. if (!$result) {
  134. $error = $storageDriver->getError();
  135. Log::error('七牛云上传失败', [
  136. 'error' => $error,
  137. 'localPath' => $absolutePath,
  138. 'remotePath' => $remotePath
  139. ]);
  140. return false;
  141. }
  142. Log::info('七牛云上传成功', [
  143. 'localPath' => $absolutePath,
  144. 'remotePath' => $remotePath
  145. ]);
  146. return true;
  147. } else {
  148. // 如果不是七牛云存储,返回本地路径
  149. Log::info('使用本地存储', ['path' => $localPath]);
  150. return true;
  151. }
  152. } catch (\Exception $e) {
  153. Log::error('上传到七牛云失败', [
  154. 'error' => $e->getMessage(),
  155. 'localPath' => $localPath,
  156. 'remotePath' => $remotePath
  157. ]);
  158. return false;
  159. }
  160. }
  161. /**
  162. * @notes 批量生成二维码
  163. * @param array $giftCards 礼品卡数组
  164. * @return array
  165. */
  166. public static function batchGenerateQrCode($giftCards)
  167. {
  168. $result = [
  169. 'success' => 0,
  170. 'failed' => 0,
  171. 'total' => count($giftCards),
  172. 'details' => []
  173. ];
  174. foreach ($giftCards as $card) {
  175. $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']);
  176. if ($qrCodePath) {
  177. // 更新数据库中的二维码路径
  178. \app\common\model\GiftCardInfo::where('id', $card['id'])
  179. ->update(['qr_code_path' => $qrCodePath]);
  180. $result['success']++;
  181. $result['details'][] = [
  182. 'card_no' => $card['card_no'],
  183. 'status' => 'success',
  184. 'qr_code_path' => $qrCodePath
  185. ];
  186. } else {
  187. $result['failed']++;
  188. $result['details'][] = [
  189. 'card_no' => $card['card_no'],
  190. 'status' => 'failed',
  191. 'error' => '生成或上传失败'
  192. ];
  193. }
  194. }
  195. return $result;
  196. }
  197. }