liuhairui 5 dagar sedan
förälder
incheckning
b9f833d1f4
1 ändrade filer med 53 tillägg och 18 borttagningar
  1. 53 18
      application/api/controller/Product.php

+ 53 - 18
application/api/controller/Product.php

@@ -11,6 +11,14 @@ class Product extends Api
     protected $noNeedLogin = ['*'];
     protected $noNeedRight = ['*'];
 
+    /**
+     * 本地文件同步到 OSS(配置齐全时);失败不影响主流程
+     */
+    protected function maybeUploadToOss($localFullPath, $objectKey)
+    {
+        return Common::uploadLocalFileToOss((string)$localFullPath, (string)$objectKey);
+    }
+
     /**
      * 商户菜单
      * @return void
@@ -139,6 +147,11 @@ class Product extends Api
             unset($item); // 解除引用
         }
 
+        foreach ($list as &$item) {
+            $item['产品图片'] = Common::ossFullUrl((string)$item['产品图片']);
+            $item['产品效果图'] = Common::ossFullUrl((string)$item['产品效果图']);
+        }
+        unset($item);
         $result = [
             'list' => $list,
             'total' => $total,
@@ -157,26 +170,17 @@ class Product extends Api
      */
     public function productDetail()
     {
-        // 1. 请求方法验证
         if (!$this->request->isGet()) {
             $this->error('只支持GET请求');
         }
-
-        // 2. 参数获取与验证
         $param = $this->request->param();
-
         if (empty($param['id']) || !is_numeric($param['id'])) {
             $this->error('产品ID参数错误');
         }
-
-        // 3. 参数安全处理
         $productId = intval($param['id']);
-
         if ($productId <= 0) {
             $this->error('产品ID必须为正整数');
         }
-
-        // 4. 查询数据
         try {
             $product = \db('product')
                 ->field([
@@ -190,13 +194,11 @@ class Product extends Api
                 ])
                 ->where('id', $productId)
                 ->whereNull('deleteTime')
+                ->order('id desc')
                 ->find();
-
         } catch (\Exception $e) {
             $this->error('查询产品详情失败:' . $e->getMessage());
         }
-
-        // 5. 检查查询结果
         if (empty($product)) {
             $this->error('产品不存在或已被删除');
         }
@@ -226,12 +228,39 @@ class Product extends Api
         // 7. 移除原始图片字段,保持返回数据整洁
         unset($product['product_img'], $product['product_new_img']);
 
-        $product_image = \db('product_image')
-            ->where('product_id', $productId)
-            ->whereNull('mod_rq')
-            ->order('id desc')
+        $product_image = \db('product_image')->alias('a')
+            ->field('
+                a.id,
+                a.product_id,
+                a.template_id,
+                a.product_new_img,
+                a.product_content,
+                a.createTime,
+                b.thumbnail_image,
+                b.template_name
+            ')
+            ->join('product_template b', 'a.template_id = b.id', 'LEFT')
+            ->where('a.product_id', $productId)
+            ->whereNull('a.mod_rq')
+            ->order('a.id desc')
             ->select();
 
+        foreach ($product_image as &$item) {
+            if (!empty($item['product_new_img']) || !empty($item['thumbnail_image'])) {
+                $item['product_new_img'] = Common::ossFullUrl((string)$item['product_new_img']);
+                $item['thumbnail_image'] = Common::ossFullUrl((string)$item['thumbnail_image']);
+            }
+        }
+        unset($item);
+
+        // $product 是单条记录,不是列表,直接处理
+        if (!empty($product['产品图片'])) {
+            $product['产品图片'] = Common::ossFullUrl((string)$product['产品图片']);
+        }
+        if (!empty($product['产品效果图'])) {
+            $product['产品效果图'] = Common::ossFullUrl((string)$product['产品效果图']);
+        }
+
         return json([
             'code' => 0,
             'msg' => '获取产品详情成功',
@@ -242,7 +271,7 @@ class Product extends Api
 
     /**
      * 获取单条产品数据信息
-    */
+     */
     public function GetProductFind(){
         if (!$this->request->isGet()) {
             $this->error('只支持GET请求');
@@ -298,6 +327,8 @@ class Product extends Api
                     }
                     if (file_put_contents($saveDir . $fileName, $imageData)) {
                         $productImgPath = 'uploads/merchant/' . $prefix . '/' . $productCode . '/' . 'oldimg/'. $fileName;
+                        // 本地保存成功后,尝试同步 OSS(失败不阻塞新增)
+                        $this->maybeUploadToOss($saveDir . $fileName, $productImgPath);
                     }
                 }
             }
@@ -430,11 +461,12 @@ class Product extends Api
     }
 
     /**
-     * 产品删除(软删除,设置 deleteTime)
+     * 产品删除
      */
     public function Product_Del()
     {
         $params = $this->request->param();
+
         $record['deleteTime'] = date('Y-m-d H:i:s');
         $res = Db::name('product')->where('id', $params['id'])->update($record);
         if (!$res) {
@@ -450,4 +482,7 @@ class Product extends Api
         ]);
     }
 
+
+
+
 }