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