| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace app\common\service;
- use app\common\service\storage\Driver;
- use app\common\service\ConfigService;
- use app\common\service\WeChatConfigService;
- use EasyWeChat\Factory;
- use think\facade\Log;
- /**
- * 礼品卡二维码生成服务
- */
- class GiftCardQrCodeService
- {
- /**
- * @notes 生成礼品卡小程序二维码并上传到七牛云
- * @param string $cardNo 礼品卡卡号
- * @param string $cardPass 礼品卡密码
- * @return string|false 返回二维码文件路径或false
- */
- public static function generateAndUploadQrCode($cardNo, $cardPass)
- {
- try {
- // 小程序页面路径,根据实际情况修改
- $page = 'pages/gift-card/exchange';
- $scene = "cardNo={$cardNo}&cardPass={$cardPass}";
- // 生成小程序码
- $config = WeChatConfigService::getMnpConfig();
- $app = Factory::miniProgram($config);
- try {
- $accessToken = $app->access_token->getToken()['access_token'];
- $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken}";
- $data = [
- 'path' => $page . '?' . $scene,
- 'width' => 280
- ];
- // 使用Guzzle HTTP客户端
- $client = new \GuzzleHttp\Client();
- $response = $client->post($url, [
- 'json' => $data,
- 'headers' => [
- 'Content-Type' => 'application/json'
- ]
- ]);
- // 检查响应
- if ($response->getStatusCode() === 200) {
- $body = $response->getBody();
- // 这里$body就是二维码的二进制数据
- } else {
- Log::error('微信API调用失败', ['status' => $response->getStatusCode()]);
- return false;
- }
- } catch (\Exception $e2) {
- Log::error('HTTP客户端调用失败', ['error' => $e2->getMessage()]);
- return false;
- }
- if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
- Log::error('生成小程序码失败', [
- 'response' => $response,
- 'response_type' => gettype($response),
- 'page' => $page,
- 'scene' => $scene
- ]);
- return false;
- }
- // if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
- // Log::error('生成小程序码失败', ['response' => $response]);
- // return false;
- // }
- return $response;
- // 保存到本地临时目录
- $saveDir = 'resource/image/gift_card/qr_code/';
- if (!file_exists($saveDir)) {
- mkdir($saveDir, 0777, true);
- }
- $fileName = 'gift_card_' . $cardNo . '_' . time() . '.png';
- $localPath = $saveDir . $fileName;
- // 保存小程序码到本地
- $response->saveAs($saveDir, $fileName);
- // 上传到七牛云
- $uploadPath = self::uploadToQiniu($localPath, 'gift_card/qr_code/' . $fileName);
- if ($uploadPath) {
- // 删除本地临时文件
- if (file_exists($localPath)) {
- unlink($localPath);
- }
- return 'gift_card/qr_code/' . $fileName;
- }
- return false;
- } catch (\Exception $e) {
- Log::error('生成礼品卡二维码失败', ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * @notes 上传文件到七牛云
- * @param string $localPath 本地文件路径
- * @param string $remotePath 远程文件路径
- * @return bool
- */
- private static function uploadToQiniu($localPath, $remotePath)
- {
- try {
- $config = [
- 'default' => ConfigService::get('storage', 'default', 'local'),
- 'engine' => ConfigService::get('storage_engine')
- ];
- if ($config['default'] === 'qiniu') {
- $storageDriver = new Driver($config);
- return $storageDriver->fetch($localPath, $remotePath);
- }
- // 如果不是七牛云存储,返回本地路径
- return true;
- } catch (\Exception $e) {
- Log::error('上传到七牛云失败', ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * @notes 批量生成二维码
- * @param array $giftCards 礼品卡数组
- * @return array
- */
- public static function batchGenerateQrCode($giftCards)
- {
- $result = [
- 'success' => 0,
- 'failed' => 0,
- 'total' => count($giftCards)
- ];
- foreach ($giftCards as $card) {
- $qrCodePath = self::generateAndUploadQrCode($card['card_no'], $card['card_pass']);
- if ($qrCodePath) {
- // 更新数据库中的二维码路径
- \app\common\model\GiftCardInfo::where('id', $card['id'])
- ->update(['qr_code_path' => $qrCodePath]);
- $result['success']++;
- } else {
- $result['failed']++;
- }
- }
- return $result;
- }
- }
|