Explorar o código

零售端-商品分类页面分类设置、公告相关接口

ouyang hai 2 semanas
pai
achega
847e9bee0f

+ 4 - 4
app-hd/controllers/CategoryController.php

@@ -108,13 +108,13 @@ class CategoryController extends BaseController
             $defaultCategoryId = $defaultCategory['id'];
             foreach ($goodsCateObjs as $gcObj) {
                 // 先判断商品是否也在其它分类下
-                $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId' => $this->mainId, 'gId' => $gcObj['gId']]);
+                $goodsOtherCateCount = GoodsCategoryClass::getCount(['mainId' => $this->mainId, 'gId' => $gcObj['gpId']]);
                 if ($goodsOtherCateCount == 1) {
                     // 如果商品仅在此分类下,则归到默认分类
-                    GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId, 'delStatus' => 0]);
+                    GoodsCategoryClass::updateById($gcObj['id'], ['cId' => $defaultCategoryId]);
                 } else {
                     // 如果商品还在其它分类下,则直接删除此商品的当前分类
-                    GoodsCategoryClass::delGoodsCategory($this->mainId, $categoryId, $gcObj['gId']);
+                    GoodsCategoryClass::deleteById($gcObj['id']);
                 }
             }
         }
@@ -173,4 +173,4 @@ class CategoryController extends BaseController
         util::success($return);
     }
 
-}
+}

+ 43 - 0
app-hd/controllers/CategorySettingController.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace hd\controllers;
+
+use bizHd\goods\classes\CategorySettingClass;
+use common\components\util;
+use Yii;
+
+/**
+ * 商品分类配置:商城花材分类设置
+ */
+class CategorySettingController extends BaseController
+{
+    /**
+     * 读取当前门店商城分类设置
+     */
+    public function actionDetail()
+    {
+        $info = CategorySettingClass::getOrInit($this->mainId, $this->shopId);
+        util::success($info);
+    }
+
+    /**
+     * 保存商城分类设置
+     */
+    public function actionSave()
+    {
+        $post = Yii::$app->request->post();
+        $info = CategorySettingClass::saveSetting($this->mainId, $this->shopId, $post);
+        util::success($info, '保存成功');
+    }
+
+    /**
+     * 保存分类广告(仅 advertisement 字段)
+     */
+    public function actionSaveAdvertisement()
+    {
+        $post = Yii::$app->request->post();
+        $advertisement = $post['advertisement'] ?? '';
+        $info = CategorySettingClass::saveAdvertisement($this->mainId, $this->shopId, $advertisement);
+        util::success($info, '保存成功');
+    }
+}

+ 36 - 0
app-mall/controllers/CategorySettingController.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace mall\controllers;
+
+use bizHd\goods\classes\CategorySettingClass;
+use common\components\util;
+use Yii;
+
+/**
+ * 商城客户端:分类页配置(xhCategorySetting)
+ */
+class CategorySettingController extends BaseController
+{
+    /**
+     * 读取当前门店分类页配置(花材展示名、置顶、广告等)
+     * GET /category-setting/detail
+     */
+    public function actionDetail()
+    {
+        $user = $this->user;
+        if (empty($user) || empty($this->mainId) || empty($this->shopId)) {
+            util::success([
+                'id' => 0,
+                'mainId' => intval($this->mainId),
+                'shopId' => intval($this->shopId),
+                'itemCateName' => '',
+                'isItemCateTop' => 0,
+                'isItemCateShow' => 0,
+                'advertisement' => '',
+            ]);
+        }
+
+        $info = CategorySettingClass::getOrInit($this->mainId, $this->shopId);
+        util::success($info);
+    }
+}

