Delivery.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\store\controller\setting;
  3. use app\store\controller\Controller;
  4. use app\store\model\Region as RegionModel;
  5. use app\store\model\Delivery as DeliveryModel;
  6. /**
  7. * 配送设置
  8. * Class Delivery
  9. * @package app\store\controller\setting
  10. */
  11. class Delivery extends Controller
  12. {
  13. /**
  14. * 配送模板列表
  15. * @return mixed
  16. * @throws \think\exception\DbException
  17. */
  18. public function index()
  19. {
  20. $model = new DeliveryModel;
  21. $list = $model->getList();
  22. return $this->fetch('index', compact('list'));
  23. }
  24. /**
  25. * 删除模板
  26. * @param $delivery_id
  27. * @return array
  28. * @throws \think\Exception
  29. * @throws \think\exception\DbException
  30. * @throws \think\exception\PDOException
  31. */
  32. public function delete($delivery_id)
  33. {
  34. $model = DeliveryModel::detail($delivery_id);
  35. if (!$model->remove()) {
  36. return $this->renderError($model->getError() ?: '删除失败');
  37. }
  38. return $this->renderSuccess('删除成功');
  39. }
  40. /**
  41. * 添加配送模板
  42. * @return array|mixed
  43. * @throws \think\Exception
  44. * @throws \think\exception\PDOException
  45. */
  46. public function add()
  47. {
  48. if (!$this->request->isAjax()) {
  49. // 获取所有地区
  50. $regionData = json_encode(RegionModel::getCacheTree());
  51. // 地区总数
  52. $cityCount = RegionModel::getCacheCounts()['city'];
  53. return $this->fetch('add', compact('regionData', 'cityCount'));
  54. }
  55. // 新增记录
  56. $model = new DeliveryModel;
  57. if ($model->add($this->postData('delivery'))) {
  58. return $this->renderSuccess('添加成功', url('setting.delivery/index'));
  59. }
  60. return $this->renderError($model->getError() ?: '添加失败');
  61. }
  62. /**
  63. * 编辑配送模板
  64. * @param $delivery_id
  65. * @return array|mixed
  66. * @throws \think\Exception
  67. * @throws \think\exception\DbException
  68. * @throws \think\exception\PDOException
  69. */
  70. public function edit($delivery_id)
  71. {
  72. // 模板详情
  73. $model = DeliveryModel::detail($delivery_id);
  74. if (!$this->request->isAjax()) {
  75. // 获取所有地区
  76. $regionData = json_encode(RegionModel::getCacheTree());
  77. // 地区总数
  78. $cityCount = RegionModel::getCacheCounts()['city'];
  79. // 获取配送区域及运费设置项
  80. $formData = json_encode($model->getFormList());
  81. return $this->fetch('add', compact('model', 'regionData', 'cityCount', 'formData'));
  82. }
  83. // 更新记录
  84. if ($model->edit($this->postData('delivery'))) {
  85. return $this->renderSuccess('更新成功', url('setting.delivery/index'));
  86. }
  87. return $this->renderError($model->getError() ?: '更新失败');
  88. }
  89. }