Kaynağa Gözat

1、批发端新增公告管理功能
2、零售端分类菜单、订单提交界面新增批发商的公告轮播展示

ouyang 3 hafta önce
ebeveyn
işleme
8df95393c9

+ 120 - 0
app-ghs/controllers/GhsNoticeController.php

@@ -0,0 +1,120 @@
+<?php
+
+namespace ghs\controllers;
+
+use bizGhs\shop\classes\GhsNoticeClass;
+use common\components\util;
+use Yii;
+
+/**
+ * 批发端通知公告
+ */
+class GhsNoticeController extends BaseController
+{
+    /**
+     * 管理端公告列表
+     */
+    public function actionList()
+    {
+        $get = Yii::$app->request->get();
+        $list = GhsNoticeClass::searchList([
+            'mainId' => $this->mainId,
+            'title' => $get['title'] ?? '',
+            'isDel' => 0,
+            'order' => 'id DESC',
+        ]);
+        util::success($list);
+    }
+
+    /**
+     * 客户端公告列表(仅显示中的公告)
+     */
+    public function actionShowList()
+    {
+        $get = Yii::$app->request->get();
+        $list = GhsNoticeClass::searchList([
+            'mainId' => $this->mainId,
+            'title' => $get['title'] ?? '',
+            'position' => $get['position'] ?? '',
+            'isDel' => 0,
+            'status' => 1,
+            'order' => 'sort DESC, id DESC',
+        ]);
+        util::success($list);
+    }
+
+    /**
+     * 新增公告
+     */
+    public function actionAdd()
+    {
+        $post = Yii::$app->request->post();
+        $post['mainId'] = $this->mainId;
+        $post['staffId'] = intval($this->shopAdminId);
+        $id = GhsNoticeClass::addNotice($post);
+        $info = GhsNoticeClass::getDetail($id);
+        util::success($info);
+    }
+
+    /**
+     * 更新公告
+     */
+    public function actionUpdate()
+    {
+        $post = Yii::$app->request->post();
+        $id = intval($post['id'] ?? 0);
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+        $info = GhsNoticeClass::getById($id);
+        GhsNoticeClass::valid($info, $this->mainId);
+        $post['staffId'] = intval($this->shopAdminId);
+        GhsNoticeClass::updateNotice($id, $post);
+        util::complete('修改成功');
+    }
+
+    /**
+     * 公告详情
+     */
+    public function actionDetail()
+    {
+        $id = intval(Yii::$app->request->get('id', 0));
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+        $info = GhsNoticeClass::getById($id);
+        GhsNoticeClass::valid($info, $this->mainId);
+        util::success(GhsNoticeClass::getDetail($id));
+    }
+
+    /**
+     * 软删除公告
+     */
+    public function actionDelete()
+    {
+        $id = intval(Yii::$app->request->post('id', Yii::$app->request->get('id', 0)));
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+        $info = GhsNoticeClass::getById($id);
+        GhsNoticeClass::valid($info, $this->mainId);
+        GhsNoticeClass::deleteNotice($id, intval($this->shopAdminId));
+        util::complete('删除成功');
+    }
+
+    /**
+     * 上下架公告
+     */
+    public function actionUpdateStatus()
+    {
+        $id = intval(Yii::$app->request->post('id', Yii::$app->request->get('id', 0)));
+        $status = intval(Yii::$app->request->post('status', Yii::$app->request->get('status', 0)));
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+        $info = GhsNoticeClass::getById($id);
+        GhsNoticeClass::valid($info, $this->mainId);
+        GhsNoticeClass::updateStatus($id, $status, intval($this->shopAdminId));
+        util::complete('操作成功');
+    }
+}

+ 61 - 1
app-hd/controllers/GhsController.php

@@ -8,6 +8,7 @@ use bizGhs\custom\classes\AccountMoneyClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\custom\classes\CustomLevelClass;
 use bizGhs\merchant\classes\WlClass;
