ShippingTemplates.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\ShippingTemplatesFree;
  5. use app\admin\model\system\ShippingTemplatesRegion;
  6. use crmeb\services\UtilService;
  7. use crmeb\services\JsonService;
  8. use app\admin\model\system\SystemCity;
  9. use app\admin\model\system\ShippingTemplates as STModel;
  10. class ShippingTemplates extends AuthController
  11. {
  12. /**
  13. * 列表页面
  14. * @return string
  15. * @throws \Exception
  16. */
  17. public function index()
  18. {
  19. return $this->fetch();
  20. }
  21. /**
  22. * 添加页面
  23. * @return string
  24. * @throws \Exception
  25. */
  26. public function add($id = 0)
  27. {
  28. $this->assign(compact('id'));
  29. return $this->fetch();
  30. }
  31. /**
  32. * 修改
  33. * @return string
  34. * @throws \Exception
  35. */
  36. public function edit($id = 0)
  37. {
  38. $templates = STModel::get($id);
  39. if (!$templates) {
  40. return JsonService::fail('修改的模板不存在');
  41. }
  42. $data['appointList'] = ShippingTemplatesFree::getFreeList($id);
  43. $data['templateList'] = ShippingTemplatesRegion::getRegionList($id);
  44. if (!isset($data['templateList'][0]['region'])) {
  45. $data['templateList'][0]['region'] = ['city_id' => 0, 'name' => '默认全国'];
  46. }
  47. $data['formData'] = [
  48. 'name' => $templates->name,
  49. 'type' => $templates->getData('type'),
  50. 'appoint_check' => $templates->getData('appoint'),
  51. 'sort' => $templates->getData('sort'),
  52. 'collect_on_delivery' => $templates->getData('collect_on_delivery') ?: 0,
  53. ];
  54. return JsonService::successful($data);
  55. }
  56. /**
  57. * 选择城市页面
  58. * @return string
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function city()
  64. {
  65. $data = UtilService::getMore([
  66. ['type', 0],
  67. ['isedit', 0]
  68. ]);
  69. $this->assign('is_layui', true);
  70. $this->assign($data);
  71. return $this->fetch();
  72. }
  73. public function city_list()
  74. {
  75. $list = SystemCity::with('children')->where('parent_id', 0)->order('id asc')->select();
  76. return app('json')->success($list->toArray());
  77. }
  78. /**
  79. * 列表数据
  80. */
  81. public function temp_list()
  82. {
  83. $where = UtilService::postMore([
  84. ['page', 1],
  85. ['limit', 20],
  86. ['name', '']
  87. ]);
  88. return JsonService::successlayui(STModel::getList($where));
  89. }
  90. /**
  91. * 保存或者修改
  92. * @param int $id
  93. */
  94. public function save($id = 0)
  95. {
  96. $data = UtilService::postMore([
  97. ['region_info', []],
  98. ['appoint_info', []],
  99. ['sort', 0],
  100. ['type', 0],
  101. ['name', ''],
  102. ['appoint', 0],
  103. ['collect_on_delivery', 0],
  104. ]);
  105. $temp['name'] = $data['name'];
  106. $temp['type'] = $data['type'];
  107. $temp['appoint'] = $data['appoint'];
  108. $temp['sort'] = $data['sort'];
  109. $temp['collect_on_delivery'] = $data['collect_on_delivery'];
  110. $temp['add_time'] = time();
  111. STModel::beginTrans();
  112. $res = true;
  113. try {
  114. if ($id) {
  115. $res = STModel::where('id', $id)->update($temp);
  116. } else {
  117. $id = STModel::insertGetId($temp);
  118. }
  119. //设置区域配送
  120. $res = $res && ShippingTemplatesRegion::saveRegion($data['region_info'], $data['type'], $id);
  121. if (!$res) {
  122. STModel::rollbackTrans();
  123. return JsonService::fail(ShippingTemplatesRegion::getErrorInfo());
  124. }
  125. //设置指定包邮
  126. if ($data['appoint']) {
  127. $res = $res && ShippingTemplatesFree::saveFree($data['appoint_info'], $data['type'], $id);
  128. }
  129. if ($res) {
  130. STModel::commitTrans();
  131. return JsonService::successful('保存成功');
  132. } else {
  133. STModel::rollbackTrans();
  134. return JsonService::fail(ShippingTemplatesFree::getErrorInfo('保存失败'));
  135. }
  136. } catch (\Throwable $e) {
  137. STModel::rollbackTrans();
  138. return JsonService::fail($e->getMessage());
  139. }
  140. }
  141. /**
  142. * 删除运费模板
  143. */
  144. public function delete()
  145. {
  146. $data = UtilService::getMore([
  147. ['id', ''],
  148. ]);
  149. if ($data['id'] == 1) {
  150. return JsonService::fail('默认模板不能删除');
  151. } else {
  152. STModel::del($data['id']);
  153. ShippingTemplatesRegion::where('temp_id', $data['id'])->delete();
  154. ShippingTemplatesFree::where('temp_id', $data['id'])->delete();
  155. return JsonService::successful('删除成功');
  156. }
  157. }
  158. /**
  159. * 获取所有运费模板
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function get_template_list()
  165. {
  166. return JsonService::successful(STModel::order('sort desc,id desc')->field(['id', 'name'])->select()->toArray());
  167. }
  168. }