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