123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- /**
- * 地区设置
- */
- namespace app\admin\controller;
- use think\facade\View;
- use think\facade\Db;
- use think\facade\Lang;
- /**
- * ============================================================================
- *
- * ============================================================================
- * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
- * 网站地址: https://www.valimart.net/
- * ----------------------------------------------------------------------------
- *
- * ============================================================================
- * 控制器
- */
- class Region extends AdminControl {
- public function initialize() {
- parent::initialize();
- Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/region.lang.php');
- $this->_area_model = model('Area');
- define('MAX_LAYER', 3);
- }
- public function index() {
- $region_list = $this->_area_model->getAreaChild(0);
- /* 先根排序 */
- foreach ($region_list as $key => $val) {
- $region_list[$key]['switchs'] = 0;
- if ($this->_area_model->getAreaChild($val['area_id'])) {
- $region_list[$key]['switchs'] = 1;
- }
- }
- View::assign('region_list', $region_list);
- $this->setAdminCurItem('index');
- return View::fetch();
- }
- function ajax_cate() {
- $cate_id = input('param.id');
- if (empty($cate_id)) {
- return;
- }
- $cate = $this->_area_model->getAreaChild($cate_id);
- foreach ($cate as $key => $val) {
- $child = $this->_area_model->getAreaChild($val['area_id']);
- if ($val['area_deep'] >= MAX_LAYER) {
- $cate[$key]['add_child'] = 0;
- } else {
- $cate[$key]['add_child'] = 1;
- }
- if (!$child || empty($child)) {
- $cate[$key]['switchs'] = 0;
- } else {
- $cate[$key]['switchs'] = 1;
- }
- }
- echo json_encode(array_values($cate));
- return;
- }
- /**
- * ajax操作
- */
- public function ajax() {
- $condition = array();
- switch (input('param.branch')) {
- /**
- * 更新地区
- */
- case 'area_name':
- $area_model = model('area');
- $condition[] = array('area_id','=',intval(input('get.id')));
- $update_array = array();
- $update_array['area_name'] = addslashes(trim(input('get.value')));
- $area_model->editArea($update_array, $condition);
- \areacache::deleteCacheFile();
- \areacache::updateAreaPhp();
- \areacache::updateAreaArrayJs();
- echo 'true';
- exit;
- break;
- /**
- * 地区 排序 显示 设置
- */
- case 'area_sort':
- $area_model = model('area');
- $condition[] = array('area_id','=',intval(input('get.id')));
- $update_array = array();
- $update_array['area_sort'] = trim(input('get.value'));
- $area_model->editArea($update_array, $condition);
- \areacache::deleteCacheFile();
- \areacache::updateAreaPhp();
- \areacache::updateAreaArrayJs();
- echo 'true';
- exit;
- case 'area_region':
- $area_model = model('area');
- $condition[] = array('area_id','=',intval(input('get.id')));
- $update_array = array();
- $update_array['area_region'] = trim(input('get.value'));
- $area_model->editArea($update_array, $condition);
-
- \areacache::deleteCacheFile();
- \areacache::updateAreaArrayJs();
- \areacache::updateAreaPhp();
- echo 'true';
- exit;
- case 'area_index_show':
- $area_model = model('area');
- $condition[] = array('area_id','=',intval(input('get.id')));
- $update_array = array();
- $update_array[input('get.column')] = input('get.value');
- $area_model->editArea($update_array, $condition);
- \areacache::deleteCacheFile();
- \areacache::updateAreaArrayJs();
- \areacache::updateAreaPhp();
- echo 'true';
- exit;
- break;
- /**
- * 添加、修改操作中 检测类别名称是否有重复
- */
- case 'check_class_name':
- $area_model = model('area');
- $condition[]=array('area_name','=',trim(input('param.area_name')));
- $condition[]=array('area_parent_id','=',intval(input('param.area_parent_id')));
- $condition[]=array('area_id','<>', intval(input('param.area_id')));
- $class_list = $area_model->getAreaList($condition);
- if (empty($class_list)) {
- echo 'true';
- exit;
- } else {
- echo 'false';
- exit;
- }
- break;
- }
- }
- public function add() {
- if (!request()->isPost()) {
- $area = array(
- 'area_parent_id' => input('param.area_id'),
- );
- View::assign('area', $area);
- View::assign('parents', $this->_get_options());
- return View::fetch('form');
- } else {
- $area_mod=model('area');
- $area_parent_id = intval(input('param.area_parentid'));
-
- $area = $area_mod->getAreaInfo(array('area_id'=>$area_parent_id));
- $area_deep=intval($area['area_deep'])+1;
- if($area_deep>MAX_LAYER){
- $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
- }
- $data = array(
- 'area_name' => input('post.area_name'),
- 'area_initial' => input('post.area_initial'),
- 'area_region' => input('post.area_region'),
- 'area_parent_id' => $area_parent_id,
- 'area_deep'=> $area_deep,
- 'area_sort' => input('post.area_sort'),
- );
- $region_validate = ds_validate('region');
- if (!$region_validate->scene('add')->check($data)) {
- $this->error($region_validate->getError());
- }
- $result = $area_mod->addArea($data);
- if ($result) {
- \areacache::deleteCacheFile();
- \areacache::updateAreaArrayJs();
- \areacache::updateAreaPhp();
- dsLayerOpenSuccess(lang('ds_common_save_succ'));
- } else {
- $this->error(lang('ds_common_save_fail'));
- }
- }
- }
- public function edit() {
- $area_id = intval(input('param.area_id'));
- if ($area_id<=0) {
- $this->error(lang('param_error'));
- }
- $area_mod=model('area');
- $area = $area_mod->getAreaInfo(array('area_id'=>$area_id));
- if(!$area){
- $this->error(lang('area_empty'));
- }
- if (!request()->isPost()) {
-
- View::assign('area', $area);
- View::assign('parents', $this->_get_options());
- return View::fetch('form');
- } else {
- $area_parent_id = intval(input('param.area_parentid'));
- $data = array(
- 'area_name' => input('post.area_name'),
- 'area_initial' => input('post.area_initial'),
- 'area_region' => input('post.area_region'),
- 'area_parent_id' => $area_parent_id,
- 'area_sort' => input('post.area_sort'),
- );
- $region_validate = ds_validate('region');
- if (!$region_validate->scene('edit')->check($data)) {
- $this->error($region_validate->getError());
- }
- if($data['area_parent_id']==$area_id){
- $this->error(lang('area_parent_error'));
- }
- try {
- Db::startTrans();
- if($data['area_parent_id']!=$area['area_parent_id']){
- //如果不同级
- $now_deep=intval(Db::name('area')->where('area_id='.$data['area_parent_id'])->value('area_deep'))+1;
- $old_deep=intval(Db::name('area')->where('area_id='.$area['area_parent_id'])->value('area_deep'))+1;
- if($now_deep!=$old_deep){
- if($now_deep>MAX_LAYER){
- $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
- }
- $data['area_deep']=$now_deep;
- $j=$old_deep;
- $subQuery='('.$area_id.')';
- while($j<=MAX_LAYER){
- //如果自己的上级是自己的下级则报错
- if(Db::name('area')->where('area_id='.$data['area_parent_id'].' AND area_parent_id IN '.$subQuery)->value('area_id')){
- Db::rollback();
- $this->error(lang('area_parent_error'));
- }
- $subQuery=Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql();
- $j++;
- }
- //给他的下级修改深度
- $i=$now_deep+1;
- $subQuery='('.$area_id.')';
- while($i<=MAX_LAYER){
- Db::name('area')->where('area_parent_id IN '.$subQuery)->update(array('area_deep'=>$i));
- $subQuery='(SELECT area_id FROM '.Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql().' a)';
- $i++;
- }
- }
- }
- $result = $area_mod->editArea($data,array('area_id'=>$area_id));
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- Db::commit();
- if ($result >= 0) {
- \areacache::deleteCacheFile();
- \areacache::updateAreaArrayJs();
- \areacache::updateAreaPhp();
- dsLayerOpenSuccess(lang('ds_common_op_succ'));
- } else {
- $this->error(lang('ds_common_op_fail'));
- }
- }
- }
- public function drop() {
- $area_id = input('param.area_id');
- if (empty($area_id)) {
- $this->error(lang('param_error'));
- }
- //判断此分类下是否有子分类
- $area_mod=model('area');
- $result = $area_mod->getAreaInfo(array('area_parent_id'=>$area_id));
- if ($result) {
- ds_json_encode(10001, lang('please_drop_child_region'));
- }
- $result = $area_mod->delArea(array('area_id'=>$area_id));
- if ($result) {
- \areacache::deleteCacheFile();
- \areacache::updateAreaArrayJs();
- \areacache::updateAreaPhp();
- ds_json_encode(10000, lang('ds_common_op_succ'));
- } else {
- ds_json_encode(10001, lang('error'));
- }
- }
- /* 取得可以作为上级的地区分类数据 */
- function _get_options($except = NULL) {
- $area = $this->_area_model->getAreaChild();
- if (empty($area)) {
- return;
- }
- $tree = new \mall\Tree();
- $tree->setTree($area, 'area_id', 'area_parent_id', 'area_name');
- return $tree->getOptions(MAX_LAYER - 1, 0, $except);
- }
- protected function getAdminItemList() {
- $menu_array = array(
- array(
- 'name' => 'index',
- 'text' => lang('ds_manage'),
- 'url' => (string)url('Region/index')
- ),
- );
- if (request()->action() == 'add' || request()->action() == 'index') {
- $menu_array[] = array(
- 'name' => 'add',
- 'text' => lang('ds_new'),
- 'url' =>"javascript:dsLayerOpen('".(string)url('Region/add')."','".lang('ds_add')."')",
- );
- }
- if (request()->action() == 'edit') {
- $menu_array[] = array(
- 'name' => 'edit',
- 'text' => lang('ds_edit'),
- 'url' => 'javascript:void(0)'
- );
- }
- return $menu_array;
- }
- }
|