json(4001, '记录不存在'); } if ($this->request->isPost()) { $item = input('post.'); $validate = $this->validateByApi($item, [ 'sort|标题' => 'require|number', 'name|标题' => 'require|max:100', ]); if ($validate !== true) { return $validate; } Db::startTrans(); try { $info->save($item); Db::commit(); return $this->json(); } catch (ValidateException $e) { Db::rollback(); return $this->json(4001, $e->getError()); } } $disabled[] = $id; $this->data['item'] = $info; return $this->view(); } /** * 添加 * * @return Json|View * @throws Exception */ public function add() { if ($this->request->isPost()) { $item = input('post.'); $validate = $this->validateByApi($item, [ 'name|标题' => 'require|max:100', ]); if ($validate !== true) { return $validate; } try { BusinessCircleModel::create($item); return $this->json(); } catch (ValidateException $e) { return $this->json(4001, $e->getError()); } } return $this->view(); } /** * 删除 * @return Json */ public function del() { if (!$this->request->isPost()) { return $this->json(4000, '非法请求'); } $ids = $this->request->param('ids/a', []); BusinessCircleModel::destroy($ids); return $this->json(); } /** * 单个字段编辑 * * @return Json * @throws Exception */ public function modify(): Json { if ($this->request->isPost()) { $item = input('post.'); $validate = $this->validateByApi($item, [ 'field' => 'require', 'value' => 'require', ]); if ($validate !== true) { return $validate; } if (!$info = BusinessCircleModel::findById($item['id'])) { return $this->json(4001, '记录不存在'); } $update = [$item['field'] => $item['value']]; try { $info->save($update); return $this->json(); } catch (ValidateException $e) { return $this->json(4001, $e->getError()); } } return $this->json(4000, '非法请求'); } /** * 列表 * * @return Json|View * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function index() { if ($this->request->isPost()) { $menus = BusinessCircleModel::getList(); $res = [ 'code' => 0, 'msg' => 'success', 'count' => $menus->count(), 'data' => $menus->toArray(), ]; return json($res); } return $this->view(); } }