Forráskód Böngészése

update: 优化推广海报逻辑,不用每次生成一遍

joe 4 éve
szülő
commit
507a69dcf2

+ 6 - 3
app/admin/model/system/SystemAttachment.php

@@ -113,9 +113,12 @@ class SystemAttachment extends BaseModel
     public static function getInfo($value, $field = 'att_id')
     {
         $where[$field] = $value;
-        $count = self::where($where)->count();
-        if (!$count) return false;
-        return self::where($where)->find()->toArray();
+        // $count = self::where($where)->count();
+        // if (!$count) return false;
+        // return self::where($where)->find()->toArray();
+        $row = self::where($where)->find();
+        $row = $row ? $row->toArray() : false;
+        return $row;
     }
 
     /**

+ 62 - 16
app/api/controller/user/UserBillController.php

@@ -14,6 +14,7 @@ use crmeb\services\GroupDataService;
 use crmeb\services\SystemConfigService;
 use crmeb\services\UtilService;
 use crmeb\services\upload\Upload;
+use think\facade\Log;
 
 /**
  * 账单类
@@ -154,21 +155,52 @@ class UserBillController
         ], $request, true);
         $user = $request->user();
         $rootPath = app()->getRootPath();
+        $qiniu_path = 'user_share_qrcode';
+
         try {
             $resRoutine = true;//小程序
             $resWap = true;//公众号
             $siteUrl = sys_config('site_url');
             $routineSpreadBanner = sys_data('routine_spread_banner');
-            if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
+            if (!count($routineSpreadBanner)) {
+                return app('json')->fail('暂无海报');
+            }
+            
             if ($type == 1) {
-                //小程序
+                // 直接获取用户海报
+                foreach ($routineSpreadBanner as &$item) {
+                    $item['namep'] = $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg';
+                }
+                $posters = SystemAttachment::whereIn('name', array_column($routineSpreadBanner, 'namep'))->field('name, att_dir')->select()->toArray();
+                if (count($posters)) {
+                    foreach ($routineSpreadBanner as $key => &$item) {
+                        foreach ($posters as $poster) {
+                            if ($item['namep'] == $poster['name']) {
+                                $item['poster'] = $poster['att_dir'];
+                            }
+                        }
+                        unset($item['namep']);
+                        if (!isset($item['poster'])) {
+                            unset($routineSpreadBanner[$key]);
+                        }
+                    }
+                    $routineSpreadBanner = array_values($routineSpreadBanner);
+                    if (count($routineSpreadBanner) > 0) {
+                        return app('json')->successful($routineSpreadBanner);
+                    }
+                }
+
+                //小程序个人分享二维码文件名
                 $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
                 $imageInfo = SystemAttachment::getInfo($name, 'name');
                 //检测远程文件是否存在
-                if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
-                    $imageInfo = null;
-                    SystemAttachment::where(['name' => $name])->delete();
-                }
+                // 20210722 注释掉,太卡
+                // if (isset($imageInfo['att_dir']) && 
+                //     strstr($imageInfo['att_dir'], 'http') !== false && 
+                //     curl_file_exist($imageInfo['att_dir']) === false) {
+                //     $imageInfo = null;
+                //     SystemAttachment::where(['name' => $name])->delete();
+                // }
                 if (!$imageInfo) {
                     $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
                     if (!$res) return app('json')->fail('二维码生成失败');
@@ -180,27 +212,37 @@ class UserBillController
                         'storageName' => sys_config('storage_name'),
                         'storageRegion' => sys_config('storage_region'),
                     ]);
-                    $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
+                    // 上传分享二维码 [ qiniu 添加一个子路径]
+                    $remoteName = $uploadType == 2 ?  $qiniu_path. '/' . $name : $name;
+                    $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $remoteName);
                     if ($uploadRes === false) {
                         return app('json')->fail($upload->getError());
                     }
                     $upload->delete($name);
                     $imageInfo = $upload->getUploadInfo();
                     $imageInfo['image_type'] = $uploadType;
-                    SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
+                    SystemAttachment::attachmentAdd(basename($imageInfo['name']), $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
                     RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
                     $urlCode = $imageInfo['dir'];
-                } else $urlCode = $imageInfo['att_dir'];
-                if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
+                } else {
+                    $urlCode = $imageInfo['att_dir'];
+                }
+                if ($imageInfo['image_type'] == 1) {
+                    $urlCode = $siteUrl . $urlCode;
+                }
                 $siteUrlHttps = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
                 $filelink = [
                     'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
                     'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
                 ];
-                if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
-                if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
+                if (!file_exists($filelink['Bold'])) {
+                     return app('json')->fail('缺少字体文件Bold');
+                }
+                if (!file_exists($filelink['Normal'])) {
+                    return app('json')->fail('缺少字体文件Normal');
+                }
                 foreach ($routineSpreadBanner as $key => &$item) {
-                    $posterInfo = '海报生成失败:(';
+                    $posterInfo = '海报生成失败';
                     $config = array(
                         'image' => array(
                             array(
@@ -235,11 +277,14 @@ class UserBillController
                                 'angle' => 0,
                             )
                         ),
-                        'background' => $item['pic']
+                        'background' => $item['pic'],
+                        'name' => $qiniu_path. '/' . $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg',
                     );
                     $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
-                    if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
-                    SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
+                    if (!is_array($posterInfo)) {
+                        return app('json')->fail($posterInfo);
+                    }
+                    SystemAttachment::attachmentAdd(basename($posterInfo['name']), $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
                     if ($resRoutine) {
                         if ($posterInfo['image_type'] == 1)
                             $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
@@ -324,6 +369,7 @@ class UserBillController
             if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
             else return app('json')->fail('生成图片失败');
         } catch (\Exception $e) {
+            Log::error('生成图片时,系统错误:' . $e->getMessage());
             return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
         }
     }

+ 1 - 1
app/common.php

@@ -54,7 +54,7 @@ if (!function_exists('sys_data')) {
     /**
      * 获取系统单个配置
      * @param string $name
-     * @return string
+     * @return string|array
      */
     function sys_data(string $name, int $limit = 0)
     {

+ 7 - 2
app/models/routine/RoutineQrcode.php

@@ -41,8 +41,13 @@ class RoutineQrcode extends BaseModel
      */
     public static function routineQrCodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
     {
-        $count = self::where('third_id', $thirdId)->where('third_type', $thirdType)->count();
-        if ($count) return self::where('third_id', $thirdId)->where('third_type', $thirdType)->field('id')->find();
+        // $count = self::where('third_id', $thirdId)->where('third_type', $thirdType)->count();
+        // if ($count) return self::where('third_id', $thirdId)->where('third_type', $thirdType)->field('id')->find();
+        // return self::setRoutineQrcodeForever($thirdId, $thirdType, $page, $qrCodeLink);
+        $row = self::where('third_id', $thirdId)->where('third_type', $thirdType)->find();
+        if ($row) {
+            return $row;
+        } 
         return self::setRoutineQrcodeForever($thirdId, $thirdType, $page, $qrCodeLink);
     }
 

+ 1 - 1
crmeb/services/UtilService.php

@@ -253,7 +253,7 @@ class UtilService
         imagedestroy($imageRes);
         $res = ob_get_contents();
         ob_end_clean();
-        $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
+        $key = isset($config['name']) ? $config['name'] : substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
         $uploadType = (int)sys_config('upload_type', 1);
         $upload = new Upload($uploadType, [
             'accessKey' => sys_config('accessKey'),