Region.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. * DSMall多用户商城
  12. * ============================================================================
  13. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  14. * 网站地址: http://www.csdeshang.com
  15. * ----------------------------------------------------------------------------
  16. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  17. * 不允许对程序代码以任何形式任何目的的再发布。
  18. * ============================================================================
  19. * 控制器
  20. */
  21. class Region extends AdminControl {
  22. public function initialize() {
  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. $region_list = $this->_area_model->getAreaChild(0);
  30. /* 先根排序 */
  31. foreach ($region_list as $key => $val) {
  32. $region_list[$key]['switchs'] = 0;
  33. if ($this->_area_model->getAreaChild($val['area_id'])) {
  34. $region_list[$key]['switchs'] = 1;
  35. }
  36. }
  37. View::assign('region_list', $region_list);
  38. $this->setAdminCurItem('index');
  39. return View::fetch();
  40. }
  41. function ajax_cate() {
  42. $cate_id = input('param.id');
  43. if (empty($cate_id)) {
  44. return;
  45. }
  46. $cate = $this->_area_model->getAreaChild($cate_id);
  47. foreach ($cate as $key => $val) {
  48. $child = $this->_area_model->getAreaChild($val['area_id']);
  49. if ($val['area_deep'] >= MAX_LAYER) {
  50. $cate[$key]['add_child'] = 0;
  51. } else {
  52. $cate[$key]['add_child'] = 1;
  53. }
  54. if (!$child || empty($child)) {
  55. $cate[$key]['switchs'] = 0;
  56. } else {
  57. $cate[$key]['switchs'] = 1;
  58. }
  59. }
  60. echo json_encode(array_values($cate));
  61. return;
  62. }
  63. /**
  64. * ajax操作
  65. */
  66. public function ajax() {
  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. if (!request()->isPost()) {
  142. $area = array(
  143. 'area_parent_id' => input('param.area_id'),
  144. );
  145. View::assign('area', $area);
  146. View::assign('parents', $this->_get_options());
  147. return View::fetch('form');
  148. } else {
  149. $area_mod=model('area');
  150. $area_parent_id = intval(input('param.area_parentid'));
  151. $area = $area_mod->getAreaInfo(array('area_id'=>$area_parent_id));
  152. $area_deep=intval($area['area_deep'])+1;
  153. if($area_deep>MAX_LAYER){
  154. $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
  155. }
  156. $data = array(
  157. 'area_name' => input('post.area_name'),
  158. 'area_initial' => input('post.area_initial'),
  159. 'area_region' => input('post.area_region'),
  160. 'area_parent_id' => $area_parent_id,
  161. 'area_deep'=> $area_deep,
  162. 'area_sort' => input('post.area_sort'),
  163. );
  164. $region_validate = ds_validate('region');
  165. if (!$region_validate->scene('add')->check($data)) {
  166. $this->error($region_validate->getError());
  167. }
  168. $result = $area_mod->addArea($data);
  169. if ($result) {
  170. \areacache::deleteCacheFile();
  171. \areacache::updateAreaArrayJs();
  172. \areacache::updateAreaPhp();
  173. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  174. } else {
  175. $this->error(lang('ds_common_save_fail'));
  176. }
  177. }
  178. }
  179. public function edit() {
  180. $area_id = intval(input('param.area_id'));
  181. if ($area_id<=0) {
  182. $this->error(lang('param_error'));
  183. }
  184. $area_mod=model('area');
  185. $area = $area_mod->getAreaInfo(array('area_id'=>$area_id));
  186. if(!$area){
  187. $this->error(lang('area_empty'));
  188. }
  189. if (!request()->isPost()) {
  190. View::assign('area', $area);
  191. View::assign('parents', $this->_get_options());
  192. return View::fetch('form');
  193. } else {
  194. $area_parent_id = intval(input('param.area_parentid'));
  195. $data = array(
  196. 'area_name' => input('post.area_name'),
  197. 'area_initial' => input('post.area_initial'),
  198. 'area_region' => input('post.area_region'),
  199. 'area_parent_id' => $area_parent_id,
  200. 'area_sort' => input('post.area_sort'),
  201. );
  202. $region_validate = ds_validate('region');
  203. if (!$region_validate->scene('edit')->check($data)) {
  204. $this->error($region_validate->getError());
  205. }
  206. if($data['area_parent_id']==$area_id){
  207. $this->error(lang('area_parent_error'));
  208. }
  209. try {
  210. Db::startTrans();
  211. if($data['area_parent_id']!=$area['area_parent_id']){
  212. //如果不同级
  213. $now_deep=intval(Db::name('area')->where('area_id='.$data['area_parent_id'])->value('area_deep'))+1;
  214. $old_deep=intval(Db::name('area')->where('area_id='.$area['area_parent_id'])->value('area_deep'))+1;
  215. if($now_deep!=$old_deep){
  216. if($now_deep>MAX_LAYER){
  217. $this->error(sprintf(lang('area_deep_error'), MAX_LAYER));
  218. }
  219. $data['area_deep']=$now_deep;
  220. $j=$old_deep;
  221. $subQuery='('.$area_id.')';
  222. while($j<=MAX_LAYER){
  223. //如果自己的上级是自己的下级则报错
  224. if(Db::name('area')->where('area_id='.$data['area_parent_id'].' AND area_parent_id IN '.$subQuery)->value('area_id')){
  225. Db::rollback();
  226. $this->error(lang('area_parent_error'));
  227. }
  228. $subQuery=Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql();
  229. $j++;
  230. }
  231. //给他的下级修改深度
  232. $i=$now_deep+1;
  233. $subQuery='('.$area_id.')';
  234. while($i<=MAX_LAYER){
  235. Db::name('area')->where('area_parent_id IN '.$subQuery)->update(array('area_deep'=>$i));
  236. $subQuery='(SELECT area_id FROM '.Db::name('area')->field('area_id')->where('area_parent_id IN '.$subQuery)->buildSql().' a)';
  237. $i++;
  238. }
  239. }
  240. }
  241. $result = $area_mod->editArea($data,array('area_id'=>$area_id));
  242. } catch (Exception $e) {
  243. Db::rollback();
  244. $this->error($e->getMessage());
  245. }
  246. Db::commit();
  247. if ($result >= 0) {
  248. \areacache::deleteCacheFile();
  249. \areacache::updateAreaArrayJs();
  250. \areacache::updateAreaPhp();
  251. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  252. } else {
  253. $this->error(lang('ds_common_op_fail'));
  254. }
  255. }
  256. }
  257. public function drop() {
  258. $area_id = input('param.area_id');
  259. if (empty($area_id)) {
  260. $this->error(lang('param_error'));
  261. }
  262. //判断此分类下是否有子分类
  263. $area_mod=model('area');
  264. $result = $area_mod->getAreaInfo(array('area_parent_id'=>$area_id));
  265. if ($result) {
  266. ds_json_encode(10001, lang('please_drop_child_region'));
  267. }
  268. $result = $area_mod->delArea(array('area_id'=>$area_id));
  269. if ($result) {
  270. \areacache::deleteCacheFile();
  271. \areacache::updateAreaArrayJs();
  272. \areacache::updateAreaPhp();
  273. ds_json_encode(10000, lang('ds_common_op_succ'));
  274. } else {
  275. ds_json_encode(10001, lang('error'));
  276. }
  277. }
  278. /* 取得可以作为上级的地区分类数据 */
  279. function _get_options($except = NULL) {
  280. $area = $this->_area_model->getAreaChild();
  281. if (empty($area)) {
  282. return;
  283. }
  284. $tree = new \mall\Tree();
  285. $tree->setTree($area, 'area_id', 'area_parent_id', 'area_name');
  286. return $tree->getOptions(MAX_LAYER - 1, 0, $except);
  287. }
  288. protected function getAdminItemList() {
  289. $menu_array = array(
  290. array(
  291. 'name' => 'index',
  292. 'text' => lang('ds_manage'),
  293. 'url' => (string)url('Region/index')
  294. ),
  295. );
  296. if (request()->action() == 'add' || request()->action() == 'index') {
  297. $menu_array[] = array(
  298. 'name' => 'add',
  299. 'text' => lang('ds_new'),
  300. 'url' =>"javascript:dsLayerOpen('".(string)url('Region/add')."','".lang('ds_add')."')",
  301. );
  302. }
  303. if (request()->action() == 'edit') {
  304. $menu_array[] = array(
  305. 'name' => 'edit',
  306. 'text' => lang('ds_edit'),
  307. 'url' => 'javascript:void(0)'
  308. );
  309. }
  310. return $menu_array;
  311. }
  312. }