+use bizGhs\shop\classes\GhsNoticeClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\shop\classes\ShopExtClass;
 use bizGhs\stat\classes\StatVisitClass;
@@ -23,7 +24,7 @@ use Yii;
 class GhsController extends BaseController
 {
 
-    public $guestAccess = ['info', 'get-ghs-data', 'detail'];
+    public $guestAccess = ['info', 'get-ghs-data', 'detail', 'common-info', 'ghs-notice-detail'];
 
     //提示花店有多个供货商
     public function actionRemindMoreGhs()
@@ -496,4 +497,63 @@ class GhsController extends BaseController
         util::complete();
     }
 
+    public function actionCommonInfo()
+    {
+        $mainId = $this->resolveGhsMainId();
+        // 通用接口返回全部有效公告,前端按 positions 字段筛选展示位置
+        $ghsNoticeList = GhsNoticeClass::getClientNoticeList($mainId);
+        util::success(['ghsNoticeList' => $ghsNoticeList]);
+    }
+
+    /**
+     * 批发商公告详情(零售端采购页查看)
+     */
+    public function actionGhsNoticeDetail()
+    {
+        $id = intval(Yii::$app->request->get('id', 0));
+        if ($id <= 0) {
+            util::fail('公告id不对');
+        }
+        util::success(GhsNoticeClass::getClientDetail($id));
+    }
+
+    /**
+     * 根据批发商 ghsId 或 shopId 解析 mainId(列表接口用,勿与公告 id 混用)
+     */
+    protected function resolveGhsMainId(): int
+    {
+        $get = Yii::$app->request->get();
+        $ghsId = intval($get['ghsId'] ?? 0);
+        $shopId = intval($get['shopId'] ?? 0);
+        $ghsShopId = 0;
+
+        if ($ghsId > 0) {
+            $ghs = GhsClass::getById($ghsId);
+            if (empty($ghs)) {
+                util::fail('没有找到批发商');
+            }
+            if (!empty($this->shopId)) {
+                GhsClass::valid($ghs, $this->shopId);
+            }
+            $ghsShopId = intval($ghs['shopId'] ?? 0);
+        } elseif ($shopId > 0) {
+            $ghsShop = ShopClass::getById($shopId, true);
+            if (empty($ghsShop) || $ghsShop->ptStyle != dict::getDict('ptStyle', 'ghs')) {
+                util::fail('没有找到批发店');
+            }
+            $ghsShopId = $shopId;
+        } else {
+            util::fail('参数不对');
+        }
+
+        $ghsShop = ShopClass::getById($ghsShopId, true);
+        if (empty($ghsShop)) {
+            util::fail('没有找到批发商');
+        }
+        $mainId = intval($ghsShop->mainId ?? 0);
+        if ($mainId <= 0) {
+            util::fail('批发商信息不完整');
+        }
+        return $mainId;
+    }
 }

+ 288 - 0
biz-ghs/shop/classes/GhsNoticeClass.php

