<?php
|
|
namespace app\admin\controller;
|
|
use think\facade\Cache;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
|
class CountryCode extends Common {
|
private $viewPath = 'country_code';
|
/**
|
* 菜单列表
|
*/
|
public function index() {
|
////////
|
if (!Request::isPost()) {
|
$guide['one']['text'] = '基础信息设置';
|
$guide['two']['text'] = '国家城市管理';
|
View::assign('guide', $guide);
|
View::assign('viewPath', $this->viewPath);
|
return View::fetch('index');
|
}
|
}
|
public function getCountryCodeList() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
try {
|
$father_id = input('father_id');
|
$where[] = ['father_id','=',$father_id];
|
$order = 'order_id asc,id asc';
|
$list = Db::name('country_code')->field('id,cate_name,cate_code,order_id,status,father_id')->where($where)->order($order)->select()->toArray();
|
if(!$list){
|
return $this->errorResponse('暂无菜单');
|
}
|
$reuslt['list'] = $list;
|
return $this->successResponse($reuslt);
|
} catch (\Exception $e) {
|
return $this->errorResponse($e->getMessage());
|
}
|
}
|
/**
|
* 更新显示状态
|
*/
|
public function updateShow() {
|
header('Content-Type:text/html;charset=utf-8');
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
if (!is_numeric($id)) {
|
return $this->errorResponse('参数错误');
|
}
|
$status = input('status');
|
$info = Db::name('country_code')->field('id')->where('id', $id)->find();
|
if (empty($info)) {
|
return $this->errorResponse('信息不存在');
|
}
|
if ($status == '1') {
|
$data['status'] = 1;
|
} else {
|
$data['status'] = 0;
|
}
|
$backval = Db::name('country_code')->where('id', $id)->save($data);
|
if ($backval === FALSE) {
|
return $this->errorResponse('更新失败');
|
} else {
|
$this->delCache();
|
return $this->successResponse('更新成功');
|
}
|
}
|
|
/**
|
* 保存国家信息
|
* @return array|int[]
|
*/
|
public function saveCountryCode() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
$data = Request::post();
|
$id = $data['id'];
|
if (is_numeric($id)) {
|
//更新数据
|
$validate = new \app\admin\validate\CountryCode();
|
$result = $validate->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////保存////////////
|
Db::name('country_code')->where('id', $id)->withoutField('id')->save($data);
|
////////////////////////
|
$this->delCache();
|
$res['message'] = '编辑成功';
|
return $this->successResponse($res);
|
} else {
|
unset($data['id']);
|
$data['status'] = 1;
|
$validate = new \app\admin\validate\CountryCode();
|
$result = $validate->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////////////////////
|
Db::startTrans();
|
try {
|
$new_id = Db::name('country_code')->insertGetId($data);
|
if ($new_id) {
|
Db::name('country_code')->where('id', $new_id)->save(['order_id' => $new_id]);
|
///////执行成功,提交事务///////
|
Db::commit();
|
$this->delCache();
|
$res['message'] = '添加成功';
|
return $this->successResponse($res);
|
} else {
|
//任一执行失败,执行回滚操作
|
Db::rollback();
|
return $this->errorResponse('添加失败');
|
}
|
} catch (\Exception $e) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse('添加失败');
|
}
|
}
|
}
|
/*
|
* 保存城市信息
|
*/
|
public function saveCityCode() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
$data = Request::post();
|
$id = $data['id'];
|
if (is_numeric($id)) {
|
//更新数据
|
$validate = new \app\admin\validate\CountryCode();
|
$result = $validate->scene('city')->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////保存////////////
|
Db::name('country_code')->where('id', $id)->withoutField('id')->save($data);
|
////////////////////////
|
$this->delCache();
|
$res['message'] = '编辑成功';
|
return $this->successResponse($res);
|
} else {
|
unset($data['id']);
|
$data['status'] = 1;
|
$validate = new \app\admin\validate\CountryCode();
|
$result = $validate->scene('city')->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////////////////////
|
Db::startTrans();
|
try {
|
$new_id = Db::name('country_code')->insertGetId($data);
|
if ($new_id) {
|
Db::name('country_code')->where('id', $new_id)->save(['order_id' => $new_id]);
|
///////执行成功,提交事务///////
|
Db::commit();
|
$this->delCache();
|
$res['message'] = '添加成功';
|
return $this->successResponse($res);
|
} else {
|
//任一执行失败,执行回滚操作
|
Db::rollback();
|
return $this->errorResponse('添加失败');
|
}
|
} catch (\Exception $e) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse('添加失败');
|
}
|
}
|
}
|
/**
|
* 清除缓存
|
* @return void
|
*/
|
private function delCache() {
|
Cache::tag('country_code')->clear();
|
}
|
|
}
|