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