load->library('session'); $this->load->_model('Model_customs','customs'); $this->load->_model('Model_country','country'); $this->load->_model('Model_express','express'); $this->load->_model('Model_detailed','detailed'); } //定义方法的调用规则 获取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 if($arg == 'detailed')//修改 { $this->_detailed(); } else { $this->_index(); } } //管理 public function _index() { $post = $this->input->post(NULL, TRUE); if(isset($post['page'])) { $page = $this->input->post('page',true); $perpage = $this->input->post('perpage',true); $country = $this->input->post('country',true); $express = $this->input->post('express',true); $where = "1=1 "; if($country) { $where .= " and country = '$country'"; } if($express) { $where .= " and express = '$express'"; } //数据排序 $order_str = "id asc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } //取得信息列表 $info_list = $this->customs->find_all($where,'id,express,country',$order_str,$start,$perpage); foreach ($info_list as $key=>$value) { $country = $this->country->read($value['country']); $info_list[$key]['country'] = $country['name']; $express = $this->express->read($value['express']); $info_list[$key]['express'] = $express['title']; } $total = $this->customs->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list)); echo json_encode($rows);exit; } $country = $this->country->find_all(); $this->data['country'] = $country; $express = $this->express->find_all(); $this->data['express'] = $express; $this->_Template('customs',$this->data); } //添加 public function _add() { $post = $this->input->post(NULL, TRUE); if(isset($post['express'])) { $post['express'] = $this->input->post('express',true); $detailedrows = $this->input->post('detailed',true); //添加detailed信息开始 if($detailedrows != NULL) { $dnum = array(); $detaileddata = explode('|',rtrim($detailedrows,'|')); foreach ($detaileddata as $k=>$v) { $rows = explode(',',rtrim($v,',')); $detailedpost['lowerlimit'] = $rows[0]; $detailedpost['includelower'] = $rows[1]; $detailedpost['upperlimit'] = $rows[2]; $detailedpost['includeupper'] = $rows[3]; $detailedpost['calculatemethod'] = $rows[4]; $detailedpost['customsval'] = $rows[5]; $detailedpost['time'] = time().rand(1000,9999); if($this->detailed->insert($detailedpost)) { $dnum[] = $detailedpost['time']; } } $post['detailed'] = "|"; foreach ($dnum as $k=>$v) { $ddata = $this->detailed->get_time($v); $post['detailed'] = $post['detailed'].$ddata['id']."|"; } } //添加detailed信息结束 $countryck = $this->input->post('country',true); if($countryck) { $countryck = explode(',',rtrim($countryck,',')); foreach ($countryck as $key=>$value) { $post['country'] = $value; $this->customs->insert($post); } echo json_encode(array('msg'=>'添加成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit; } } $country = $this->country->find_all(); $this->data['country'] = $country; $express = $this->express->find_all(); $this->data['express'] = $express; $this->_Template('customs_add',$this->data); } //修改 public function _edit($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $id = $this->input->post('id',true); $post['express'] = $this->input->post('express',true); $post['country'] = $this->input->post('country',true); $detailedrows = $this->input->post('detailed',true); $customs = $this->customs->read($id); $detaileddata=""; if($detailedrows != NULL) { $dnum = array(); $rw = explode('|',rtrim($detailedrows,'|')); foreach ($rw as $k=>$v) { $rows = explode(',',rtrim($v,',')); $detailedpost['lowerlimit'] = $rows[0]; $detailedpost['includelower'] = $rows[1]; $detailedpost['upperlimit'] = $rows[2]; $detailedpost['includeupper'] = $rows[3]; $detailedpost['calculatemethod'] = $rows[4]; $detailedpost['customsval'] = $rows[5]; $detailedpost['time'] = time().rand(1000,9999); if($this->detailed->insert($detailedpost)) { $dnum[] = $detailedpost['time']; } } foreach ($dnum as $k=>$v) { $ddata = $this->detailed->get_time($v); $detaileddata = $detaileddata.$ddata['id']."|"; } } if($customs['detailed'] == "") { $customs['detailed'] = "|"; } $post['detailed'] = $customs['detailed'].$detaileddata; if($this->customs->save($post,$id)) { echo json_encode(array('msg'=>'修改成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit; } } $arg_array = $arg_array[0]; $customs = $this->customs->read($arg_array); $this->data['customs'] = $customs; $country = $this->country->find_all(); $this->data['country'] = $country; $express = $this->express->find_all(); $this->data['express'] = $express; $rows = array(); if($customs['detailed']) { $detailed = $customs['detailed']; $detailed = explode('|',trim($detailed,'|')); foreach ($detailed as $k=>$v) { $rw = $this->detailed->read($v); if($rw['calculatemethod'] == 1) { $wr = array('title'=>"按总价"); } else { $wr = array('title'=>"按单价"); } $rw = array_merge($rw,$wr); $rows[] = $rw; } } $this->data['rows'] = $rows; $this->_Template('customs_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->customs->remove($v); } echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true)); } } //修改数据 public function _detailed() { $post = $this->input->post(NULL, TRUE); if(isset($post['s'])) { $s = $this->input->post('s'); $id= $this->input->post('id'); $customs = $this->customs->read($id); $detailed = str_replace('|'.$s.'|','|',$customs['detailed']); if($detailed == "|") { $detailed = ""; } if($this->customs->save(array('detailed'=>$detailed),$id))//写入删除后的数据 { echo json_encode(array('success'=>true));exit; } else { echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit; } } } }