ShippingTemplates.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. ];
  53. return JsonService::successful($data);
  54. }
  55. /**
  56. * 选择城市页面
  57. * @return string
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function city()
  63. {
  64. $data = UtilService::getMore([
  65. ['type', 0],
  66. ['isedit', 0]
  67. ]);
  68. $this->assign('is_layui', true);
  69. $this->assign($data);
  70. return $this->fetch();
  71. }
  72. public function city_list()
  73. {
  74. $list = SystemCity::with('children')->where('parent_id', 0)->order('id asc')->select();
  75. return app('json')->success($list->toArray());
  76. }
  77. /**
  78. * 列表数据
  79. */
  80. public function temp_list()
  81. {
  82. $where = UtilService::postMore([
  83. ['page', 1],
  84. ['limit', 20],
  85. ['name', '']
  86. ]);
  87. return JsonService::successlayui(STModel::getList($where));
  88. }
  89. /**
  90. * 保存或者修改
  91. * @param int $id
  92. */
  93. public function save($id = 0)
  94. {
  95. $data = UtilService::postMore([
  96. ['region_info', []],
  97. ['appoint_info', []],
  98. ['sort', 0],
  99. ['type', 0],
  100. ['name', ''],
  101. ['appoint', 0],
  102. ]);
  103. $temp['name'] = $data['name'];
  104. $temp['type'] = $data['type'];
  105. $temp['appoint'] = $data['appoint'];
  106. $temp['sort'] = $data['sort'];
  107. $temp['add_time'] = time();
  108. STModel::beginTrans();
  109. $res = true;
  110. try {
  111. if ($id) {
  112. $res = STModel::where('id', $id)->update($temp);
  113. } else {
  114. $id = STModel::insertGetId($temp);
  115. }
  116. //设置区域配送
  117. $res = $res && ShippingTemplatesRegion::saveRegion($data['region_info'], $data['type'], $id);
  118. if (!$res) {
  119. STModel::rollbackTrans();
  120. return JsonService::fail(ShippingTemplatesRegion::getErrorInfo());
  121. }
  122. //设置指定包邮
  123. if ($data['appoint']) {
  124. $res = $res && ShippingTemplatesFree::saveFree($data['appoint_info'], $data['type'], $id);
  125. }
  126. if ($res) {
  127. STModel::commitTrans();
  128. return JsonService::successful('保存成功');
  129. } else {
  130. STModel::rollbackTrans();
  131. return JsonService::fail(ShippingTemplatesFree::getErrorInfo('保存失败'));
  132. }
  133. } catch (\Throwable $e) {
  134. STModel::rollbackTrans();
  135. return JsonService::fail($e->getMessage());
  136. }
  137. }
  138. /**
  139. * 删除运费模板
  140. */
  141. public function delete()
  142. {
  143. $data = UtilService::getMore([
  144. ['id', ''],
  145. ]);
  146. if ($data['id'] == 1) {
  147. return JsonService::fail('默认模板不能删除');
  148. } else {
  149. STModel::del($data['id']);
  150. ShippingTemplatesRegion::where('temp_id', $data['id'])->delete();
  151. ShippingTemplatesFree::where('temp_id', $data['id'])->delete();
  152. return JsonService::successful('删除成功');
  153. }
  154. }
  155. /**
  156. * 获取所有运费模板
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function get_template_list()
  162. {
  163. return JsonService::successful(STModel::order('sort desc,id desc')->field(['id', 'name'])->select()->toArray());
  164. }
  165. }