GiftCardQrCodeService.php 8.0 KB

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