Category.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if((input('keyField') && input('keyValue')))
  67. {
  68. if($v[input('keyField')] == input('keyValue'))
  69. {
  70. $list[] = $v;
  71. }
  72. }
  73. else{
  74. $list[] = $v;
  75. }
  76. }
  77. }
  78. }
  79. $total = count($list);
  80. $result = array("total" => $total, "rows" => $list);
  81. return json($result);
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost()) {
  91. $this->token();
  92. }
  93. if ($this->request->isPost()) {
  94. $params = $this->request->post("row/a");
  95. if ($params) {
  96. $params = $this->preExcludeFields($params);
  97. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  98. $params[$this->dataLimitField] = $this->auth->id;
  99. }
  100. $result = false;
  101. Db::startTrans();
  102. try {
  103. //是否采用模型验证
  104. if ($this->modelValidate) {
  105. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  107. $this->model->validateFailException(true)->validate($validate);
  108. }
  109. $params['cid'] = $this->auth->getUserInfo()['cid'];
  110. $result = $this->model->allowField(true)->save($params);
  111. Db::commit();
  112. } catch (ValidateException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (PDOException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. if ($result !== false) {
  123. $this->success();
  124. } else {
  125. $this->error(__('No rows were inserted'));
  126. }
  127. }
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 编辑
  134. */
  135. public function edit($ids = null)
  136. {
  137. $row = $this->model->get($ids);
  138. if (!$row) {
  139. $this->error(__('No Results were found'));
  140. }
  141. if ($row['cid'] == 0 && $this->auth->getUserInfo()['cid'] != 0) {
  142. $this->error(__('You have no permission'));
  143. }
  144. if ($row['cid'] != $this->auth->getUserInfo()['cid']) {
  145. $this->error(__('You have no permission'));
  146. }
  147. $adminIds = $this->getDataLimitAdminIds();
  148. if (is_array($adminIds)) {
  149. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  150. $this->error(__('You have no permission'));
  151. }
  152. }
  153. if ($this->request->isPost()) {
  154. $this->token();
  155. $params = $this->request->post("row/a");
  156. if ($params) {
  157. $params = $this->preExcludeFields($params);
  158. if ($params['pid'] != $row['pid']) {
  159. $childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
  160. if (in_array($params['pid'], $childrenIds)) {
  161. $this->error(__('Can not change the parent to child or itself'));
  162. }
  163. }
  164. try {
  165. //是否采用模型验证
  166. if ($this->modelValidate) {
  167. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  168. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  169. $row->validate($validate);
  170. }
  171. $result = $row->allowField(true)->save($params);
  172. if ($result !== false) {
  173. $this->success();
  174. } else {
  175. $this->error($row->getError());
  176. }
  177. } catch (\think\exception\PDOException $e) {
  178. $this->error($e->getMessage());
  179. } catch (\think\Exception $e) {
  180. $this->error($e->getMessage());
  181. }
  182. }
  183. $this->error(__('Parameter %s can not be empty', ''));
  184. }
  185. $this->view->assign("row", $row);
  186. return $this->view->fetch();
  187. }
  188. /**
  189. * Selectpage搜索
  190. *
  191. * @internal
  192. */
  193. public function selectpage()
  194. {
  195. return parent::selectpage();
  196. }
  197. }