GiftCardQrCodeService.php 5.8 KB

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