Selaa lähdekoodia

批发端-生成邀请开店小程序二维码

ouyang 10 tuntia sitten
vanhempi
commit
fc9740fa4c
2 muutettua tiedostoa jossa 71 lisäystä ja 14 poistoa
  1. 67 13
      app-ghs/controllers/MainInviteController.php
  2. 4 1
      common/components/miniUtil.php

+ 67 - 13
app-ghs/controllers/MainInviteController.php

@@ -10,8 +10,10 @@ use bizHd\shop\classes\MainInviteCommissionClass;
 use bizHd\shop\classes\MainInviteFlowClass;
 use bizHd\wx\classes\WxOpenClass;
 use common\components\dict;
+use common\components\dirUtil;
 use common\components\imgUtil;
 use common\components\miniUtil;
+use common\components\stringUtil;
 use common\components\util;
 use Yii;
 
@@ -103,33 +105,85 @@ class MainInviteController extends BaseController
     }
 
     /**
-     * 生成 hdApp 邀请开店小程序码(落地 applyInvite,scene 带 inviteCode
-     * GET:可选 envVersion=develop|trial|release
+     * 生成 hdApp 邀请开店海报(参考 main/get-mall-poster:太阳码 + OSS 背景合成
+     * GET:可选 env_version=develop|trial|release
      */
-    public function actionInviteHdMiniCode()
+    public function actionGetInviteHdPoster()
     {
-        $invite = MainInviteClass::getByMainId((int) $this->mainId);
+        $mainId = (int) $this->mainId;
+        $invite = MainInviteClass::getByMainId($mainId);
         $inviteCode = trim((string) ($invite['inviteCode'] ?? ''));
         if ($inviteCode === '') {
             util::fail('暂无邀请码');
         }
 
-        $get = Yii::$app->request->get();
-        $envVersion = trim((string) ($get['envVersion'] ?? ''));
-        if ($envVersion === '') {
-            $envVersion = getenv('YII_ENV') === 'production' ? 'release' : 'develop';
+        $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
+
+        $posterCacheKey = 'wx_mini_invite_hd_poster_v3_' . $mainId . '_' . $inviteCode . '_' . $envVersion;
+        $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
+        if (!empty($cachedPoster)) {
+            $respond = json_decode($cachedPoster, true);
+            if (!empty($respond['imgUrl']) && !empty($respond['miniCodeUrl'])) {
+                util::success($respond);
+            }
         }
 
         $merchant = WxOpenClass::getWxInfo();
         $page = 'pagesClient/official/applyInvite';
         $ptStyle = dict::getDict('ptStyle', 'hd');
         $scene = 'inviteCode=' . $inviteCode;
-        $imgPath = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
-
-        util::success([
-            'imgUrl' => imgUtil::groupImg($imgPath),
+        // 开通页可能尚未发布到体验版,check_path=false 仍可生成码图;并校验本地 JPEG 避免缓存错误 JSON
+        $imgUrl = $this->ensureValidInviteHdMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
+        $miniCodeUrl = imgUtil::groupImg($imgUrl);
+        $titleBase64 = stringUtil::ossBase64('邀请开店');
+        $prefix = imgUtil::getPrefix();
+        $url = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
+        $miniCodeBase64 = stringUtil::ossBase64($imgUrl);
+        $url .= '/watermark,image_' . $miniCodeBase64 . ',g_north,x_0,y_40';
+        $url .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
+        $respond = [
+            'imgUrl' => $url,
+            'miniCodeUrl' => $miniCodeUrl,
             'inviteCode' => $inviteCode,
-        ]);
+        ];
+
+        Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
+
+        util::success($respond);
+    }
+
+    /**
+     * 生成 hd 邀请开店小程序码相对路径,无效缓存文件时删除并重试一次
+     */
+    private function ensureValidInviteHdMiniCode($merchant, $page, $scene, $ptStyle, $envVersion)
+    {
+        $relative = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion, false);
+        $fullPath = dirUtil::getImgDir() . $relative;
+        if ($this->isValidJpegFile($fullPath)) {
+            return $relative;
+        }
+        if (is_file($fullPath)) {
+            @unlink($fullPath);
+        }
+        $relative = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion, false);
+        $fullPath = dirUtil::getImgDir() . $relative;
+        if (!$this->isValidJpegFile($fullPath)) {
+            if (is_file($fullPath)) {
+                @unlink($fullPath);
+            }
+            util::fail('小程序码生成失败,请确认花店端已上传体验版并包含开通页');
+        }
+        return $relative;
+    }
+
+    /** 判断本地文件是否为有效 JPEG(微信失败时可能写入 JSON 文本) */
+    private function isValidJpegFile($fullPath)
+    {
+        if (!is_file($fullPath) || filesize($fullPath) < 100) {
+            return false;
+        }
+        $head = @file_get_contents($fullPath, false, null, 0, 2);
+        return $head === "\xFF\xD8";
     }
 
 }

+ 4 - 1
common/components/miniUtil.php

@@ -1609,7 +1609,7 @@ class miniUtil
         return $envVersion;
     }
 
-    public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $ptStyle = 0, $envVersion = 'release')
+    public static function generateUnlimitedMiniCode($merchant, $page, $scene = '', $ptStyle = 0, $envVersion = 'release', $checkPath = null)
     {
         $envVersion = self::normalizeMiniEnvVersion($envVersion);
         $id = isset($merchant['id']) ? $merchant['id'] : 0;
@@ -1631,6 +1631,9 @@ class miniUtil
             $data["page"] = $page;
         }
         $data["scene"] = $scene;
+        if ($checkPath === false) {
+            $data["check_path"] = false;
+        }
         if ($envVersion !== 'release') {
             $data["env_version"] = $envVersion;
         }