// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\blog\controller\member; use addon\blog\model\BlogCategory as CategoryModel; use think\facade\Request; use think\facade\View; use think\Validate; use ywxapp\controller\MemberBase; /** * 会员中心「我的分类」管理(用户自建分类) * 每个会员只能管理自己 uid 下的分类;全局共享分类(uid=0)由后台管理。 */ class Category extends MemberBase { protected $noNeedLogin = []; protected $noNeedVerify = ['*']; // 当前登录会员 id(会员主键为 uid) protected function uid() { return $this->auth->model->uid; } /** * 我的分类列表(仅当前会员自己的分类) * 以 layui treeTable 所需的扁平结构返回(带 pid / is_parent),由前端 treeTable 自动成树。 */ public function index() { $categories = CategoryModel::where('uid', $this->uid()) ->withCount('articles') ->order('sort', 'asc') ->select() ->toArray(); // 计算哪些分类拥有子分类(用于 treeTable 的 is_parent 标记) $parentIds = array_unique(array_filter(array_column($categories, 'pid'))); $treeData = []; foreach ($categories as $c) { $treeData[] = [ 'id' => $c['id'], 'pid' => $c['pid'], 'title' => $c['name'], // treeTable 节点显示名称 'name' => $c['name'], 'description' => $c['description'] ?? '', 'sort' => $c['sort'], 'status' => $c['status'], 'is_parent' => in_array($c['id'], $parentIds, true) ? 1 : 0, 'articles_count' => $c['articles_count'] ?? 0, ]; } return View::fetch('category/index', [ 'treeData' => json_encode($treeData, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG), ]); } /** * 添加分类页面 */ public function create() { $categories = CategoryModel::where(function ($query) { $query->where('uid', 0)->whereOr('uid', $this->uid()); })->order('sort', 'asc')->select(); return View::fetch('category/create', ['categories' => $categories]); } /** * 保存分类(归属当前会员) */ public function save() { $data = Request::post(); $data['uid'] = $this->uid(); $data['pid'] = (int) Request::post('pid', 0); $data['status'] = 1; $data['create_at'] = time(); $data['update_at'] = time(); $validate = new Validate([ 'name|分类名称' => 'require|unique:BlogCategory,name,0,id,uid,' . $this->uid(), 'pid|上级分类' => 'number', 'sort|排序' => 'number|between:0,999', ]); if (! $validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } CategoryModel::create($data); return json(['code' => 1, 'msg' => '分类添加成功', 'url' => '/blog/member/category']); } /** * 编辑分类页面(仅自己的分类) */ public function edit($id = null) { $category = CategoryModel::where('id', $id) ->where('uid', $this->uid()) ->find(); if (! $category) { return redirect('/blog/member/category'); } $categories = CategoryModel::where(function ($query) { $query->where('uid', 0)->whereOr('uid', $this->uid()); })->order('sort', 'asc')->select(); return View::fetch('category/edit', ['category' => $category, 'categories' => $categories]); } /** * 更新分类(仅自己的分类) */ public function update($id) { $category = CategoryModel::where('id', $id) ->where('uid', $this->uid()) ->find(); if (! $category) { return json(['code' => 0, 'msg' => '分类不存在或无权限']); } if ((int) Request::post('pid', 0) === (int) $id) { return json(['code' => 0, 'msg' => '上级分类不能选择自己']); } $data = Request::post(); $data['pid'] = (int) Request::post('pid', 0); $data['update_at'] = time(); $validate = new Validate([ 'name|分类名称' => 'require|unique:BlogCategory,name,' . $id . ',id,uid,' . $this->uid(), 'pid|上级分类' => 'number', 'sort|排序' => 'number|between:0,999', ]); if (! $validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } $category->save($data); return json(['code' => 1, 'msg' => '分类更新成功', 'url' => '/blog/member/category']); } /** * 删除分类(仅自己的分类,有文章时禁止删除) */ public function delete($id) { $category = CategoryModel::where('id', $id) ->where('uid', $this->uid()) ->find(); if (! $category) { return json(['code' => 0, 'msg' => '分类不存在或无权限']); } if ($category->children()->count() > 0) { return json(['code' => 0, 'msg' => '该分类下有子分类,请先删除子分类']); } if ($category->articles()->count() > 0) { return json(['code' => 0, 'msg' => '该分类下有文章,无法删除']); } $category->delete(); return json(['code' => 1, 'msg' => '分类已删除']); } /** * 批量操作(仅限自己的分类) */ public function batch() { $action = Request::post('action'); $ids = Request::post('ids/a'); if (empty($ids)) { return json(['code' => 0, 'msg' => '请选择分类']); } if ($action === 'delete') { $list = CategoryModel::whereIn('id', $ids) ->where('uid', $this->uid()) ->select(); foreach ($list as $c) { if ($c->articles()->count() > 0) { continue; // 跳过有文章的分类 } if (CategoryModel::where('pid', $c->id)->whereNotIn('id', $ids)->count() > 0) { continue; // 跳过有外部子分类的分类 } $c->delete(); } return json(['code' => 1, 'msg' => '批量删除成功']); } return json(['code' => 0, 'msg' => '无效操作']); } }