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