load->library('session'); $this->load->_model('Model_skumaster','skumaster'); } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'add')//添加 { $this->_add(); } else if($arg == 'edit')//修改 { $this->_edit($arg_array); } else if($arg == 'del')//修改 { $this->_del(); } else { $this->_index(); } } //管理 public function _index() { $post = $this->input->post(NULL, TRUE); if(!empty($post)) { $page = $this->input->post('page',true); $perpage = $this->input->post('perpage',true); $master = $this->input->post('master',true); $where = ""; if(!empty($master)){ $where = "master = '%".$master."%'"; }else { $where = 'id > 0'; } if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } $list = $this->skumaster->find_all($where,"*","id desc",$start,$perpage); $total = $this->skumaster->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($list)); echo json_encode($rows);exit; } $this->_Template('skumaster',$this->data); } //添加 public function _add() { $post = $this->input->post(NULL, TRUE); if(isset($post['master'])) { $params = $this->input->post(NULL, TRUE); if(empty($params['master'])){ echo json_encode(array('msg'=>'请输入sku负责人!','success'=>false));exit; } $master = trim($params['master']); $num = $this->skumaster->find_count("master = '{$master}'"); if($num > 0){ echo json_encode(array('msg'=>'负责人已存在,请勿重复添加','success'=>false));exit; } $this->skumaster->insert(['master' => $master]); echo json_encode(array('msg'=>'添加成功','success'=>true));exit; } $this->_Template('skumaster_add',$this->data); } //修改 public function _edit($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $id = $this->input->post('id',true); $master = $this->input->post('master',true); $master = trim($master); $info = $this->skumaster->find_count('id != '.$id." and master = '{$master}'"); if($info > 0){ echo json_encode(array('msg'=>'负责人已存在,请勿重复添加','success'=>false));exit; } if($this->skumaster->save([ 'master' => $master ],$id)) { echo json_encode(array('msg'=>'修改成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit; } } $arg_array = $arg_array[0]; $skumaster = $this->skumaster->read($arg_array); $this->data['skumaster'] = $skumaster; $this->_Template('skumaster_edit',$this->data); } //删除 public function _del() { $post = $this->input->post(NULL, TRUE); if(isset($post['s'])) { $id_arr = $this->input->post('s'); $id_arr = explode(',',$id_arr); if(!$id_arr) { echo json_encode(array('msg'=>'参数错误!','success'=>false));exit; } //循环删除记录 foreach ($id_arr as $v) { $this->skumaster->remove($v); } echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true)); } } }