Delivery.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\supplier\controller\setting;
  3. use app\supplier\controller\Controller;
  4. use app\common\model\settings\Region as RegionModel;
  5. use app\supplier\model\settings\Delivery as DeliveryModel;
  6. /**
  7. * 运费模板控制器
  8. */
  9. class Delivery extends Controller
  10. {
  11. /**
  12. * 运费模板列表
  13. */
  14. public function index()
  15. {
  16. $model = new DeliveryModel;
  17. $list = $model->getList($this->postData(), $this->getSupplierId());
  18. return $this->renderSuccess('', compact('list'));
  19. }
  20. /**
  21. * 配送区域
  22. */
  23. public function area()
  24. {
  25. // 获取所有地区
  26. $regionData = RegionModel::getCacheTree();
  27. $arr = $this->dataFormat($regionData);
  28. return $this->renderSuccess('', compact('arr'));
  29. }
  30. /**
  31. * 新增
  32. */
  33. public function add()
  34. {
  35. // 新增记录
  36. $model = new DeliveryModel;
  37. $data = $this->postData();
  38. if ($model->add($data, $this->getSupplierId())) {
  39. return $this->renderSuccess('添加成功');
  40. }
  41. return $this->renderError($model->getError() ?: '添加失败');
  42. }
  43. /**
  44. * 详情
  45. */
  46. public function detail($delivery_id = 0)
  47. {
  48. // 获取所有地区
  49. $regionData = RegionModel::getCacheTree();
  50. $arr = $this->dataFormat($regionData);
  51. // 地区总数
  52. $cityCount = RegionModel::getCacheCounts()['city'];
  53. //新增
  54. if($delivery_id == 0){
  55. return $this->renderSuccess('', compact('arr', 'cityCount'));
  56. }
  57. // 详情
  58. $detail = DeliveryModel::detail($delivery_id, $this->getSupplierId());
  59. // 获取配送区域及运费设置项
  60. $formData = $detail->getFormList();
  61. $returnData = $this->dataFormat1($arr, $formData);
  62. $arr = $returnData['arr'];
  63. return $this->renderSuccess('', compact('detail', 'arr', 'cityCount', 'formData'));
  64. }
  65. /**
  66. * 格式化数据
  67. */
  68. public function dataFormat($regionData)
  69. {
  70. $arr = [];
  71. foreach ($regionData as $val) {
  72. $city = array_column($val['city'], 'name');
  73. $id = array_column($val['city'], 'id');
  74. $len = count($city);
  75. $arr2 = [];
  76. for ($i = 0; $i < $len; $i++) {
  77. $arr1 = [
  78. 'value' => $id[$i],
  79. 'label' => $city[$i],
  80. ];
  81. array_push($arr2, $arr1);
  82. }
  83. $arr[] = [
  84. 'value' => $val['id'],
  85. 'label' => $val['name'],
  86. 'children' => $arr2
  87. ];
  88. $arr2 = [];
  89. }
  90. return $arr;
  91. }
  92. /**
  93. * 格式化数据1
  94. */
  95. public function dataFormat1($arr, $formData)
  96. {
  97. foreach ($arr as $key => &$val) {
  98. $val['index'] = null;
  99. $index = 0;
  100. foreach ($formData as $k => $v) {
  101. if (in_array($val['value'], $v['province'])) {
  102. $citys = array();
  103. $val['checked'] = true;
  104. if (is_array($val['index'])) {
  105. $list = $val['index'];
  106. array_push($list, $index);
  107. $val['index'] = $list;
  108. } else {
  109. $val['index'] = array($index);
  110. }
  111. foreach ($val['children'] as $c => &$city) {
  112. if (in_array($city['value'], $v['citys'])) {
  113. $city['checked'] = true;
  114. $city['index'] = $index;
  115. $citys[] = $city;
  116. }
  117. }
  118. $province = array(
  119. 'value' => $arr[$key]['value'],
  120. 'label' => $arr[$key]['label'],
  121. 'children' => $citys);
  122. $formData[$k]['areas'][] = $province;
  123. }
  124. $index++;
  125. }
  126. }
  127. return array('arr' => $arr, "formData" => $formData);
  128. }
  129. /**
  130. * 修改
  131. */
  132. public function edit($delivery_id = 0)
  133. {
  134. if($this->request->isGet()){
  135. return $this->detail($delivery_id);
  136. }
  137. if ($delivery_id == 0) {
  138. return $this->add();
  139. }
  140. $model = DeliveryModel::detail($delivery_id, $this->getSupplierId());
  141. // 更新记录
  142. if ($model->edit($this->postData())) {
  143. return $this->renderSuccess('更新成功');
  144. }
  145. return $this->renderError($model->getError() ?: '更新失败');
  146. }
  147. /**
  148. * 删除记录
  149. */
  150. public function delete($delivery_id)
  151. {
  152. $model = DeliveryModel::detail($delivery_id, $this->getSupplierId());
  153. if (!$model->remove()) {
  154. return $this->renderError($model->getError() ?: '删除失败');
  155. }
  156. return $this->renderSuccess('删除成功');
  157. }
  158. }