Offpayarea.php 7.0 KB

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