Category.php 7.7 KB

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