GiftCardQrCodeService.php 6.6 KB

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