getAllList($where); $listAll = $standardListModel->getAllList([]); $data['data'] = $this->getPidList($list); $data['all'] = $listAll; return dataReturn(0, 'success', $data); } public function getAllList($id='') { $standardListModel = new StandardList(); $res = $standardListModel->getAllList([]); $list = []; foreach ($res as $k=>$v){ if($id) { $parent = trim($v['parent'],']'); $parent = trim($parent,'['); $parnet = explode(',',$parent); if(in_array($id,$parnet) || $v['id'] == $id) { $list[$k]['value'] = $v['id']; $list[$k]['label'] = $v['code'].' -- '.$v['name']; $list[$k]['pid'] = $v['pid']; $list[$k]['id'] = $v['id']; } }else{ $list[$k]['value'] = $v['id']; $list[$k]['label'] = $v['code'].' -- '.$v['name']; $list[$k]['pid'] = $v['pid']; $list[$k]['id'] = $v['id']; } } return $this->getPidList($list,$id); } private function getPidList($list,$id=0){ $packData = []; $tree = []; foreach ($list as $data) { $packData[$data['id']] = $data; } foreach ($packData as $key => $val) { if($id) { if ($val['pid'] == 0 || $val['id'] == $id) { $tree[] = &$packData[$key]; } else { $packData[$val['pid']]['children'][] = &$packData[$key]; } }else{ if ($val['pid'] == 0) { $tree[] = &$packData[$key]; } else { $packData[$val['pid']]['children'][] = &$packData[$key]; } } } return $tree; } /** * 添加类别 * @param $param */ public function store($param) { $standardListModel = new StandardList(); // $has = $standardListModel->checkUnique([ // 'name' => $param['name'] // ]); // if (!empty($has)) { // throw new ApiException('该子目名称已经存在', -1); // } $standardListModel->insertOne($param); return dataReturn(0, '添加成功'); } /** * 详情 */ public function show($id) { $standardListModel = new StandardList(); $row = $standardListModel->getInfoById($id); return dataReturn(0, 'success', $row); } /** * 编辑 * @param $param * @return array|\think\response\Json */ public function update($param) { $standardListModel = new StandardList(); // $where[] = ['name', '=', $param['name']]; // $where[] = ['id', '<>', $param['id']]; // $has = $standardListModel->checkUnique($where); // if (!empty($has)) { // throw new ApiException('该子目名称已经存在', -1); // } $standardListModel->updateById($param, $param['id']); return dataReturn(0, '编辑成功'); } public function destroy($id){ $standardListModel = new StandardList(); $info = $standardListModel->getInfoByWhere(['pid'=>$id]); if($info) { throw new ApiException('存在下级类目,不可删除', -1); } $standardListModel->delById($id); return dataReturn(0, '删除成功'); } }