浏览代码

海报图优化

shish 1 月之前
父节点
当前提交
8777c367fe
共有 2 个文件被更改,包括 52 次插入6 次删除
  1. 27 3
      app-ghs/controllers/MainController.php
  2. 25 3
      app-hd/controllers/MainController.php

+ 27 - 3
app-ghs/controllers/MainController.php

@@ -56,15 +56,35 @@ class MainController extends BaseController
         util::success(['info' => $main]);
     }
 
-    //采购商城海报 ssh 20230217
+    /**
+     * 职责:生成并获取采购商城太阳码海报
+     * 入参:HTTP GET (参数可选:env_version)
+     * 返回:太阳码海报图片的完整 OSS 地址 JSON
+     * 副作用:当没有缓存时,会触发向微信 API 请求并同步上传生成的图片至阿里 OSS。
+     * 关键边界:引入了店员+环境+门店级别的最终海报数据 Redis 强缓存(3天有效期),避免每次高频无端向微信和 OSS 发起同步请求。
+     */
     public function actionGetMallPoster()
     {
+        $shopId = $this->shopId;
+        $adminId = $this->shopAdminId;
+        $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
+
+        // ==================== 【性能优化】引入最终海报结果的 Redis 全局强缓存 ====================
+        // 缓存键包含店员ID、门店ID、环境版本,保证多维度的唯一性
+        $posterCacheKey = "wx_mini_purchase_mall_poster_url_{$shopId}_{$adminId}_{$envVersion}";
+        $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
+        if (!empty($cachedPoster)) {
+            $respond = json_decode($cachedPoster, true);
+            if (!empty($respond['imgUrl'])) {
+                util::success($respond); // 缓存直接命中,毫秒级快速返回!
+            }
+        }
+
         $merchant = WxOpenClass::getWxInfo();
         $page = 'pagesPurchase/ghsProduct';
         $ptStyle = dict::getDict('ptStyle', 'hd');
         //scene不能超过32个字条,这些进行缩写:sId = shopId gId = ghsShopAdminId
-        $scene = "sId={$this->shopId}&id=0&gId=" . $this->shopAdminId;
-        $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
+        $scene = "sId={$shopId}&id=0&gId=" . $adminId;
         $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
         $titleBase64 = stringUtil::ossBase64('扫码买花');
         $prefix = imgUtil::getPrefix();
@@ -73,6 +93,10 @@ class MainController extends BaseController
         $url .= '/watermark,image_' . $goodsMiniCodeBase64 . ',g_north,x_0,y_40';
         $url .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
         $respond = ['imgUrl' => $url];
+
+        // ==================== 【性能优化】保存最终拼接和生成结果到 Redis 中,过期时间 3 天 ====================
+        Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
+
         util::success($respond);
     }
 

+ 25 - 3
app-hd/controllers/MainController.php

@@ -183,19 +183,37 @@ class MainController extends BaseController
         util::success($respond);
     }
 
-    //绿植花材的太阳码 ssh 20250925
+    /**
+     * 职责:生成并获取绿植花材的太阳码海报及短链接
+     * 入参:HTTP GET (参数可选:env_version)
+     * 返回:太阳码海报图片的完整 OSS 地址以及短链接 URL
+     * 副作用:当没有缓存时,会触发向微信 API 请求并同步上传生成的图片至阿里 OSS。
+     * 关键边界:引入了店员+环境+门店级别的最终海报数据 Redis 强缓存(3天有效期),避免每次高频无端向微信和 OSS 发起同步请求。
+     */
     public function actionGetItemPoster()
     {
         $shop = $this->shop;
         $shopId = $this->shopId;
         $pfShopId = $shop->pfShopId ?? 0;
+        $adminId = $this->shopAdminId;
+        $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
+
+        // ==================== 【性能优化】引入最终海报结果的 Redis 全局强缓存 ====================
+        // 缓存键包含店员ID、门店ID、环境版本,保证多维度的唯一性
+        $posterCacheKey = "wx_mini_item_poster_url_{$shopId}_{$adminId}_{$envVersion}";
+        $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
+        if (!empty($cachedPoster)) {
+            $respond = json_decode($cachedPoster, true);
+            if (!empty($respond['flowerUrl']) && !empty($respond['flowerLink'])) {
+                util::success($respond); // 缓存直接命中,毫秒级快速返回!
+            }
+        }
 
         $merchant = WxOpenClass::getMallWxInfo();
         $ptStyle = dict::getDict('ptStyle', 'mall');
-        $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
+        $scene = 'a=' . $adminId . '&s=' . $shopId;
 
         $flowerPage = 'pages/item/item';
-        $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
         $flowerImgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $flowerPage, $scene, $ptStyle, $envVersion);
 
         $sjName = $shop->merchantName ?? '';
@@ -229,6 +247,10 @@ class MainController extends BaseController
             'flowerUrl' => $flowerUrl,
             'flowerLink' => $flowerLink,
         ];
+
+        // ==================== 【性能优化】保存最终拼接和生成结果到 Redis 中,过期时间 3 天 ====================
+        Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
+
         util::success($respond);
     }