Files
liche/admin/controllers/sys/Addr.php
T

281 lines
8.6 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Created by PhpStorm.
* User: xuxb
* Date: 2021/8/20
* Time: 17:46
*/
class Addr extends HD_Controller{
protected $log_dir;
function __construct(){
parent::__construct();
$this->load->model("sys/sys_addr_model", 'addr_model');
$this->load->model("area_model");
$this->log_dir = 'sys_' . get_class($this);
}
public function index(){
return $this->lists();
}
public function lists(){
$params = $this->input->get();
$page = $params['page'];
$page = !$page ? 1 : $page;
$size = $params['size'];
$size = !$size ? 20 : $size;
$where = array('province_id' => '350');
if($params['title']){
$where["title like '%{$params['title']}%'"] = null;
}
if($params['city_id']){
$where['city_id'] = $params['city_id'];
} else {
$params['city_id'] = '';
}
if($params['county_id']){
$where['county_id'] = $params['county_id'];
} else {
$params['county_id'] = '';
}
if(strlen($params['status'])>0){
$where['status'] = $params['status'];
} else {
$where['status>-1'] = null;
$params['status'] = '';
}
$total = $this->addr_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = 'id, title, city_name, county_name, status';
$rows = $this->addr_model->select($where, $orderby, $page, $size, $select);
foreach($rows as $v){
$lists[] = array(
'id' => $v['id'],
'title' => $v['title'],
'city_name' => $v['city_name'],
'county_name' => $v['county_name'],
'status' => $v['status'],
'status_name' => 1 == $v['status'] ? '开启' : '停用',
);
}
}
$this->data['params'] = $params;
$this->data['lists'] = $lists;
$this->data['pager'] = array('count'=>ceil($total/$size),'curr'=>$page,'totle'=>$total);
$this->data['_title'] = '地址管理';
$this->show_view('sys/addr/lists',true);
}
function json_lists(){
$params = $this->input->get();
$page = $params['page'];
$size = $params['size'];
$where = array('province_id' => '350');
if($params['title']){
$where["title like '%{$params['title']}%'"] = null;
}
if($params['city_id']){
$where['city_id'] = $params['city_id'];
} else {
$params['city_id'] = '';
}
if($params['county_id']){
$where['county_id'] = $params['county_id'];
} else {
$params['county_id'] = '';
}
if(strlen($params['status'])>0){
$where['status'] = $params['status'];
} else {
$where['status>-1'] = null;
$params['status'] = '';
}
$total = $this->addr_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = 'id, title';
$rows = $this->addr_model->select($where, $orderby, $page, $size, $select);
foreach($rows as $v){
$lists[] = array(
'id' => $v['id'],
'title' => $v['title'],
);
}
}
$this->data['lists'] = $lists;
$this->data['total'] = $total;
return $this->show_json(SYS_CODE_SUCCESS);
}
public function get(){
$id = $this->input->get('id');
if($id){
$row = $this->addr_model->get(array('id' => $id));
$info = array(
'id' => $row['id'],
'title' => $row['title'],
'city_id' => $row['city_id'],
'county_id' => $row['county_id'],
);
$action = '/sys/addr/edit';
$title = '编辑地址';
} else {
$info = array(
'title' => '',
'city_id' => '',
'county_id' => '',
);
$action = '/sys/addr/add';
$title = '新增地址';
}
$this->data['info'] = $info;
$this->data['action'] = $action;
$this->data['_title'] = $title;
$this->show_view('sys/addr/get');
}
public function add(){
$info = $this->input->post('info');
$title = trim($info['title']);
$city_id = $info['city_id'];
$county_id = $info['county_id'];
if(!$title){
return $this->show_json(SYS_CODE_FAIL, '请输入地址');
}
if(!$city_id){
return $this->show_json(SYS_CODE_FAIL, '请选择城市');
}
if(!$county_id){
return $this->show_json(SYS_CODE_FAIL, '请选行政区');
}
$where = array("title like '{$title}'" => null);
$count = $this->addr_model->count($where);
if($count>0){
return $this->show_json(SYS_CODE_FAIL, '地址已存在');
}
//获取行政区信息
$where = array('county_id' => $county_id);
$row_area = $this->area_model->get($where);
if(!$row_area){
return $this->show_json(SYS_CODE_FAIL, '行政区不存在');
}
$add = array(
'title' => $title,
'province_id' => $row_area['province_id'],
'province_name' => $row_area['province_name'],
'city_id' => $row_area['city_id'],
'city_name' => $row_area['city_name'],
'county_id' => $row_area['county_id'],
'county_name' => $row_area['county_name'],
'status' => 1
);
$ret = $this->addr_model->add($add);
if(!$ret){
debug_log("[error]# " . $this->addr_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function edit(){
$info = $this->input->post('info');
$title = trim($info['title']);
$city_id = $info['city_id'];
$county_id = $info['county_id'];
if(!$title){
return $this->show_json(SYS_CODE_FAIL, '请输入地址');
}
if(!$city_id){
return $this->show_json(SYS_CODE_FAIL, '请选择城市');
}
if(!$county_id){
return $this->show_json(SYS_CODE_FAIL, '请选行政区');
}
$where = array("title like '{$title}'" => null, "id<>{$info['id']}" => null);
$count = $this->addr_model->count($where);
if($count>0){
return $this->show_json(SYS_CODE_FAIL, '地址已存在');
}
//获取行政区信息
$where = array('county_id' => $county_id);
$row_area = $this->area_model->get($where);
if(!$row_area){
return $this->show_json(SYS_CODE_FAIL, '行政区不存在');
}
$upd = array(
'title' => $title,
'province_id' => $row_area['province_id'],
'province_name' => $row_area['province_name'],
'city_id' => $row_area['city_id'],
'city_name' => $row_area['city_name'],
'county_id' => $row_area['county_id'],
'county_name' => $row_area['county_name'],
);
$ret = $this->addr_model->update($upd, array('id' => $info['id']));
if(!$ret){
debug_log("[error]# " . $this->company_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
function edit_status(){
$id = $this->input->post('id');
$status = $this->input->post('status');
$upd = array('status' => $status);
$where = array('id' => $id);
$ret = $this->addr_model->update($upd, $where);
if(!$ret){
debug_log("[error]# " . $this->addr_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '保存失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
public function del(){
// TODO: Implement del() method.
}
public function batch(){
// TODO: Implement batch() method.
}
public function export(){
// TODO: Implement export() method.
}
}