moonsflyer 6 месяцев назад
Родитель
Сommit
a6f182dd38

+ 19 - 2
app/adminapi/lists/gift_card/GiftCardInfoLists.php

@@ -25,6 +25,7 @@ use app\common\{
     lists\ListsExcelInterface,
     lists\ListsExtendInterface,
     model\GiftCardInfo};
+use app\common\service\GiftCardQrCodeService;
 
 
 /**
@@ -34,8 +35,24 @@ use app\common\{
  */
 class GiftCardInfoLists extends BaseAdminDataLists implements ListsExtendInterface,ListsExcelInterface
 {
+    // ... existing code ...
 
     /**
+     * 批量生成二维码
+     * @return array
+     */
+    public function batchGenerateQrCode()
+    {
+        $giftCards = GiftCardInfo::where($this->setSearch())
+            ->field('id,card_no,card_pass')
+            ->select()
+            ->toArray();
+
+        return GiftCardQrCodeService::batchGenerateQrCode($giftCards);
+    }
+
+    // ... existing code ...
+    /**
      * @notes 搜索条件
      * @return array
      * @author cjhao
@@ -79,7 +96,6 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExtendInterfa
         return [];
     }
 
-
     /**
      * @notes 礼品卡列表
      * @return array
@@ -94,7 +110,7 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExtendInterfa
 
         $lists = GiftCardInfo::where($this->setSearch())
             ->with(['user'])
-            ->append(['is_used_desc','used_user_name','batch_no'])
+            ->append(['is_used_desc','used_user_name','batch_no','qr_code_ur'])
             ->limit($this->limitOffset, $this->limitLength)
             ->order('id', 'desc')
             ->select()
@@ -152,4 +168,5 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExtendInterfa
     }
 
 
+
 }

+ 6 - 0
app/adminapi/logic/gift_card/GiftCardLogic.php

@@ -26,6 +26,7 @@ use app\common\model\GiftCardInfo;
 use app\common\model\GiftCard;
 use app\common\service\ConfigService;
 use app\common\service\FileService;
+use app\common\service\GiftCardQrCodeService;
 use think\facade\Db;
 
 class GiftCardLogic extends BaseLogic
@@ -94,6 +95,11 @@ class GiftCardLogic extends BaseLogic
                 $saveData['card_no'] = $start_no;
                 $pass = gift_card_pass();
                 $saveData['card_pass'] = $pass;
+
+                // 生成小程序二维码并上传到七牛云
+                $qrCodePath = GiftCardQrCodeService::generateAndUploadQrCode($start_no, $pass);
+                $saveData['qr_code_path'] = $qrCodePath ?: '';
+
                 $start_no ++;
                 $gcidata[]=$saveData;
             }

+ 20 - 0
app/common/model/GiftCardInfo.php

@@ -38,6 +38,26 @@ class GiftCardInfo extends BaseModel
     // 设置JSON数据返回数组
 //    protected $jsonAssoc = true;
 
+// ... existing code ...
+
+    /**
+     * @notes 获取二维码URL
+     * @param $value
+     * @param $data
+     * @return string
+     * @author
+     * @date
+     */
+    public function getQrCodeUrlAttr($value, $data)
+    {
+        if (!empty($data['qr_code_path'])) {
+            return FileService::getFileUrl($data['qr_code_path']);
+        }
+        return '';
+    }
+
+    // ... existing code ...
+
     /**
      * @notes 关联用户模型
      * @return \think\model\relation\HasOne

+ 132 - 0
app/common/service/GiftCardQrCodeService.php

@@ -0,0 +1,132 @@
+<?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);
+
+            $response = $app->app_code->getUnlimited($scene, [
+                'page' => $page,
+                'width' => 280,
+                'auto_color' => false,
+                'line_color' => ['r' => 0, 'g' => 0, 'b' => 0],
+            ]);
+
+            if (!($response instanceof \EasyWeChat\Kernel\Http\StreamResponse)) {
+                Log::error('生成小程序码失败', ['response' => $response]);
+                return false;
+            }
+
+            // 保存到本地临时目录
+            $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;
+    }
+}