+ 40 - 0
app-mall/controllers/PicTextController.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace mall\controllers;
+
+use bizMall\pictext\classes\PicTextClass;
+use common\components\util;
+use Yii;
+
+/**
+ * 商城客户端:图文详情(分类广告跳转等)
+ */
+class PicTextController extends BaseController
+{
+    /**
+     * 图文详情
+     * GET /pic-text/detail?id=
+     */
+    public function actionDetail()
+    {
+        $user = $this->user;
+        if (empty($user) || empty($this->mainId)) {
+            util::fail('请先登录哈');
+        }
+
+        $id = intval(Yii::$app->request->get('id', 0));
+        if ($id <= 0) {
+            util::fail('图文id不对');
+        }
+
+        $picText = PicTextClass::getById($id, true);
+        if (empty($picText)) {
+            util::fail('没有找到图文');
+        }
+        if (intval($picText->mainId) !== intval($this->mainId)) {
+            util::fail('没有权限');
+        }
+
+        util::success($picText);
+    }
+}

+ 66 - 0
app-mall/controllers/ShopNoticeController.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace mall\controllers;
+
+use bizHd\shop\classes\ShopNoticeClass;
+use common\components\util;
+use Yii;
+
+/**
+ * 商城客户端:门店通知公告(xhShopNotice)
+ */
+class ShopNoticeController extends BaseController
+{
+    /**
+     * 客户端公告列表(仅上架、未删除)
+     * GET /shop-notice/show-list?position=2
+     */
+    public function actionShowList()
+    {
+        $user = $this->user;
+        if (empty($user) || empty($this->mainId)) {
+            util::success([
+                'totalNum' => 0,
+                'totalPage' => 0,
+                'moreData' => 0,
+                'list' => [],
+            ]);
+        }
+
+        $get = Yii::$app->request->get();
+        $list = ShopNoticeClass::searchList([
+            'mainId' => $this->mainId,
+            'title' => $get['title'] ?? '',
+            'position' => $get['position'] ?? '',
+            'isDel' => 0,
+            'status' => 1,
+            'order' => 'sort DESC, id DESC',
+        ]);
+        util::success($list);
+    }
+
+    /**
+     * 客户端公告详情
+     * GET /shop-notice/detail?id=
+     */
+    public function actionDetail()
+    {
+        $user = $this->user;
+        if (empty($user) || empty($this->mainId)) {
+            util::fail('请先登录哈');
+        }
+
+        $id = intval(Yii::$app->request->get('id', 0));
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+
+        $info = ShopNoticeClass::getById($id);
+        ShopNoticeClass::valid($info, $this->mainId);
+        if (intval($info['status']) !== 1) {
+            util::fail('公告已下架');
+        }
+
+        util::success(ShopNoticeClass::getDetail($id));
+    }
+}

+ 163 - 0
biz-hd/goods/classes/CategorySettingClass.php

