Flearegion.php 9.3 KB

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