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