GiftCardQrCodeService.php 5.5 KB

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