GiftCardQrCodeService.php 8.0 KB

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