Delivery.php 2.9 KB

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