Offpayarea.php 7.2 KB

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