@@ -0,0 +1,288 @@
+<?php
+
+namespace bizGhs\shop\classes;
+
+use bizGhs\base\classes\BaseClass;
+use common\components\util;
+use Yii;
+use yii\db\Expression;
+
+/**
+ * 批发端通知公告业务类
+ */
+class GhsNoticeClass extends BaseClass
+{
+    public static $baseFile = '\bizGhs\shop\models\GhsNotice';
+
+    /** 展示位置:分类菜单 */
+    const POSITION_CATEGORY = 1;
+    /** 展示位置:订单提交 */
+    const POSITION_ORDER = 2;
+
+    /**
+     * 展示位置文案映射
+     */
+    public static function getPositionMap(): array
+    {
+        return [
+            self::POSITION_CATEGORY => '分类菜单',
+            self::POSITION_ORDER => '订单提交',
+        ];
+    }
+
+    /**
+     * 客户端公告列表(不分页)
+     */
+    public static function getClientNoticeList(int $mainId, int $position = 0): array
+    {
+        if ($mainId <= 0) {
+            return [];
+        }
+        $query = self::getModel()::find()
+            ->where(['mainId' => $mainId, 'isDel' => 0, 'status' => 1]);
+        if ($position > 0) {
+            $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
+        }
+        $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
+        return self::groupBaseInfo($list);
+    }
+
+    /**
+     * 客户端公告详情(仅校验公告本身有效)
+     */
+    public static function getClientDetail(int $id): array
+    {
+        $info = self::getById($id);
+        if (empty($info) || intval($info['isDel']) === 1 || intval($info['status']) !== 1) {
+            util::fail('公告不存在');
+        }
+        return self::getDetail($id);
+    }
+
+    /**
+     * 通用列表查询,供管理端与客户端复用
+     */
+    public static function searchList(array $params): array
+    {
+        $get = Yii::$app->request->get();
+        $page = isset($get['page']) ? max(1, intval($get['page'])) : 1;
+        $pageSize = !empty($get['pageSize']) ? intval($get['pageSize']) : Yii::$app->params['pageSize'];
+
+        $query = self::getModel()::find()->where(['mainId' => intval($params['mainId'])]);
+
+        if (isset($params['isDel'])) {
+            $query->andWhere(['isDel' => intval($params['isDel'])]);
+        }
+        if (isset($params['status'])) {
+            $query->andWhere(['status' => intval($params['status'])]);
+        }
+        if (!empty($params['title'])) {
+            $query->andWhere(['like', 'title', trim($params['title'])]);
+        }
+        if (!empty($params['position'])) {
+            $position = intval($params['position']);
+            $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
+        }
+
+        $order = !empty($params['order']) ? $params['order'] : 'id DESC';
+        $totalNum = (int)$query->count();
+        $totalPage = $pageSize > 0 ? (int)ceil($totalNum / $pageSize) : 0;
+        $list = $query->orderBy($order)
+            ->offset(($page - 1) * $pageSize)
+            ->limit($pageSize)
+            ->asArray()
+            ->all();
+
+        return [
+            'totalNum' => $totalNum,
+            'totalPage' => $totalPage,
+            'moreData' => $page < $totalPage ? 1 : 0,
+            'list' => self::groupBaseInfo($list),
+        ];
+    }
+
+    /**
+     * 组装列表展示字段
+     */
+    public static function groupBaseInfo(array $list): array
+    {
+        $positionMap = self::getPositionMap();
+        foreach ($list as $key => $item) {
+            $positions = self::formatPositionToArr($item['position'] ?? '');
+            $labels = [];
+            foreach ($positions as $pos) {
+                if (isset($positionMap[$pos])) {
+                    $labels[] = $positionMap[$pos];
+                }
+            }
+            $list[$key]['positions'] = $positions;
+            $list[$key]['positionLabels'] = $labels;
+        }
+        return $list;
+    }
+
+    /**
+     * 多选位置转逗号分隔字符串
+     */
+    public static function formatPositionToStr($positions): string
+    {
+        if (is_string($positions)) {
+            $positions = array_filter(array_map('trim', explode(',', $positions)));
+        }
+        if (!is_array($positions)) {
+            return '';
+        }
+        $valid = array_keys(self::getPositionMap());
+        $positions = array_values(array_unique(array_map('intval', $positions)));
+        $positions = array_values(array_intersect($positions, $valid));
+        sort($positions);
+        return implode(',', $positions);
+    }
+
+    /**
+     * 逗号分隔字符串转位置数组
+     */
+    public static function formatPositionToArr(string $position): array
+    {
+        if ($position === '') {
+            return [];
+        }
+        $positions = array_filter(array_map('trim', explode(',', $position)));
+        return array_values(array_map('intval', $positions));
+    }
+
+    /**
+     * 校验公告表单数据
+     */
+    public static function validateNoticeData(array $data): array
+    {
+        $title = trim($data['title'] ?? '');
+        $summary = trim($data['summary'] ?? '');
+        if ($title === '') {
+            util::fail('请输入公告标题');
+        }
+        if (mb_strlen($title) > 20) {
+            util::fail('标题不能超过20个字');
+        }
+        if ($summary === '') {
+            util::fail('请输入公告简介');
+        }
+        if (mb_strlen($summary) > 70) {
+            util::fail('公告简介不能超过70个字');
+        }
+
+        $position = self::formatPositionToStr($data['position'] ?? '');
+        if ($position === '') {
+            util::fail('请选择展示位置');
+        }
+
+        $content = $data['content'] ?? '';
+        if ($content === '' || $content === '[]') {
+            util::fail('请添加公告内容');
+        }
+        if (is_string($content)) {
+            $contentArr = json_decode($content, true);
+            if (!is_array($contentArr) || empty($contentArr)) {
+                util::fail('公告内容格式不正确');
+            }
+        }
+
+        return [
+            'title' => $title,
+            'summary' => $summary,
+            'content' => is_string($content) ? $content : json_encode($content, JSON_UNESCAPED_UNICODE),
+            'position' => $position,
+            'sort' => intval($data['sort'] ?? 0),
+            'status' => intval($data['status'] ?? 0) === 1 ? 1 : 0,
+        ];
+    }
+
+    /**
+     * 新增公告
+     */
+    public static function addNotice(array $data): int
+    {
+        $noticeData = self::validateNoticeData($data);
+        if (!empty($data['mainId'])) {
+            $noticeData['mainId'] = intval($data['mainId']);
+        }
+        if (!empty($data['staffId'])) {
+            $noticeData['staffId'] = intval($data['staffId']);
+        }
+        if ($noticeData['status'] === 1) {
+            $noticeData['publishTime'] = date('Y-m-d H:i:s');
+        }
+        $result = self::add($noticeData);
+        return is_array($result) ? intval($result['id'] ?? 0) : intval($result);
+    }
+
+    /**
+     * 更新公告
+     */
+    public static function updateNotice(int $id, array $data): void
+    {
+        $noticeData = self::validateNoticeData($data);
+        $info = self::getById($id);
+        if (empty($info)) {
+            util::fail('公告不存在');
+        }
+        if (!empty($data['staffId'])) {
+            $noticeData['staffId'] = intval($data['staffId']);
+        }
+        self::updateById($id, $noticeData);
+    }
+
+    /**
+     * 公告详情
+     */
+    public static function getDetail(int $id): array
+    {
+        $info = self::getById($id);
+        if (empty($info)) {
+            util::fail('公告不存在');
+        }
+        $list = self::groupBaseInfo([$info]);
+        return current($list);
+    }
+
+    /**
+     * 软删除公告
+     */
+    public static function deleteNotice(int $id, int $staffId = 0): void
+    {
+        $updateData = ['isDel' => 1];
+        if ($staffId > 0) {
+            $updateData['staffId'] = $staffId;
+        }
+        self::updateById($id, $updateData);
+    }
+
+    /**
+     * 上下架公告
+     */
+    public static function updateStatus(int $id, int $status, int $staffId = 0): void
+    {
+        $status = $status === 1 ? 1 : 0;
+        $updateData = ['status' => $status];
+        if ($status === 1) {
+            $updateData['publishTime'] = date('Y-m-d H:i:s');
+        }
+        if ($staffId > 0) {
+            $updateData['staffId'] = $staffId;
+        }
+        self::updateById($id, $updateData);
+    }
+
+    /**
+     * 校验公告归属权限
+     */
+    public static function valid(array $info, int $mainId): void
+    {
+        if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
+            util::fail('没有权限操作该公告');
+        }
+        if (intval($info['isDel']) === 1) {
+            util::fail('公告已删除');
+        }
+    }
+}

+ 16 - 0
biz-ghs/shop/models/GhsNotice.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace bizGhs\shop\models;
+
+use bizGhs\base\models\Base;
+
+/**
+ * 批发端通知公告
+ */
+class GhsNotice extends Base
+{
+    public static function tableName()
+    {
+        return 'xhGhsNotice';
+    }
+}