ShippingTemplates.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\adminapi\controller\v1\setting;
  3. use app\models\system\ShippingTemplatesFree;
  4. use app\models\system\ShippingTemplatesRegion;
  5. use app\models\system\SystemCity;
  6. use app\models\system\ShippingTemplates as STModel;
  7. use app\adminapi\controller\AuthController;
  8. use crmeb\services\UtilService;
  9. class ShippingTemplates extends AuthController
  10. {
  11. /**
  12. * 运费模板列表
  13. * @return mixed
  14. */
  15. public function temp_list()
  16. {
  17. $where = UtilService::getMore([
  18. [['page','d'], 1],
  19. [['limit','d'], 20],
  20. [['name','s'], '']
  21. ]);
  22. $where['mer_id'] = $this->merId ?: '';
  23. return $this->success(STModel::getList($where));
  24. }
  25. /**
  26. * 修改
  27. * @return string
  28. * @throws \Exception
  29. */
  30. public function edit($id = 0)
  31. {
  32. $templates = STModel::get($id);
  33. if (!$templates) {
  34. return $this->fail('修改的模板不存在');
  35. }
  36. $data['appointList'] = ShippingTemplatesFree::getFreeList($id);
  37. $data['templateList'] = ShippingTemplatesRegion::getRegionList($id);
  38. if (!isset($data['templateList'][0]['region'])) {
  39. $data['templateList'][0]['region'] = ['city_id' => 0, 'name' => '默认全国'];
  40. }
  41. $data['formData'] = [
  42. 'name' => $templates->name,
  43. 'type' => $templates->getData('type'),
  44. 'appoint_check' => $templates->getData('appoint'),
  45. 'sort' => $templates->getData('sort'),
  46. ];
  47. return $this->success($data);
  48. }
  49. /**
  50. * 保存或者修改
  51. * @param int $id
  52. */
  53. public function save($id = 0)
  54. {
  55. $data = UtilService::postMore([
  56. [['region_info','a'], []],
  57. [['appoint_info','a'], []],
  58. [['sort','d'], 0],
  59. [['type','d'], 0],
  60. [['name','s'], ''],
  61. [['appoint','d'], 0],
  62. ]);
  63. $this->validate($data, \app\adminapi\validates\setting\ShippingTemplatesValidate::class, 'save');
  64. $temp['name'] = $data['name'];
  65. $temp['type'] = $data['type'];
  66. $temp['appoint'] = $data['appoint'];
  67. $temp['sort'] = $data['sort'];
  68. $temp['add_time'] = time();
  69. $temp['mer_id'] = $this->merId ?: '';
  70. STModel::beginTrans();
  71. $res = true;
  72. try {
  73. if ($id) {
  74. $res = STModel::where('id', $id)->update($temp);
  75. } else {
  76. $id = STModel::insertGetId($temp);
  77. }
  78. //设置区域配送
  79. $res = $res && ShippingTemplatesRegion::saveRegion($data['region_info'], $data['type'], $id);
  80. if (!$res) {
  81. STModel::rollbackTrans();
  82. return $this->fail(ShippingTemplatesRegion::getErrorInfo());
  83. }
  84. //设置指定包邮
  85. if ($data['appoint']) {
  86. $res = $res && ShippingTemplatesFree::saveFree($data['appoint_info'], $data['type'], $id);
  87. }
  88. if ($res) {
  89. STModel::commitTrans();
  90. return $this->success('保存成功');
  91. } else {
  92. STModel::rollbackTrans();
  93. return $this->fail(ShippingTemplatesFree::getErrorInfo('保存失败'));
  94. }
  95. } catch (\Throwable $e) {
  96. STModel::rollbackTrans();
  97. return $this->fail($e->getMessage());
  98. }
  99. }
  100. /**
  101. * 删除运费模板
  102. */
  103. public function delete()
  104. {
  105. $data = UtilService::getMore([
  106. [['id','d'], 0],
  107. ]);
  108. if ($data['id'] == 1) {
  109. return $this->fail('默认模板不能删除');
  110. } else {
  111. STModel::del($data['id']);
  112. ShippingTemplatesRegion::where('temp_id', $data['id'])->delete();
  113. ShippingTemplatesFree::where('temp_id', $data['id'])->delete();
  114. return $this->success('删除成功');
  115. }
  116. }
  117. /**
  118. * 城市数据
  119. * @return mixed
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function city_list()
  125. {
  126. $list = SystemCity::with('children')->where('parent_id', 0)->order('id asc')->select();
  127. return $this->success($list->toArray());
  128. }
  129. }