Region.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * 地区设置
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Db;
  8. use think\facade\Lang;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Region extends AdminControl {
  21. public function initialize() {
  22. parent::initialize();
  23. Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/region.lang.php');
  24. $this->_area_model = model('Area');
  25. define('MAX_LAYER', 3);
  26. }
  27. public function index() {
  28. $region_list = $this->_area_model->getAreaChild(0);
  29. /* 先根排序 */
  30. foreach ($region_list as $key => $val) {
  31. $region_list[$key]['switchs'] = 0;
  32. if ($this->_area_model->getAreaChild($val['area_id'])) {
  33. $region_list[$key]['switchs'] = 1;
  34. }
  35. }
  36. View::assign('region_list', $region_list);
  37. $this->setAdminCurItem('index');
  38. return View::fetch();
  39. }
  40. function ajax_cate() {
  41. $cate_id = input('param.id');
  42. if (empty($cate_id)) {
  43. return;
  44. }
  45. $cate = $this->_area_model->getAreaChild($cate_id);
  46. foreach ($cate as $key => $val) {
  47. $child = $this->_area_model->getAreaChild($val['area_id']);
  48. if ($val['area_deep'] >= MAX_LAYER) {
  49. $cate[$key]['add_child'] = 0;
  50. } else {
  51. $cate[$key]['add_child'] = 1;
  52. }
  53. if (!$child || empty($child)) {
  54. $cate[$key]['switchs'] = 0;
  55. } else {
  56. $cate[$key]['switchs'] = 1;
  57. }
  58. }
  59. echo json_encode(array_values($cate));
  60. return;
  61. }
  62. /**
  63. * ajax操作
  64. */
  65. public function ajax() {
  66. $condition = array();
  67. switch (input('param.branch')) {
  68. /**
  69. * 更新地区
  70. */
  71. case 'area_name':
  72. $area_model = model('area');
  73. $condition[] = array('area_id','=',intval(input('get.id')));
  74. $update_array = array();
  75. $update_array['area_name'] = addslashes(trim(input('get.value')));
  76. $area_model->editArea($update_array, $condition);
  77. \areacache::deleteCacheFile();
  78. \areacache::updateAreaPhp();
  79. \areacache::updateAreaArrayJs();
  80. echo 'true';
  81. exit;
  82. break;
  83. /**
  84. * 地区 排序 显示 设置
  85. */
  86. case 'area_sort':
  87. $area_model = model('area');
  88. $condition[] = array('area_id','=',intval(input('get.id')));
  89. $update_array = array();
  90. $update_array['area_sort'] = trim(input('get.value'));
  91. $area_model->editArea($update_array, $condition);
  92. \areacache::deleteCacheFile();
  93. \areacache::updateAreaPhp();
  94. \areacache::updateAreaArrayJs();
  95. echo 'true';
  96. exit;
  97. case 'area_region':
  98. $area_model = model('area');
  99. $condition[] = array('area_id','=',intval(input('get.id')));
  100. $update_array = array();
  101. $update_array['area_region'] = trim(input('get.value'));
  102. $area_model->editArea($update_array, $condition);
  103. \areacache::deleteCacheFile();
  104. \areacache::updateAreaArrayJs();
  105. \areacache::updateAreaPhp();
  106. echo 'true';
  107. exit;
  108. case 'area_index_show':
  109. $area_model = model('area');
  110. $condition[] = array('area_id','=',intval(input('get.id')));
  111. $update_array = array();
  112. $update_array[input('get.column')] = input('get.value');
  113. $area_model->editArea($update_array, $condition);
  114. \areacache::deleteCacheFile();
  115. \areacache::updateAreaArrayJs();
  116. \areacache::updateAreaPhp();
  117. echo 'true';
  118. exit;
  119. break;
  120. /**
  121. * 添加、修改操作中 检测类别名称是否有重复
  122. */
  123. case 'check_class_name':
  124. $area_model = model('area');
  125. $condition[]=array('area_name','=',trim(input('param.area_name')));
  126. $condition[]=array('area_parent_id','=',intval(input('param.area_parent_id')));
  127. $condition[]=array('area_id','<>', intval(input('param.area_id')));
  128. $class_list = $area_model->getAreaList($condition);
  129. if (empty($class_list)) {
  130. echo 'true';
  131. exit;
  132. } else {
  133. echo 'false';
  134. exit;
  135. }
  136. break;
  137. }
  138. }
  139. public function add() {
  140. if (!request()->isPost()) {
  141. $area = array(
  142. 'area_parent_id' => input('param.area_id'),
  143. );
  144. View::assign('area', $area);
  145. View::assign('parents', $this->_get_options());
  146. return View::fetch('form');
  147. } else {
  148. $area_mod=model('area');
  149. $area_parent_id = intval(input('param.area_parentid'));
  150. $area = $area_mod->getAreaInfo(array('area_id'=>$area_parent_id));
  151. $area_deep=intval($area['area_deep'])+1;
  152. if($area_deep>MAX_LAYER){
  153. $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
  154. }
  155. $data = array(
  156. 'area_name' => input('post.area_name'),
  157. 'area_initial' => input('post.area_initial'),
  158. 'area_region' => input('post.area_region'),
  159. 'area_parent_id' => $area_parent_id,
  160. 'area_deep'=> $area_deep,
  161. 'area_sort' => input('post.area_sort'),
  162. );
  163. $region_validate = ds_validate('region');
  164. if (!$region_validate->scene('add')->check($data)) {
  165. $this->error($region_validate->getError());
  166. }
  167. $result = $area_mod->addArea($data);
  168. if ($result) {
  169. \areacache::deleteCacheFile();
  170. \areacache::updateAreaArrayJs();
  171. \areacache::updateAreaPhp();
  172. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  173. } else {
  174. $this->error(lang('ds_common_save_fail'));
  175. }
  176. }
  177. }
  178. public function edit() {
  179. $area_id = intval(input('param.area_id'));
  180. if ($area_id<=0) {
  181. $this->error(lang('param_error'));
  182. }
  183. $area_mod=model('area');
  184. $area = $area_mod->getAreaInfo(array('area_id'=>$area_id));
  185. if(!$area){
  186. $this->error(lang('area_empty'));
  187. }
  188. if (!request()->isPost()) {
  189. View::assign('area', $area);
  190. View::assign('parents', $this->_get_options());
  191. return View::fetch('form');
  192. } else {
  193. $area_parent_id = intval(input('param.area_parentid'));
  194. $data = array(
  195. 'area_name' => input('post.area_name'),
  196. 'area_initial' => input('post.area_initial'),
  197. 'area_region' => input('post.area_region'),
  198. 'area_parent_id' => $area_parent_id,
  199. 'area_sort' => input('post.area_sort'),
  200. );
  201. $region_validate = ds_validate('region');
  202. if (!$region_validate->scene('edit')->check($data)) {
  203. $this->error($region_validate->getError());
  204. }
  205. if($data['area_parent_id']==$area_id){
  206. $this->error(lang('area_parent_error'));
  207. }
  208. try {
  209. Db::startTrans();
  210. if($data['area_parent_id']!=$area['area_parent_id']){
  211. //如果不同级
  212. $now_deep=intval(Db::name('area')->where('area_id='.$data['area_parent_id'])->value('area_deep'))+1;
  213. $old_deep=intval(Db::name('area')->where('area_id='.$area['area_parent_id'])->value('area_deep'))+1;
  214. if($now_deep!=$old_deep){
  215. if($now_deep>MAX_LAYER){
  216. $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
  217. }
  218. $data['area_deep']=$now_deep;
  219. $j=$old_deep;
  220. $subQuery='('.$area_id.')';
  221. while($j<=MAX_LAYER){
  222. //如果自己的上级是自己的下级则报错
  223. if(Db::name('area')->where('area_id='.$data['area_parent_id'].' AND area_parent_id IN '.$subQuery)->value('area_id')){
  224. Db::rollback();
  225. $this->error(lang('area_parent_error'));
  226. }
  227. $subQuery=Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql();
  228. $j++;
  229. }
  230. //给他的下级修改深度
  231. $i=$now_deep+1;
  232. $subQuery='('.$area_id.')';
  233. while($i<=MAX_LAYER){
  234. Db::name('area')->where('area_parent_id IN '.$subQuery)->update(array('area_deep'=>$i));
  235. $subQuery='(SELECT area_id FROM '.Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql().' a)';
  236. $i++;
  237. }
  238. }
  239. }
  240. $result = $area_mod->editArea($data,array('area_id'=>$area_id));
  241. } catch (Exception $e) {
  242. Db::rollback();
  243. $this->error($e->getMessage());
  244. }
  245. Db::commit();
  246. if ($result >= 0) {
  247. \areacache::deleteCacheFile();
  248. \areacache::updateAreaArrayJs();
  249. \areacache::updateAreaPhp();
  250. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  251. } else {
  252. $this->error(lang('ds_common_op_fail'));
  253. }
  254. }
  255. }
  256. public function drop() {
  257. $area_id = input('param.area_id');
  258. if (empty($area_id)) {
  259. $this->error(lang('param_error'));
  260. }
  261. //判断此分类下是否有子分类
  262. $area_mod=model('area');
  263. $result = $area_mod->getAreaInfo(array('area_parent_id'=>$area_id));
  264. if ($result) {
  265. ds_json_encode(10001, lang('please_drop_child_region'));
  266. }
  267. $result = $area_mod->delArea(array('area_id'=>$area_id));
  268. if ($result) {
  269. \areacache::deleteCacheFile();
  270. \areacache::updateAreaArrayJs();
  271. \areacache::updateAreaPhp();
  272. ds_json_encode(10000, lang('ds_common_op_succ'));
  273. } else {
  274. ds_json_encode(10001, lang('error'));
  275. }
  276. }
  277. /* 取得可以作为上级的地区分类数据 */
  278. function _get_options($except = NULL) {
  279. $area = $this->_area_model->getAreaChild();
  280. if (empty($area)) {
  281. return;
  282. }
  283. $tree = new \mall\Tree();
  284. $tree->setTree($area, 'area_id', 'area_parent_id', 'area_name');
  285. return $tree->getOptions(MAX_LAYER - 1, 0, $except);
  286. }
  287. protected function getAdminItemList() {
  288. $menu_array = array(
  289. array(
  290. 'name' => 'index',
  291. 'text' => lang('ds_manage'),
  292. 'url' => (string)url('Region/index')
  293. ),
  294. );
  295. if (request()->action() == 'add' || request()->action() == 'index') {
  296. $menu_array[] = array(
  297. 'name' => 'add',
  298. 'text' => lang('ds_new'),
  299. 'url' =>"javascript:dsLayerOpen('".(string)url('Region/add')."','".lang('ds_add')."')",
  300. );
  301. }
  302. if (request()->action() == 'edit') {
  303. $menu_array[] = array(
  304. 'name' => 'edit',
  305. 'text' => lang('ds_edit'),
  306. 'url' => 'javascript:void(0)'
  307. );
  308. }
  309. return $menu_array;
  310. }
  311. }