Area.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller\shopro\data;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 地区管理
  7. */
  8. class Area extends Common
  9. {
  10. protected $noNeedRight = ['select'];
  11. /**
  12. * Faq模型对象
  13. * @var \app\admin\model\shopro\data\Express
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\shopro\data\Area;
  20. }
  21. /**
  22. * 查看
  23. *
  24. * @return string|Json
  25. * @throws \think\Exception
  26. * @throws DbException
  27. */
  28. public function index()
  29. {
  30. if (!$this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. $list = $this->model->sheepFilter()->with('children.children')->where('pid', 0)->select(); // 查询全部
  34. $this->success('', null, $list);
  35. }
  36. public function select()
  37. {
  38. if (!$this->request->isAjax()) {
  39. return $this->view->fetch();
  40. }
  41. $level = $this->request->param('level', 'all');
  42. $with = ['children.children'];
  43. if ($level == 'city') {
  44. $with = ['children'];
  45. } else if ($level == 'province') {
  46. $with = [];
  47. }
  48. $list = $this->model->with($with)->where('pid', 0)->field('id, name, pid, level')->select(); // 查询全部
  49. $this->success('', null, $list);
  50. }
  51. /**
  52. * 添加
  53. */
  54. public function add()
  55. {
  56. if (!$this->request->isAjax()) {
  57. return $this->view->fetch();
  58. }
  59. $params = $this->request->only(['id', 'pid', 'name']);
  60. $params['level'] = 'province';
  61. if (isset($params['pid']) && $params['pid']) {
  62. $parent = $this->model->find($params['pid']);
  63. if (!$parent) {
  64. $this->error(__('No Results were found'));
  65. }
  66. if ($parent['level'] == 'province') {
  67. $params['level'] = 'city';
  68. } else if ($parent['level'] == 'city') {
  69. $params['level'] = 'district';
  70. }
  71. }
  72. $this->model->save($params);
  73. $this->success('保存成功', null, $this->model);
  74. }
  75. /**
  76. * 详情
  77. *
  78. * @param $id
  79. * @return \think\Response
  80. */
  81. public function detail($id)
  82. {
  83. $detail = $this->model->where('id', $id)->find();
  84. if (!$detail) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $this->success('获取成功', null, $detail);
  88. }
  89. /**
  90. * 编辑(支持批量)
  91. */
  92. public function edit($old_id = null)
  93. {
  94. if (!$this->request->isAjax()) {
  95. return $this->view->fetch('add');
  96. }
  97. $params = $this->request->only(['id', 'pid', 'name']);
  98. $params['level'] = 'province';
  99. if (isset($params['pid']) && $params['pid']) {
  100. $parent = $this->model->find($params['pid']);
  101. if (!$parent) {
  102. $this->error(__('No Results were found'));
  103. }
  104. if ($parent['level'] == 'province') {
  105. $params['level'] = 'city';
  106. } else if ($parent['level'] == 'city') {
  107. $params['level'] = 'district';
  108. }
  109. }
  110. $area = $this->model->where('id', 'in', $old_id)->find();
  111. if (!$area) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $area->save($params);
  115. $this->success('更新成功', null, $area);
  116. }
  117. /**
  118. * 删除
  119. *
  120. * @param $id
  121. * @return \think\Response
  122. */
  123. public function delete($id)
  124. {
  125. if (empty($id)) {
  126. $this->error(__('Parameter %s can not be empty', 'id'));
  127. }
  128. $area = $this->model->where('id', 'in', $id)->find();
  129. if (!$area) {
  130. $this->error(__('No Results were found'));
  131. }
  132. $children = $this->model->where('pid', $id)->count();
  133. if ($children) {
  134. $this->error('请先删除下级地市');
  135. }
  136. $area->delete();
  137. $this->success('删除成功');
  138. }
  139. }