GiftCardQrCodeService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\common\service;
  3. use app\common\service\storage\Driver;
  4. use app\common\service\ConfigService;
  5. use app\common\service\WeChatConfigService;
  6. use EasyWeChat\Factory;
  7. use think\facade\Log;
  8. /**
  9. * 礼品卡二维码生成服务
  10. */
  11. class GiftCardQrCodeService
  12. {
  13. /**
  14. * @notes 生成礼品卡小程序二维码并上传到七牛云
  15. * @param string $cardNo 礼品卡卡号
  16. * @param string $cardPass 礼品卡密码
  17. * @return string|false 返回二维码文件路径或false
  18. */
  19. public static function generateAndUploadQrCode($cardNo, $cardPass)
  20. {
  21. try {
  22. // 小程序页面路径,根据实际情况修改
  23. $page = 'pages/gift-card/exchange';
  24. $scene = "cardNo={$cardNo}&cardPass={$cardPass}";
  25. // 生成小程序码
  26. $config = WeChatConfigService::getMnpConfig();
  27. return $config;
  28. $app = Factory::miniProgram($config);
  29. // 修正:使用正确的API方法
  30. // $response = $app->app_code->getUnlimited($scene, [
  31. // 'page' => $page,
  32. // 'width' => 280,
  33. // 'auto_color' => false,
  34. // 'line_color' => ['r' => 0, 'g' => 0, 'b' => 0],
  35. // ]);
  36. // 如果上面的方法不工作,尝试以下替代方案:
  37. // 方案1:使用get方法
  38. // $response = $app->app_code->get($page . '?' . $scene, [
  39. // 'width' => 280,
  40. // ]);
  41. outFileLog('1','giftCard','test');
  42. // 方案2:使用getQrCode方法
  43. $response = $app->app_code->getQrCode($page . '?' . $scene, 280
  44. );
  45. if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
  46. Log::error('生成小程序码失败', ['response' => $response]);
  47. return false;
  48. }
  49. // 保存到本地临时目录
  50. $saveDir = 'resource/image/gift_card/qr_code/';
  51. if (!file_exists($saveDir)) {
  52. mkdir($saveDir, 0777, true);
  53. }
  54. $fileName = 'gift_card_' . $cardNo . '_' . time() . '.png';
  55. $localPath = $saveDir . $fileName;
  56. // 保存小程序码到本地
  57. $response->saveAs($saveDir, $fileName);
  58. // 上传到七牛云
  59. $uploadPath = self::uploadToQiniu($localPath, 'gift_card/qr_code/' . $fileName);
  60. if ($uploadPath) {
  61. // 删除本地临时文件
  62. if (file_exists($localPath)) {
  63. unlink($localPath);
  64. }
  65. return 'gift_card/qr_code/' . $fileName;
  66. }
  67. return false;
  68. } catch (\Exception $e) {
  69. Log::error('生成礼品卡二维码失败', ['error' => $e->getMessage()]);
  70. return false;
  71. }
  72. }
  73. /**
  74. * @notes 上传文件到七牛云
  75. * @param string $localPath 本地文件路径
  76. * @param string $remotePath 远程文件路径
  77. * @return bool
  78. */
  79. private static function uploadToQiniu($localPath, $remotePath)
  80. {
  81. try {
  82. $config = [
  83. 'default' => ConfigService::get('storage', 'default', 'local'),
  84. 'engine' => ConfigService::get('storage_engine')
  85. ];
  86. if ($config['default'] === 'qiniu') {
  87. $storageDriver = new Driver($config);
  88. return $storageDriver->fetch($localPath, $remotePath);
  89. }
  90. // 如果不是七牛云存储,返回本地路径
  91. return true;
  92. } catch (\Exception $e) {
  93. Log::error('上传到七牛云失败', ['error' => $e->getMessage()]);
  94. return false;
  95. }
  96. }
  97. /**
  98. * @notes 批量生成二维码
  99. * @param array $giftCards 礼品卡数组
  100. * @return array
  101. */
  102. public static function batchGenerateQrCode($giftCards)
  103. {
  104. $result = [
  105. 'success' => 0,
  106. 'failed' => 0,
  107. 'total' => count($giftCards)
  108. ];
  109. foreach ($giftCards as $card) {
  110. $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']);
  111. if ($qrCodePath) {
  112. // 更新数据库中的二维码路径
  113. \app\common\model\GiftCardInfo::where('id', $card['id'])
  114. ->update(['qr_code_path' => $qrCodePath]);
  115. $result['success']++;
  116. } else {
  117. $result['failed']++;
  118. }
  119. }
  120. return $result;
  121. }
  122. }