shish 2 hónapja
szülő
commit
bacabf806e

+ 4 - 15
app-hd/controllers/GoodsController.php

@@ -184,23 +184,12 @@ class GoodsController extends BaseController
             util::fail('获取失败');
         }
         GoodsClass::valid($goods, $this->mainId);
-        $cover = $goods->cover ?? '';
-        if (empty($cover)) {
-            $shopImg = $goods->shopImg ?? '';
-            if (!empty($shopImg)) {
-                $imgArr = is_string($shopImg) ? json_decode($shopImg, true) : $shopImg;
-                if (!empty($imgArr[0])) {
-                    $cover = $imgArr[0];
-                }
-            }
-        }
-        if (empty($cover)) {
+        $rawCover = $goods->cover ?? '';
+        $shopImg = $goods->shopImg ?? '';
+        if ($rawCover === '' && ($shopImg === '' || $shopImg === '[]')) {
             util::fail('请先上传花束封面');
         }
-        // OSS 水印只认桶内相对路径,与花材海报一致,不可 groupImg 拼域名
-        if (strpos($cover, 'http://') === 0 || strpos($cover, 'https://') === 0) {
-            $cover = ltrim(parse_url($cover, PHP_URL_PATH) ?? '', '/');
-        }
+        $cover = GoodsClass::getPosterCoverPath($goods);
 
         $merchant = WxOpenClass::getMallWxInfo();
         $page = 'pages/goods/detail';

+ 19 - 0
biz-hd/goods/classes/GoodsClass.php

@@ -283,6 +283,25 @@ class GoodsClass extends BaseClass
         return $return;
     }
 
+    /**
+     * 花束海报主图 OSS 路径:优先 shopImg 第一张(与列表展示一致),并修正 default-img 等
+     */
+    public static function getPosterCoverPath($goods)
+    {
+        $cover = '';
+        $shopImg = is_object($goods) ? ($goods->shopImg ?? '') : ($goods['shopImg'] ?? '');
+        if (!empty($shopImg)) {
+            $arr = is_string($shopImg) ? json_decode($shopImg, true) : $shopImg;
+            if (is_array($arr) && !empty($arr[0])) {
+                $cover = is_string($arr[0]) ? $arr[0] : '';
+            }
+        }
+        if ($cover === '') {
+            $cover = is_object($goods) ? ($goods->cover ?? '') : ($goods['cover'] ?? '');
+        }
+        return imgUtil::normalizeOssObjectPath($cover);
+    }
+
     //获取商品完整详情,包括商品的分类 ssh 2019.12.3
     public static function getGoodsInfo($id, $shop, $custom, $params)
     {

+ 20 - 0
common/components/imgUtil.php

@@ -32,4 +32,24 @@ class imgUtil
         return $prefix . $pic;
     }
 
+    /**
+     * OSS 图片水印用的对象路径(桶内相对路径,不要带域名)
+     * xhGoods.cover 常为 default-img.png,实际对象在 retail/default-img.png
+     */
+    public static function normalizeOssObjectPath($pic)
+    {
+        $pic = trim((string)$pic);
+        if ($pic === '') {
+            return 'retail/default-img.png';
+        }
+        if (strpos($pic, 'http://') === 0 || strpos($pic, 'https://') === 0) {
+            $pic = ltrim(parse_url($pic, PHP_URL_PATH) ?? '', '/');
+        }
+        $pic = ltrim($pic, '/');
+        if ($pic === 'default-img.png' || $pic === 'hhb_small.png') {
+            return 'retail/default-img.png';
+        }
+        return $pic;
+    }
+
 }