GiftCardQrCodeService.php 7.9 KB

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