Category.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use think\Db;
  4. use addons\shopro\library\Tree;
  5. use app\admin\model\shopro\Category as CategoryModel;
  6. /**
  7. * 商品分类
  8. */
  9. class Category extends Common
  10. {
  11. protected $noNeedRight = ['select', 'goodsSelect'];
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->model = new CategoryModel;
  16. }
  17. /**
  18. * 服务保障列表
  19. *
  20. * @return \think\Response
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. $categories = $this->model->sheepFilter()->where('parent_id', 0)->order('weigh', 'desc')->order('id', 'desc')->select();
  28. $this->success('获取成功', null, $categories);
  29. }
  30. /**
  31. * 添加服务保障
  32. *
  33. * @return \think\Response
  34. */
  35. public function add()
  36. {
  37. if (!$this->request->isAjax()) {
  38. return $this->view->fetch();
  39. }
  40. $params = $this->request->only([
  41. 'name', 'style', 'description', 'weigh', 'categories'
  42. ]);
  43. $this->svalidate($params, ".add");
  44. $categories = json_decode($params['categories'], true);
  45. Db::transaction(function () use ($params, $categories) {
  46. $this->model->allowField(true)->save($params);
  47. //递归处理分类数据
  48. $this->createOrEditCategory($categories, $this->model->id);
  49. });
  50. $this->success('保存成功');
  51. }
  52. /**
  53. * 服务保障详情
  54. *
  55. * @param $id
  56. * @return \think\Response
  57. */
  58. public function detail($id)
  59. {
  60. $category = $this->model->where('parent_id', 0)->where('id', $id)->find();
  61. if (!$category) {
  62. $this->error(__('No Results were found'));
  63. }
  64. $categories = $this->model->with('children.children')->where('parent_id', $category->id)->order('weigh', 'desc')->order('id', 'desc')->select();
  65. $this->success('获取成功', null, ['category' => $category, 'categories' => $categories]);
  66. }
  67. /**
  68. * 修改商品分类
  69. *
  70. * @return \think\Response
  71. */
  72. public function edit($id = null)
  73. {
  74. if (!$this->request->isPost()) {
  75. return $this->view->fetch('add');
  76. }
  77. $params = $this->request->only([
  78. 'name', 'style', 'description', 'weigh', 'categories'
  79. ]);
  80. $this->svalidate($params, ".edit");
  81. $categories = json_decode($params['categories'], true);
  82. $category = $this->model->where('parent_id', 0)->where('id', $id)->find();
  83. if (!$category) {
  84. $this->error(__('No Results were found'));
  85. }
  86. Db::transaction(function () use ($category, $params, $categories) {
  87. $category->allowField(true)->save($params);
  88. //递归处理分类数据
  89. $this->createOrEditCategory($categories, $category->id);
  90. });
  91. $this->success('更新成功');
  92. }
  93. /**
  94. * 删除服务标签
  95. *
  96. * @param string $id 要删除的服务保障列表
  97. * @return void
  98. */
  99. public function delete($id)
  100. {
  101. if (empty($id)) {
  102. $this->error(__('Parameter %s can not be empty', 'id'));
  103. }
  104. $list = $this->model->with('children')->where('id', 'in', $id)->select();
  105. $result = Db::transaction(function () use ($list) {
  106. $count = 0;
  107. foreach ($list as $item) {
  108. if ($item->children) {
  109. $this->error('请先删除子分类');
  110. }
  111. $count += $item->delete();
  112. }
  113. return $count;
  114. });
  115. if ($result) {
  116. $this->success('删除成功', null, $result);
  117. } else {
  118. $this->error(__('No rows were deleted'));
  119. }
  120. }
  121. /**
  122. * 获取所有服务列表
  123. *
  124. * @return \think\Response
  125. */
  126. public function select()
  127. {
  128. if (!$this->request->isAjax()) {
  129. return $this->view->fetch();
  130. }
  131. $categories = (new Tree(function () {
  132. // 组装搜索条件,排序等
  133. return $this->model->field("id, name, parent_id, status, weigh")->normal()
  134. ->order('weigh', 'desc')->order('id', 'asc');
  135. }))->getTree(0);
  136. $this->success('获取成功', null, $categories);
  137. }
  138. /**
  139. * 商品列表左边分类
  140. *
  141. * @return void
  142. */
  143. public function goodsSelect()
  144. {
  145. $categories = $this->model->with(['children'])
  146. ->where('parent_id', 0)->order('weigh', 'desc')->order('id', 'desc')->select();
  147. $this->success('获取成功', null, $categories);
  148. }
  149. private function createOrEditCategory($categories, $parent_id)
  150. {
  151. foreach ($categories as $key => $data) {
  152. $data['parent_id'] = $parent_id;
  153. if (isset($data['id']) && $data['id']) {
  154. $category = $this->model->find($data['id']);
  155. if (!$category) {
  156. $this->error(__('No Results were found'));
  157. }
  158. if (isset($data['deleted']) && $data['deleted'] == 1) {
  159. $category->delete();
  160. } else {
  161. $category->name = $data['name'];
  162. $category->parent_id = $data['parent_id'];
  163. $category->image = $data['image'];
  164. $category->description = $data['description'] ?? null;
  165. $category->status = $data['status'];
  166. $category->weigh = $data['weigh'];
  167. $category->save();
  168. }
  169. } else {
  170. if (!isset($data['deleted']) || !$data['deleted']) {
  171. $category = new CategoryModel;
  172. $category->name = $data['name'];
  173. $category->parent_id = $data['parent_id'];
  174. $category->image = $data['image'];
  175. $category->description = $data['description'] ?? null;
  176. $category->status = $data['status'];
  177. $category->weigh = $data['weigh'];
  178. $category->save();
  179. $data['id'] = $category->id;
  180. }
  181. }
  182. if (isset($data['children']) && !empty($data['children']) && isset($data['id'])) {
  183. $this->createOrEditCategory($data['children'], $data['id']);
  184. }
  185. }
  186. }
  187. }