model = new MallGoods(); $this->assign('cate', (new MallCate())->column('title', 'id')); } #[NodeAnnotation(title: '列表', auth: true)] public function index(Request $request): Json|string { if ($request->isAjax()) { if (input('selectFields')) return $this->selectList(); list($page, $limit, $where) = $this->buildTableParams(); $count = $this->model->where($where)->count(); $list = $this->model->with(['cate'])->where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list, ]; return json($data); } return $this->fetch(); } #[NodeAnnotation(title: '入库', auth: true)] public function stock(Request $request, $id): string { $row = $this->model->find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); $rule = []; $this->validate($post, $rule); try { $post['total_stock'] = $row->total_stock + $post['stock']; $post['stock'] = $row->stock + $post['stock']; $save = $row->save($post); }catch (\Exception $e) { $this->error('保存失败'); } $save ? $this->success('保存成功') : $this->error('保存失败'); } $this->assign('row', $row); return $this->fetch(); } #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOGIN)] public function no_check_login(Request $request): string { return '这里演示方法不需要经过登录验证'; } }