Region.php 13 KB

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