Flearegion.php 9.1 KB

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