Flearegion.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 控制器
  12. */
  13. class Flearegion extends AdminControl
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/region.lang.php');
  19. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/flea.lang.php');
  20. }
  21. /**
  22. * 地区列表
  23. *
  24. * @param
  25. * @return
  26. */
  27. public function flea_region()
  28. {
  29. /**
  30. * 实例化模型
  31. */
  32. $fleaarea_model = model('fleaarea');
  33. /**
  34. * 增加 修改 地区信息
  35. */
  36. if (request()->isPost()) {
  37. /**
  38. * 是否生成缓存的标识
  39. */
  40. $new_cache = true;
  41. /**
  42. * 新增地区
  43. */
  44. $new_area_name_array = input('post.new_area_name/a'); #获取数组
  45. if (is_array($new_area_name_array) && !empty($new_area_name_array)) {
  46. foreach ($new_area_name_array as $k => $v) {
  47. if (!empty($v)) {
  48. $insert_array = array();
  49. $insert_array['fleaarea_name'] = $v;
  50. $insert_array['fleaarea_parent_id'] = input('post.fleaarea_parent_id');
  51. $insert_array['fleaarea_sort'] = intval($new_area_name_array[$k]);
  52. $insert_array['fleaarea_deep'] = input('post.child_area_deep');
  53. $fleaarea_model->addFleaarea($insert_array);
  54. $new_cache = true;
  55. }
  56. }
  57. }
  58. /**
  59. * 修改地区
  60. */
  61. $area_name_array = input('post.area_name/a'); #获取数组
  62. if (is_array($area_name_array) && !empty($area_name_array)) {
  63. foreach ($area_name_array as $k => $v) {
  64. if (!empty($v)) {
  65. $insert_array = array();
  66. $insert_array['fleaarea_name'] = $v;
  67. $insert_array['fleaarea_sort'] = intval($area_name_array[$k]);
  68. $fleaarea_model->editFleaarea(array('fleaarea_id' => $k), $insert_array);
  69. $new_cache = true;
  70. }
  71. }
  72. }
  73. /**
  74. * 删除地区
  75. */
  76. $hidden_del_id = input('post.hidden_del_id');
  77. if (!empty($hidden_del_id)) {
  78. $hidden_del_id = trim($hidden_del_id, '|');
  79. $del_id = explode('|', $hidden_del_id);
  80. $fleaarea_model->delFleaarea($del_id, input('post.child_area_deep'));
  81. $new_cache = true;
  82. }
  83. /**
  84. * 更新缓存
  85. */
  86. if ($new_cache === true) {
  87. \fleacache::getCache('flea_area', array('deep' => input('post.child_area_deep'), 'new' => '1'));
  88. }
  89. $this->success(lang('region_index_modify_succ'));
  90. } else {
  91. /**
  92. * 导航地区内容
  93. */
  94. /**
  95. * 一级
  96. */
  97. $province_list = \fleacache::getCache('flea_area', array('deep' => '1'));
  98. $child_area_deep = 1;
  99. /**
  100. * 二级
  101. */
  102. $city_list = array();
  103. $district_list = array();
  104. if (input('param.province')) {
  105. $cache_data = \fleacache::getCache('flea_area', array('deep' => '2'));
  106. if (is_array($cache_data)) {
  107. foreach ($cache_data as $k => $v) {
  108. if ($v['fleaarea_parent_id'] == intval(input('param.province'))) {
  109. $city_list[] = $v;
  110. }
  111. }
  112. }
  113. unset($cache_data);
  114. $child_area_deep = 2;
  115. /**
  116. * 三级
  117. */
  118. if (input('param.city')) {
  119. $cache_data = \fleacache::getCache('flea_area', array('deep' => '3'));
  120. if (is_array($cache_data)) {
  121. foreach ($cache_data as $k => $v) {
  122. if ($v['fleaarea_parent_id'] == intval(input('param.city'))) {
  123. $district_list[] = $v;
  124. }
  125. }
  126. }
  127. unset($cache_data);
  128. $child_area_deep = 3;
  129. /**
  130. * 四级
  131. */
  132. if (input('param.district')) {
  133. $child_area_deep = 4;
  134. }
  135. }
  136. }
  137. /**
  138. * 地区列表
  139. */
  140. $condition = array();
  141. $fleaarea_parent_id = input('param.fleaarea_parent_id') ? input('param.fleaarea_parent_id') : '0';
  142. $condition[] = array('fleaarea_parent_id', '=', $fleaarea_parent_id);
  143. $area_list = $fleaarea_model->getFleaareaList($condition);
  144. View::assign('province', input('param.province') ? input('param.province') : '');
  145. View::assign('city', input('param.city'));
  146. View::assign('district', input('param.district'));
  147. View::assign('province_list', $province_list);
  148. View::assign('city_list', $city_list);
  149. View::assign('district_list', $district_list);
  150. View::assign('fleaarea_parent_id', input('param.fleaarea_parent_id') ? input('param.fleaarea_parent_id') : '0');
  151. View::assign('area_list', $area_list);
  152. View::assign('child_area_deep', $child_area_deep);
  153. $this->setAdminCurItem('index');
  154. return View::fetch('index');
  155. }
  156. }
  157. /**
  158. * 导入默认地区
  159. *
  160. * @param
  161. * @return
  162. */
  163. public function flea_import_default_area()
  164. {
  165. $file = PUBLIC_PATH . '/examples/flea_area.sql';
  166. if (!is_file($file)) {
  167. ds_json_encode(10001, lang('region_import_csv_null'));
  168. }
  169. $handle = @fopen($file, "r");
  170. $tmp_sql = '';
  171. if ($handle) {
  172. Db::query("TRUNCATE TABLE `" . config('database.connections.mysql.prefix') . "fleaarea`");
  173. while (!feof($handle)) {
  174. $buffer = fgets($handle);
  175. if (trim($buffer) != '') {
  176. $tmp_sql .= $buffer;
  177. if (substr(rtrim($buffer), -1) == ';') {
  178. if (preg_match('/^(INSERT)\s+(INTO)\s+/i', ltrim($tmp_sql)) && substr(rtrim($buffer), -2) == ');') {
  179. //标准的SQL语句,将被执行
  180. } else {
  181. //不能组成标准的SQL语句,继续向下一行取内容,直到组成合法的SQL为止
  182. continue;
  183. }
  184. if (!empty($tmp_sql)) {
  185. if (strtoupper(CHARSET) == 'GBK') {
  186. $tmp_sql = iconv('UTF-8', strtoupper(CHARSET), $tmp_sql);
  187. }
  188. $tmp_sql = str_replace("`#__fleaarea`", "`" . config('database.connections.mysql.prefix') . "fleaarea`", $tmp_sql);
  189. Db::query($tmp_sql);
  190. $tmp_sql = '';
  191. }
  192. }
  193. }
  194. }
  195. @fclose($handle);
  196. /**
  197. * 重新生成缓存
  198. */
  199. for ($i = 1; $i <= 4; $i++) {
  200. $tmp = 'flea_area_parent_id_' . $i;
  201. if (isset($$tmp) && intval($$tmp) >= 0) {
  202. \fleacache::getCache('flea_area', array('deep' => intval($i), 'new' => '1'));
  203. }
  204. }
  205. ds_json_encode(10000, lang('region_import_succ'));
  206. } else {
  207. ds_json_encode(10001, lang('region_import_csv_null'));
  208. }
  209. }
  210. protected function getAdminItemList()
  211. {
  212. $menu_array = array(
  213. array(
  214. 'name' => 'index', 'text' => lang('ds_manage'), 'url' => (string)url('Flearegion/flea_region')
  215. ),
  216. array(
  217. 'name' => 'import', 'text' => lang('flea_region_import'), 'url' => "javascript:dsLayerConfirm('" . (string)url('Flearegion/flea_import_default_area') . "','" . lang('import_confirm') . "')"
  218. ),
  219. );
  220. return $menu_array;
  221. }
  222. }