ShippingTemplates.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. } else {
  126. if ($id) {
  127. if (ShippingTemplatesFree::where('temp_id', $id)->count()) {
  128. $res = $res && ShippingTemplatesFree::where('temp_id', $id)->delete();
  129. }
  130. }
  131. }
  132. if ($res) {
  133. STModel::commitTrans();
  134. return JsonService::successful('保存成功');
  135. } else {
  136. STModel::rollbackTrans();
  137. return JsonService::fail(ShippingTemplatesFree::getErrorInfo('保存失败'));
  138. }
  139. } catch (\Throwable $e) {
  140. STModel::rollbackTrans();
  141. return JsonService::fail($e->getMessage());
  142. }
  143. }
  144. /**
  145. * 删除运费模板
  146. */
  147. public function delete()
  148. {
  149. $data = UtilService::getMore([
  150. ['id', ''],
  151. ]);
  152. if ($data['id'] == 1) {
  153. return JsonService::fail('默认模板不能删除');
  154. } else {
  155. STModel::del($data['id']);
  156. ShippingTemplatesRegion::where('temp_id', $data['id'])->delete();
  157. ShippingTemplatesFree::where('temp_id', $data['id'])->delete();
  158. return JsonService::successful('删除成功');
  159. }
  160. }
  161. /**
  162. * 获取所有运费模板
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\DbException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. */
  167. public function get_template_list()
  168. {
  169. return JsonService::successful(STModel::order('sort desc,id desc')->field(['id', 'name'])->select()->toArray());
  170. }
  171. }