model = new GoodsModel(); } public function index($cid=0, $key="") { $map = []; if ($key) { $map[] = ['name', 'LIKE', '%'.$key.'%']; } $category_name = ''; if ($cid) { $map[] = ['category_id', '=', $cid]; $category_name = CategoryModel::where('id', $cid)->value('name'); } $limit = $this->request->cookie('limit') ? : 50; $list = $this->model->where($map)->paginate($limit); $page = $list->render(); return View::fetch('',[ 'list' => $list, 'key' => $key, 'page' => $page, 'limit' => $limit, 'cid' => $cid, 'category_name' => $category_name ]); } public function info($id) { if ($this->request->isAjax()) { $data = $this->model->find($id); return json(['code'=>2, 'data'=>$data]); } } public function save() { $categoryModel = new CategoryModel(); $categories = $categoryModel->column('name', 'id'); if ($this->request->isPost()) { $param = $this->request->param(); try { if ($param['id']) { $goods = $this->model->find(intval($param['id'])); } else { $goods = new GoodsModel(); $goods->create_time = time(); } $goods->name = $param['name']; $goods->price = intval(bcmul($param['price'], 100)); $goods->category_id = intval($param['category_id']); $goods->category_name = $categories[$param['category_id']]; $goods->type = intval($param['type']); $goods->save(); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('操作成功', 'goods/index'); } else { $id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0; if ($id) { $data = $this->model->find($id); } else { $data = [ 'id' => 0, 'name' => '', 'price' => 0.00, 'category_id' => 1, 'type' => 0, ]; } return View::fetch('', [ 'data' => $data, 'categories' => $categories ]); } } public function delete($id) { if ($this->request->isPost()){ if ($this->model->destroy($id)) { return json(['code'=>2]); } else { return json(['code'=>0]); } } } }