Offpayarea.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Offpayarea extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/offpayarea.lang.php');
  18. }
  19. public function index()
  20. {
  21. \areacache::deleteCacheFile();
  22. \areacache::updateAreaArrayJs();
  23. \areacache::updateAreaPhp();
  24. $offpayarea_model = model('offpayarea');
  25. $area_model = model('area');
  26. //默认平台店铺id
  27. $store_id = 1;
  28. if (request()->isPost()) {
  29. $county_array = input('post.county');
  30. if (!preg_match('/^[\d,]+$/', $county_array)) {
  31. $county_array = '';
  32. }
  33. //内置自营店ID
  34. $area_info = $offpayarea_model->getOffpayareaInfo(array('store_id' => $store_id));
  35. $data = array();
  36. $county = trim($county_array, ',');
  37. //地区修改
  38. $county_array = explode(',', $county);
  39. $all_array = array();
  40. $province_array = input('post.province/a');
  41. if (!empty($province_array) && is_array($province_array)) {
  42. foreach ($province_array as $v) {
  43. $all_array[$v] = $v;
  44. }
  45. }
  46. $city_array = input('post.city/a');
  47. if (!empty($city_array) && is_array($city_array)) {
  48. foreach ($city_array as $v) {
  49. $all_array[$v] = $v;
  50. }
  51. }
  52. if (is_array($county_array)) {
  53. foreach ($county_array as $pid) {
  54. if ($pid == '') continue;
  55. $all_array[$pid] = $pid;
  56. $temp = $area_model->getChildsByPid($pid);
  57. if (!empty($temp) && is_array($temp)) {
  58. foreach ($temp as $v) {
  59. $all_array[$v] = $v;
  60. }
  61. }
  62. }
  63. }
  64. $all_array = array_values($all_array);
  65. $data['area_id'] = serialize($all_array);
  66. if (!$area_info) {
  67. $data['store_id'] = $store_id;
  68. $result = $offpayarea_model->addOffpayarea($data);
  69. } else {
  70. $result = $offpayarea_model->editOffpayarea(array('store_id' => $store_id), $data);
  71. }
  72. if ($result) {
  73. $this->success(lang('ds_common_op_succ'));
  74. } else {
  75. $this->error(lang('ds_common_op_fail'));
  76. }
  77. }
  78. //取出支持货到付款的县ID及上级市ID
  79. $parea_info = $offpayarea_model->getOffpayareaInfo(array('store_id' => $store_id));
  80. if (!empty($parea_info['area_id'])) {
  81. $parea_ids = @unserialize($parea_info['area_id']);
  82. }
  83. if (empty($parea_ids)) {
  84. $parea_ids = array();
  85. }
  86. View::assign('parea_ids', $parea_ids);
  87. //取出支持货到付款县ID的上级市ID
  88. $city_checked_child_array = array();
  89. //地区修改
  90. $county_array = $area_model->getAreaList(array('area_deep' => 3), 'area_id,area_parent_id');
  91. foreach ($county_array as $v) {
  92. if (in_array($v['area_id'], $parea_ids)) {
  93. $city_checked_child_array[$v['area_parent_id']][] = $v['area_id'];
  94. }
  95. }
  96. //halt($city_checked_child_array);
  97. View::assign('city_checked_child_array', $city_checked_child_array);
  98. //市级下面的县是不是全部支持货到付款,如果全部支持,默认选中,如果其中部分县支持货到付款,默认不选中但显示一个支付到付县的数量
  99. //格式 city_id => 下面支持到付的县ID数量
  100. $city_count_array = array();
  101. //格式 city_id => 是否选中true/false
  102. $city_checked_array = array();
  103. $list = $area_model->getAreaList(array('area_deep' => 3), 'area_parent_id,count(area_id) as child_count', 'area_parent_id');
  104. foreach ($list as $k => $v) {
  105. $city_count_array[$v['area_parent_id']] = $v['child_count'];
  106. }
  107. foreach ($city_checked_child_array as $city_id => $city_child) {
  108. if (count($city_child) > 0) {
  109. if (count($city_child) == $city_count_array[$city_id]) {
  110. $city_checked_array[$city_id] = true;
  111. }
  112. }
  113. }
  114. View::assign('city_checked_array', $city_checked_array);
  115. //取得省级地区及直属子地区(循环输出)
  116. require(PUBLIC_PATH . DIRECTORY_SEPARATOR . "static" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . '/area_datas.php');
  117. //地区修改 修改地区从3级变成5级,以及N级引发的错误
  118. $province_array = array();
  119. foreach ($area_array as $k => $v) {
  120. if ($v['area_parent_id'] == '0') {
  121. $province_array[$k] = $k;
  122. }
  123. }
  124. foreach ($area_array as $k => $v) {
  125. if ($v['area_parent_id'] != '0') {
  126. if (in_array($v['area_parent_id'], $province_array)) {
  127. $area_array[$v['area_parent_id']]['child'][$k] = $v['area_name'];
  128. }
  129. unset($area_array[$k]);
  130. }
  131. }
  132. View::assign('province_array', $area_array);
  133. //计算哪些省需要默认选中(即该省下面的所有县都支持到付,即所有市都是选中状态)
  134. $province_array = $area_array;
  135. foreach ($province_array as $pid => $value) {
  136. if (isset($value['child']) && is_array($value['child'])) {
  137. foreach ($value['child'] as $k => $v) {
  138. if (!array_key_exists($k, $city_checked_array)) {
  139. unset($province_array[$pid]);
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. View::assign('province_checked_array', $province_array);
  146. $this->setAdminCurItem('index');
  147. return View::fetch();
  148. }
  149. protected function getAdminItemList()
  150. {
  151. $menu_array = array(
  152. array(
  153. 'name' => 'index',
  154. 'text' => lang('ds_offpayarea'),
  155. 'url' => (string)url('Offpayarea/index')
  156. )
  157. );
  158. return $menu_array;
  159. }
  160. }