Delivery.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\admin\model\unishop\DeliveryRule;
  4. use app\common\controller\Backend;
  5. use app\admin\model\unishop\Area;
  6. /**
  7. * 运费模板
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Delivery extends Backend
  12. {
  13. /**
  14. * Multi方法可批量修改的字段
  15. */
  16. protected $multiFields = 'switch';
  17. /**
  18. * Delivery模型对象
  19. * @var \app\admin\model\unishop\Delivery
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\unishop\Delivery;
  26. $this->view->assign("typeList", $this->model->getTypeList());
  27. $areaData = json_encode(Area::getCacheTree());
  28. $this->view->assign('areaData',$areaData);
  29. }
  30. /**
  31. * 添加
  32. */
  33. public function add()
  34. {
  35. if ($this->request->isPost())
  36. {
  37. $params = $this->request->post("row/a");
  38. if ($params)
  39. {
  40. if(empty($params['area'])){
  41. $this->error('请添加可配送区域和运费');
  42. }
  43. if ($this->dataLimit && $this->dataLimitFieldAutoFill)
  44. {
  45. $params[$this->dataLimitField] = $this->auth->id;
  46. }
  47. try
  48. {
  49. //是否采用模型验证
  50. if ($this->modelValidate)
  51. {
  52. $name = basename(str_replace('\\', '/', get_class($this->model)));
  53. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  54. $this->model->validate($validate);
  55. }
  56. $result = $this->model::saveDelivery($params);
  57. if ($result !== false)
  58. {
  59. $this->success();
  60. }
  61. else
  62. {
  63. $this->error($this->model->getError());
  64. }
  65. }
  66. catch (\think\exception\PDOException $e)
  67. {
  68. $this->error($e->getMessage());
  69. }
  70. }
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 删除
  77. */
  78. public function del($ids = "")
  79. {
  80. if ($ids)
  81. {
  82. $pk = $this->model->getPk();
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds))
  85. {
  86. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  87. }
  88. $list = $this->model->where($pk, 'in', $ids)->select();
  89. $count = 0;
  90. foreach ($list as $k => $v)
  91. {
  92. (new DeliveryRule)->where('delivery_id',$v['id'])->delete();
  93. $count += $v->delete();
  94. }
  95. if ($count)
  96. {
  97. $this->success();
  98. }
  99. else
  100. {
  101. $this->error(__('No rows were deleted'));
  102. }
  103. }
  104. $this->error(__('Parameter %s can not be empty', 'ids'));
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = NULL)
  110. {
  111. $row = $this->model->detail($ids);
  112. if (!$row)
  113. $this->error(__('No Results were found'));
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds))
  116. {
  117. if (!in_array($row[$this->dataLimitField], $adminIds))
  118. {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. if ($this->request->isPost())
  123. {
  124. $params = $this->request->post("row/a");
  125. if ($params)
  126. {
  127. if(empty($params['area'])){
  128. $this->error('请添加可配送区域和运费');
  129. }
  130. try
  131. {
  132. //是否采用模型验证
  133. if ($this->modelValidate)
  134. {
  135. $name = basename(str_replace('\\', '/', get_class($this->model)));
  136. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  137. $row->validate($validate);
  138. }
  139. $result = $this->model::editDelivery($params,$ids);
  140. if ($result !== false)
  141. {
  142. $this->success();
  143. }
  144. else
  145. {
  146. $this->error($row->getError());
  147. }
  148. }
  149. catch (\think\exception\PDOException $e)
  150. {
  151. $this->error($e->getMessage());
  152. }
  153. }
  154. $this->error(__('Parameter %s can not be empty', ''));
  155. }
  156. $this->view->assign("row", $row);
  157. // 模板详情
  158. return $this->view->fetch();
  159. }
  160. }