GiftCardQrCodeService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Log::error('Failed to generate regular QR code', [
  75. 'error' => $e->getMessage(),
  76. 'trace' => $e->getTraceAsString()
  77. ]);
  78. return false;
  79. }
  80. }
  81. public static function httpRequest($url, $data='', $method='GET'){
  82. $curl = curl_init();
  83. curl_setopt($curl, CURLOPT_URL, $url);
  84. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  85. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  86. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0');
  87. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  88. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  89. if($method=='POST') {
  90. curl_setopt($curl, CURLOPT_POST, 1);
  91. if ($data != '') {
  92. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  93. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  94. 'Content-Type: application/json',
  95. 'Content-Length: ' . strlen($data)
  96. ]);
  97. }
  98. }
  99. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  100. curl_setopt($curl, CURLOPT_HEADER, 0);
  101. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  102. $result = curl_exec($curl);
  103. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  104. $error = curl_error($curl);
  105. curl_close($curl);
  106. if ($error) {
  107. Log::error('CURL请求失败', ['error' => $error, 'url' => $url]);
  108. return false;
  109. }
  110. if ($httpCode !== 200) {
  111. Log::error('HTTP请求失败', ['code' => $httpCode, 'url' => $url]);
  112. return false;
  113. }
  114. return $result;
  115. }
  116. /**
  117. * @notes 上传文件到七牛云
  118. * @param string $localPath 本地文件路径
  119. * @param string $remotePath 远程文件路径
  120. * @return bool
  121. */
  122. private static function uploadToQiniu($localPath, $remotePath)
  123. {
  124. try {
  125. // 检查本地文件是否存在
  126. if (!file_exists($localPath)) {
  127. Log::error('本地文件不存在', ['path' => $localPath]);
  128. return false;
  129. }
  130. $config = [
  131. 'default' => ConfigService::get('storage', 'default', 'local'),
  132. 'engine' => ConfigService::get('storage_engine')
  133. ];
  134. Log::info('存储配置', $config);
  135. if ($config['default'] === 'qiniu') {
  136. $storageDriver = new Driver($config);
  137. // 使用绝对路径进行上传
  138. $absolutePath = realpath($localPath);
  139. $result = $storageDriver->fetch($absolutePath, $remotePath);
  140. if (!$result) {
  141. $error = $storageDriver->getError();
  142. Log::error('七牛云上传失败', [
  143. 'error' => $error,
  144. 'localPath' => $absolutePath,
  145. 'remotePath' => $remotePath
  146. ]);
  147. return false;
  148. }
  149. Log::info('七牛云上传成功', [
  150. 'localPath' => $absolutePath,
  151. 'remotePath' => $remotePath
  152. ]);
  153. return true;
  154. } else {
  155. // 如果不是七牛云存储,返回本地路径
  156. Log::info('使用本地存储', ['path' => $localPath]);
  157. return true;
  158. }
  159. } catch (\Exception $e) {
  160. Log::error('上传到七牛云失败', [
  161. 'error' => $e->getMessage(),
  162. 'localPath' => $localPath,
  163. 'remotePath' => $remotePath
  164. ]);
  165. return false;
  166. }
  167. }
  168. /**
  169. * @notes 批量生成二维码
  170. * @param array $giftCards 礼品卡数组
  171. * @return array
  172. */
  173. public static function batchGenerateQrCode($giftCards)
  174. {
  175. $result = [
  176. 'success' => 0,
  177. 'failed' => 0,
  178. 'total' => count($giftCards),
  179. 'details' => []
  180. ];
  181. foreach ($giftCards as $card) {
  182. $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']);
  183. if ($qrCodePath) {
  184. // 更新数据库中的二维码路径
  185. \app\common\model\GiftCardInfo::where('id', $card['id'])
  186. ->update(['qr_code_path' => $qrCodePath]);
  187. $result['success']++;
  188. $result['details'][] = [
  189. 'card_no' => $card['card_no'],
  190. 'status' => 'success',
  191. 'qr_code_path' => $qrCodePath
  192. ];
  193. } else {
  194. $result['failed']++;
  195. $result['details'][] = [
  196. 'card_no' => $card['card_no'],
  197. 'status' => 'failed',
  198. 'error' => '生成或上传失败'
  199. ];
  200. }
  201. }
  202. return $result;
  203. }
  204. }