diff --git a/.gitignore b/.gitignore index 22f2627..431af65 100755 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ dge.gynhc.com_*.tar.gz # 小程序端源码(uniapp/)内的本地凭据文件,同样切勿入库 /uniapp/AGENTS.md +tools/ diff --git a/application/admin/controller/fc/Category.php b/application/admin/controller/fc/Category.php index 043cf68..0eb34e8 100644 --- a/application/admin/controller/fc/Category.php +++ b/application/admin/controller/fc/Category.php @@ -106,6 +106,8 @@ class Category extends Backend return json(['total' => $total, 'rows' => array_slice($ordered, $offset, $limit)]); } + $homeCount = Db::name('fc_category')->where('pid', '>', 0)->where('is_home', 1)->count(); + $this->view->assign('homeCount', $homeCount); return $this->view->fetch(); } @@ -114,6 +116,64 @@ class Category extends Backend * * @return string */ + + /** + * 校验首页金刚区推荐上限(最多8个,且仅允许二级分类) + * + * @param array $idArr 涉及的分类ID数组 + * @param int $isHome 设定的推荐值(1开启,0关闭) + * @return void + */ + protected function checkHomeNavLimit(array $idArr, $isHome) + { + if ((int)$isHome !== 1 || empty($idArr)) { + return; + } + + // 1. 检查是否存在一级大类 + $topCategories = Db::name('fc_category') + ->where('id', 'in', $idArr) + ->where('pid', 0) + ->column('name'); + if (!empty($topCategories)) { + $topNames = implode('、', $topCategories); + $this->error("首页金刚区专用于二级品类推荐,一级大类【{$topNames}】不可推荐至金刚区"); + } + + // 2. 统计当前已推荐的二级分类数量(排除当前正在编辑的记录) + $currentCount = Db::name('fc_category') + ->where('pid', '>', 0) + ->where('is_home', 1) + ->whereNotIn('id', $idArr) + ->count(); + + $newTotal = $currentCount + count($idArr); + if ($newTotal > 8) { + $this->error("首页金刚区按钮上限仅为 8 个!当前已有 {$currentCount} 个已推荐,本次操作将达到 {$newTotal} 个,请先关闭其他已推荐分类。"); + } + } + + /** + * 批量更新与单项开关切换(严格控制首页金刚区最多 8 个) + * + * @param string $ids 待操作的记录ID串 + * @return void + */ + public function multi($ids = "") + { + $ids = $ids ? $ids : $this->request->param("ids"); + if ($ids) { + if ($this->request->has('params')) { + parse_str($this->request->post("params"), $values); + if (isset($values['is_home'])) { + $idArr = array_filter(explode(',', (string)$ids)); + $this->checkHomeNavLimit($idArr, $values['is_home']); + } + } + } + parent::multi($ids); + } + public function add() { if ($this->request->isPost()) { @@ -143,6 +203,9 @@ class Category extends Backend if ($this->request->isPost()) { $params = $this->request->post('row/a', []); $params = $this->normalizeCategoryParams($params, (int)$ids); + if (isset($params['is_home'])) { + $this->checkHomeNavLimit([(int)$ids], $params['is_home']); + } $this->request->post(['row' => $params]); return parent::edit($ids); } diff --git a/application/admin/view/fc/category/add.html b/application/admin/view/fc/category/add.html index 1293630..3688201 100644 --- a/application/admin/view/fc/category/add.html +++ b/application/admin/view/fc/category/add.html @@ -618,6 +618,16 @@ +