@@ -0,0 +1,163 @@
+<?php
+
+namespace bizHd\goods\classes;
+
+use bizHd\base\classes\BaseClass;
+use common\components\util;
+
+/**
+ * 商品分类配置业务:商城花材分类名称、置顶、展示
+ */
+class CategorySettingClass extends BaseClass
+{
+    public static $baseFile = '\bizHd\goods\models\CategorySetting';
+
+    /**
+     * 校验操作权限
+     * @param array|null $info
+     * @param int $mainId
+     */
+    public static function valid($info, $mainId)
+    {
+        if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
+            util::fail('没有权限操作');
+        }
+    }
+
+    /**
+     * 读取当前门店配置,不存在则返回默认结构
+     * @param int $mainId
+     * @param int $shopId
+     * @return array
+     */
+    public static function getOrInit($mainId, $shopId)
+    {
+        $info = self::getByCondition([
+            'mainId' => intval($mainId),
+            'shopId' => intval($shopId),
+        ]);
+        if (empty($info)) {
+            return [
+                'id' => 0,
+                'mainId' => intval($mainId),
+                'shopId' => intval($shopId),
+                'itemCateName' => '',
+                'isItemCateTop' => 0,
+                'isItemCateShow' => 0,
+                'advertisement' => '',
+            ];
+        }
+        return self::formatInfo($info);
+    }
+
+    /**
+     * 保存商城分类设置(按 mainId + shopId 唯一 upsert)
+     * @param int $mainId
+     * @param int $shopId
+     * @param array $data
+     * @return array
+     */
+    public static function saveSetting($mainId, $shopId, $data)
+    {
+        $itemCateName = trim($data['itemCateName'] ?? '');
+        if ($itemCateName === '') {
+            util::fail('请输入商城分类名称');
+        }
+        if (mb_strlen($itemCateName, 'UTF-8') > 20) {
+            util::fail('商城分类名称最多20个字');
+        }
+
+        $saveData = [
+            'itemCateName' => $itemCateName,
+            'isItemCateTop' => intval($data['isItemCateTop'] ?? 0) ? 1 : 0,
+            'isItemCateShow' => intval($data['isItemCateShow'] ?? 0) ? 1 : 0,
+        ];
+
+        $exists = self::getByCondition([
+            'mainId' => intval($mainId),
+            'shopId' => intval($shopId),
+        ]);
+
+        if (empty($exists)) {
+            $saveData['mainId'] = intval($mainId);
+            $saveData['shopId'] = intval($shopId);
+            $saveData['advertisement'] = $data['advertisement'] ?? '';
+            $row = self::add($saveData);
+            return self::formatInfo($row);
+        }
+
+        // 未传 advertisement 时保留原值
+        if (array_key_exists('advertisement', $data)) {
+            $saveData['advertisement'] = is_string($data['advertisement'])
+                ? $data['advertisement']
+                : json_encode($data['advertisement'], JSON_UNESCAPED_UNICODE);
+        }
+
+        self::updateById($exists['id'], $saveData);
+        $info = self::getById($exists['id']);
+        return self::formatInfo($info);
+    }
+
+    /**
+     * 仅保存分类广告 JSON(advertisement 字段)
+     * @param int $mainId
+     * @param int $shopId
+     * @param string|array $advertisement
+     * @return array
+     */
+    public static function saveAdvertisement($mainId, $shopId, $advertisement)
+    {
+        if (is_array($advertisement)) {
+            $advertisement = json_encode($advertisement, JSON_UNESCAPED_UNICODE);
+        }
+        $advertisement = trim((string)$advertisement);
+        if ($advertisement !== '') {
+            $decoded = json_decode($advertisement, true);
+            if (!is_array($decoded)) {
+                util::fail('广告数据格式错误');
+            }
+            if (count($decoded) > 3) {
+                util::fail('最多支持3张轮播图');
+            }
+        }
+
+        $exists = self::getByCondition([
+            'mainId' => intval($mainId),
+            'shopId' => intval($shopId),
+        ]);
+
+        if (empty($exists)) {
+            $row = self::add([
+                'mainId' => intval($mainId),
+                'shopId' => intval($shopId),
+                'itemCateName' => '',
+                'isItemCateTop' => 0,
+                'isItemCateShow' => 0,
+                'advertisement' => $advertisement,
+            ]);
+            return self::formatInfo($row);
+        }
+
+        self::updateById($exists['id'], ['advertisement' => $advertisement]);
+        $info = self::getById($exists['id']);
+        return self::formatInfo($info);
+    }
+
+    /**
+     * 输出字段规范化
+     * @param array $info
+     * @return array
+     */
+    protected static function formatInfo($info)
+    {
+        return [
+            'id' => intval($info['id'] ?? 0),
+            'mainId' => intval($info['mainId'] ?? 0),
+            'shopId' => intval($info['shopId'] ?? 0),
+            'itemCateName' => $info['itemCateName'] ?? '',
+            'isItemCateTop' => intval($info['isItemCateTop'] ?? 0),
+            'isItemCateShow' => intval($info['isItemCateShow'] ?? 0),
+            'advertisement' => $info['advertisement'] ?? '',
+        ];
+    }
+}

+ 16 - 0
biz-hd/goods/models/CategorySetting.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace bizHd\goods\models;
+
+use bizHd\base\models\Base;
+
+/**
+ * 商品分类配置 xhCategorySetting
+ */
+class CategorySetting extends Base
+{
+    public static function tableName()
+    {
+        return 'xhCategorySetting';
+    }
+}