GiftCardQrCodeService.php 7.6 KB

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