GiftCardQrCodeService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $app = Factory::miniProgram($config);
  28. try {
  29. $accessToken = $app->access_token->getToken()['access_token'];
  30. $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken}";
  31. $data = [
  32. 'path' => $page . '?' . $scene,
  33. 'width' => 280
  34. ];
  35. // 使用Guzzle HTTP客户端
  36. $client = new \GuzzleHttp\Client();
  37. $response = $client->post($url, [
  38. 'json' => $data,
  39. 'headers' => [
  40. 'Content-Type' => 'application/json'
  41. ]
  42. ]);
  43. // 检查响应
  44. if ($response->getStatusCode() === 200) {
  45. $body = $response->getBody();
  46. // 这里$body就是二维码的二进制数据
  47. } else {
  48. Log::error('微信API调用失败', ['status' => $response->getStatusCode()]);
  49. return false;
  50. }
  51. } catch (\Exception $e2) {
  52. Log::error('HTTP客户端调用失败', ['error' => $e2->getMessage()]);
  53. return false;
  54. }
  55. if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
  56. Log::error('生成小程序码失败', [
  57. 'response' => $response,
  58. 'response_type' => gettype($response),
  59. 'page' => $page,
  60. 'scene' => $scene
  61. ]);
  62. return false;
  63. }
  64. // if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
  65. // Log::error('生成小程序码失败', ['response' => $response]);
  66. // return false;
  67. // }
  68. return $response;
  69. // 保存到本地临时目录
  70. $saveDir = 'resource/image/gift_card/qr_code/';
  71. if (!file_exists($saveDir)) {
  72. mkdir($saveDir, 0777, true);
  73. }
  74. $fileName = 'gift_card_' . $cardNo . '_' . time() . '.png';
  75. $localPath = $saveDir . $fileName;
  76. // 保存小程序码到本地
  77. $response->saveAs($saveDir, $fileName);
  78. // 上传到七牛云
  79. $uploadPath = self::uploadToQiniu($localPath, 'gift_card/qr_code/' . $fileName);
  80. if ($uploadPath) {
  81. // 删除本地临时文件
  82. if (file_exists($localPath)) {
  83. unlink($localPath);
  84. }
  85. return 'gift_card/qr_code/' . $fileName;
  86. }
  87. return false;
  88. } catch (\Exception $e) {
  89. Log::error('生成礼品卡二维码失败', ['error' => $e->getMessage()]);
  90. return false;
  91. }
  92. }
  93. /**
  94. * @notes 上传文件到七牛云
  95. * @param string $localPath 本地文件路径
  96. * @param string $remotePath 远程文件路径
  97. * @return bool
  98. */
  99. private static function uploadToQiniu($localPath, $remotePath)
  100. {
  101. try {
  102. $config = [
  103. 'default' => ConfigService::get('storage', 'default', 'local'),
  104. 'engine' => ConfigService::get('storage_engine')
  105. ];
  106. if ($config['default'] === 'qiniu') {
  107. $storageDriver = new Driver($config);
  108. return $storageDriver->fetch($localPath, $remotePath);
  109. }
  110. // 如果不是七牛云存储,返回本地路径
  111. return true;
  112. } catch (\Exception $e) {
  113. Log::error('上传到七牛云失败', ['error' => $e->getMessage()]);
  114. return false;
  115. }
  116. }
  117. /**
  118. * @notes 批量生成二维码
  119. * @param array $giftCards 礼品卡数组
  120. * @return array
  121. */
  122. public static function batchGenerateQrCode($giftCards)
  123. {
  124. $result = [
  125. 'success' => 0,
  126. 'failed' => 0,
  127. 'total' => count($giftCards)
  128. ];
  129. foreach ($giftCards as $card) {
  130. $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']);
  131. if ($qrCodePath) {
  132. // 更新数据库中的二维码路径
  133. \app\common\model\GiftCardInfo::where('id', $card['id'])
  134. ->update(['qr_code_path' => $qrCodePath]);
  135. $result['success']++;
  136. } else {
  137. $result['failed']++;
  138. }
  139. }
  140. return $result;
  141. }
  142. }