Category.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Category as CategoryModel;
  5. use fast\Tree;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 分类管理
  12. *
  13. * @icon fa fa-list
  14. * @remark 用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
  15. */
  16. class Category extends Backend
  17. {
  18. /**
  19. * @var \app\common\model\Category
  20. */
  21. protected $model = null;
  22. protected $categorylist = [];
  23. protected $noNeedRight = ['selectpage'];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('app\common\model\Category');
  28. $tree = Tree::instance();
  29. $tree->init(collection($this->model->where('cid', 'in', [0, $this->auth->getUserInfo()['cid']])->order('weigh desc,id desc')->select())->toArray(), 'pid');
  30. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  31. $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
  32. foreach ($this->categorylist as $k => $v) {
  33. $categorydata[$v['id']] = $v;
  34. }
  35. $typeList = CategoryModel::getTypeList();
  36. $this->view->assign("flagList", $this->model->getFlagList());
  37. $this->view->assign("typeList", $typeList);
  38. $this->view->assign("parentList", $categorydata);
  39. $this->assignconfig('typeList', $typeList);
  40. }
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags']);
  48. if ($this->request->isAjax()) {
  49. $search = $this->request->request("search");
  50. $type = $this->request->request("type");
  51. //构造父类select列表选项数据
  52. $list = [];
  53. foreach ($this->categorylist as $k => $v) {
  54. if ($search) {
  55. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  56. if ($type == "all" || $type == null) {
  57. $list = $this->categorylist;
  58. } else {
  59. $list[] = $v;
  60. }
  61. }
  62. } else {
  63. if ($type == "all" || $type == null) {
  64. $list = $this->categorylist;
  65. } elseif ($v['type'] == $type) {
  66. $list[] = $v;
  67. }
  68. }
  69. }
  70. $total = count($list);
  71. $result = array("total" => $total, "rows" => $list);
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 添加
  78. */
  79. public function add()
  80. {
  81. if ($this->request->isPost()) {
  82. $this->token();
  83. }
  84. if ($this->request->isPost()) {
  85. $params = $this->request->post("row/a");
  86. if ($params) {
  87. $params = $this->preExcludeFields($params);
  88. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  89. $params[$this->dataLimitField] = $this->auth->id;
  90. }
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  98. $this->model->validateFailException(true)->validate($validate);
  99. }
  100. $params['cid'] = $this->auth->getUserInfo()['cid'];
  101. $result = $this->model->allowField(true)->save($params);
  102. Db::commit();
  103. } catch (ValidateException $e) {
  104. Db::rollback();
  105. $this->error($e->getMessage());
  106. } catch (PDOException $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. } catch (Exception $e) {
  110. Db::rollback();
  111. $this->error($e->getMessage());
  112. }
  113. if ($result !== false) {
  114. $this->success();
  115. } else {
  116. $this->error(__('No rows were inserted'));
  117. }
  118. }
  119. $this->error(__('Parameter %s can not be empty', ''));
  120. }
  121. return $this->view->fetch();
  122. }
  123. /**
  124. * 编辑
  125. */
  126. public function edit($ids = null)
  127. {
  128. $row = $this->model->get($ids);
  129. if (!$row) {
  130. $this->error(__('No Results were found'));
  131. }
  132. if ($row['cid'] == 0 && $this->auth->getUserInfo()['cid'] != 0) {
  133. $this->error(__('You have no permission'));
  134. }
  135. if ($row['cid'] != $this->auth->getUserInfo()['cid']) {
  136. $this->error(__('You have no permission'));
  137. }
  138. $adminIds = $this->getDataLimitAdminIds();
  139. if (is_array($adminIds)) {
  140. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  141. $this->error(__('You have no permission'));
  142. }
  143. }
  144. if ($this->request->isPost()) {
  145. $this->token();
  146. $params = $this->request->post("row/a");
  147. if ($params) {
  148. $params = $this->preExcludeFields($params);
  149. if ($params['pid'] != $row['pid']) {
  150. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  151. if (in_array($params['pid'], $childrenIds)) {
  152. $this->error(__('Can not change the parent to child or itself'));
  153. }
  154. }
  155. try {
  156. //是否采用模型验证
  157. if ($this->modelValidate) {
  158. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  159. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  160. $row->validate($validate);
  161. }
  162. $result = $row->allowField(true)->save($params);
  163. if ($result !== false) {
  164. $this->success();
  165. } else {
  166. $this->error($row->getError());
  167. }
  168. } catch (\think\exception\PDOException $e) {
  169. $this->error($e->getMessage());
  170. } catch (\think\Exception $e) {
  171. $this->error($e->getMessage());
  172. }
  173. }
  174. $this->error(__('Parameter %s can not be empty', ''));
  175. }
  176. $this->view->assign("row", $row);
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * Selectpage搜索
  181. *
  182. * @internal
  183. */
  184. public function selectpage()
  185. {
  186. return parent::selectpage();
  187. }
  188. }