Customer = model('Customer'); } public function index(){ $this->assign('meta_title','客户列表'); return $this->fetch(); } public function load(){ $where = []; $page = input('get.page'); $limit = input('get.limit'); $list = $this->Customer->where($where)->paginate($limit,false,['page'=>$page]); $data = []; foreach ($list as $key => $value) { $data[$key]['id'] = $value['id']; $data[$key]['cname'] = $value['cname']; $data[$key]['truename'] = $value['truename']; $data[$key]['mobile'] = $value['mobile']; $data[$key]['address'] = $value['address']; $data[$key]['state'] = $value['state']; $data[$key]['create_time'] = $value['create_time']; } $this->output(0,'获取成功',$data,$list->total()); } public function add(){ if ($this->request->isPost()) { $cname = input('post.cname'); if (empty($cname)) { $this->output(1,'名称不能为空'); } $this->Customer->cname = $cname; $this->Customer->truename = input('post.truename'); $this->Customer->mobile = input('post.mobile'); $this->Customer->address = input('post.address'); $result = $this->Customer->save(); if (!$result) { $this->output(1,'保存失败'); } $this->output(0,'保存成功'); }else{ $this->assign('meta_title','添加客户'); return $this->fetch(); } } public function edit(){ if ($this->request->isPost()) { $id = input('post.id'); $customer = $this->Customer->where(['id'=>$id])->find(); if (!$customer) { $this->output(1,'参数错误'); } $cname = input('post.cname'); if (empty($cname)) { $this->output(1,'名称不能为空'); } $customer->cname = $cname; $customer->truename = input('post.truename'); $customer->mobile = input('post.mobile'); $customer->address = input('post.address'); $result = $customer->save(); if (!$result) { $this->output(1,'保存失败'); } $this->output(0,'保存成功'); }else{ $id = input('get.id'); $customer = $this->Customer->where(['id'=>$id])->find(); if (!$customer) { $this->error('参数错误'); } $this->assign('customer',$customer); $this->assign('meta_title','添加客户'); return $this->fetch(); } } public function delete(){ if ($this->request->isPost()) { $id = input('post.id'); $customer = $this->Customer->where(['id'=>$id])->find(); if (!$customer) { $this->output(1,'参数错误'); } $result = $customer->delete(); if (!$result) { $this->output(1,'删除失败'); } $this->output(0,'删除成功'); } } }