logic = (new PostCategoryLogic()); } /** * 获取验证器 */ public function getValidator() { return new $this->validator; } /** * 获取系统list */ public function preset($params) { $where = []; if(isset($params['parent_id']) && $params['parent_id']) { $where['parent_id'] = $params['parent_id']; }else{ $where['parent_id'] = 0; } $result = $this->logic->getList($where)->toArray(); return $result; } /** * 获取系统list */ public function getList($params) { $where = []; if(isset($params['parent_id']) && $params['parent_id']) { $where['parent_id'] = $params['parent_id']; }else{ $where['parent_id'] = 0; } $result = $this->logic->getList($where)->toArray(); foreach ($result as $key => $value) { $where = []; $where['parent_id'] = $value['id']; $result[$key]['children'] = $this->logic->getList($where)->toArray(); foreach ( $result[$key]['children'] as $k => $v) { $where = []; $where['parent_id'] = $v['id']; $result[$key]['children'][$k]['children'] = $this->logic->getList($where)->toArray(); } } return $result; } /** * 添加前置处理 */ public function beforeServiceStore($data, Request $request) { $brand_id = $data['parent_id'] ?? 0; if($brand_id == 0) { $data['level'] = 1; $data['path'] = ''; return $data; } $parent = PostCategory::where('id', $data['parent_id'])->first(); $parent['path'] = !empty($parent['path'])?:'/'; $data['level'] = $parent['level']+1; $data['path'] = $parent['path'].$parent['id'].'/'; if($data['level'] > 3) { throw new \Exception("分类层级不能超过三层"); } return $data; } }