diff --git a/app/data/controller/shop/Cate.php b/app/data/controller/shop/Cate.php index 42f6b2745..a45741599 100644 --- a/app/data/controller/shop/Cate.php +++ b/app/data/controller/shop/Cate.php @@ -5,6 +5,7 @@ namespace app\data\controller\shop; use app\data\model\ShopGoodsCate; use think\admin\Controller; use think\admin\extend\DataExtend; +use think\admin\helper\QueryHelper; /** * 商品分类管理 @@ -13,6 +14,12 @@ use think\admin\extend\DataExtend; */ class Cate extends Controller { + /** + * 最大级别 + * @var integer + */ + protected $maxLevel = 5; + /** * 商品分类管理 * @auth true @@ -23,9 +30,12 @@ class Cate extends Controller */ public function index() { - $this->title = "商品分类管理"; - $query = ShopGoodsCate::mQuery()->like('name')->dateBetween('create_at'); - $query->equal('status')->where(['deleted' => 0])->order('sort desc,id desc')->page(false); + ShopGoodsCate::mQuery()->layTable(function () { + $this->title = "商品分类管理"; + }, function (QueryHelper $query) { + $query->where(['deleted' => 0]); + $query->like('name')->equal('status')->dateBetween('create_at'); + }); } /** @@ -66,12 +76,9 @@ class Cate extends Controller { if ($this->request->isGet()) { $data['pid'] = intval($data['pid'] ?? input('pid', '0')); - $cates = ShopGoodsCate::mk()->where(['deleted' => 0])->order('sort desc,id desc')->select()->toArray(); - $this->cates = DataExtend::arr2table(array_merge($cates, [['id' => '0', 'pid' => '-1', 'name' => '顶部分类']])); - if (isset($data['id'])) foreach ($this->cates as $cate) if ($cate['id'] === $data['id']) $data = $cate; - foreach ($this->cates as $key => $cate) if ((isset($data['spt']) && $data['spt'] <= $cate['spt'])) { - unset($this->cates[$key]); - } + $this->cates = ShopGoodsCate::getParentData($this->maxLevel, $data, [ + 'id' => '0', 'pid' => '-1', 'name' => '顶部分类', + ]); } } diff --git a/app/data/model/ShopGoodsCate.php b/app/data/model/ShopGoodsCate.php index fab25fa97..ecfc42251 100644 --- a/app/data/model/ShopGoodsCate.php +++ b/app/data/model/ShopGoodsCate.php @@ -2,6 +2,7 @@ namespace app\data\model; +use think\admin\extend\DataExtend; use think\admin\Model; /** @@ -11,5 +12,54 @@ use think\admin\Model; */ class ShopGoodsCate extends Model { + /** + * 获取上级可用选项 + * @param int $max 最大级别 + * @param array $data 表单数据 + * @param array $parent 上级分类 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function getParentData(int $max, array &$data, array $parent = []): array + { + $items = static::mk()->order('sort desc,id asc')->select()->toArray(); + $cates = DataExtend::arr2table(empty($parent) ? $items : array_merge([$parent], $items)); + if (isset($data['id'])) foreach ($cates as $cate) if ($cate['id'] === $data['id']) $data = $cate; + foreach ($cates as $key => $cate) { + $isSelf = isset($data['spt']) && isset($data['spc']) && $data['spt'] <= $cate['spt'] && $data['spc'] > 0; + if ($cate['spt'] >= $max || $isSelf) unset($cates[$key]); + } + return $cates; + } + /** + * 获取列表数据 + * @param bool $simple 仅子级别 + * @return array + */ + public static function getLevelData(bool $simple = false): array + { + $query = static::mk()->where(['status' => 1])->order('sort desc,id asc'); + $cates = array_column(DataExtend::arr2table($query->column('id,pid,name', 'id')), null, 'id'); + foreach ($cates as $cate) isset($cates[$cate['pid']]) && $cates[$cate['id']]['parent'] =& $cates[$cate['pid']]; + foreach ($cates as $key => $cate) { + $id = $cate['id']; + $cates[$id]['ids'][] = $cate['id']; + $cates[$id]['names'][] = $cate['name']; + while (isset($cate['parent']) && ($cate = $cate['parent'])) { + $cates[$id]['ids'][] = $cate['id']; + $cates[$id]['names'][] = $cate['name']; + } + $cates[$id]['ids'] = array_reverse($cates[$id]['ids']); + $cates[$id]['names'] = array_reverse($cates[$id]['names']); + if (isset($pky) && $simple && in_array($cates[$pky]['name'], $cates[$id]['names'])) { + unset($cates[$pky]); + } + $pky = $key; + } + foreach ($cates as &$cate) unset($cate['parent']); + return array_values($cates); + } } \ No newline at end of file diff --git a/app/data/view/shop/cate/form.html b/app/data/view/shop/cate/form.html index b774ede10..4ee787002 100644 --- a/app/data/view/shop/cate/form.html +++ b/app/data/view/shop/cate/form.html @@ -1,9 +1,9 @@ -
+
- +
+ +
- 商品分类图标 - Category Icon + 产品分类图标Category Cover - 可选,请上传商品分类图标,需要是 http 可访问的图片资源链接 + 可选,请上传产品分类图标,需要是 http 可访问的图片资源链接
+ +
{notempty name='vo.id'}{/notempty} +
- +
+
\ No newline at end of file diff --git a/app/data/view/shop/cate/index.html b/app/data/view/shop/cate/index.html index 2cb84af51..28c4be633 100644 --- a/app/data/view/shop/cate/index.html +++ b/app/data/view/shop/cate/index.html @@ -1,4 +1,4 @@ -{extend name="../../admin/view/main"} +{extend name="../../admin/view/table"} {block name="button"} @@ -6,68 +6,86 @@ - + {/block} {block name="content"} -
- {notempty name='list'} - - - - - - - - - - - {foreach $list as $key=>$vo} - - - - - - - - - {/foreach} - -
- - - -
- - - - {$vo.spl|raw}{$vo.name}{$vo.remark|default=''}{eq name='vo.status' value='0'}已禁用{else}已激活{/eq} - - - 添 加 - - - - 编 辑 - - - - 激 活 - - - - 禁 用 - - - - 删 除 - - -
- {$pagehtml|raw|default=''} - {else} - 没有记录哦 - {/notempty} +
+ 注意:产品分类需要在上传产品前添加,当产品分类关联有产品时不建议进行 移动删除 操作!
+ +
+
+
+ + + + + + + + + + + {/block}