Category.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if ($v[input('keyField')] == input('keyValue')) {
  71. $list[] = $v;
  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'] && $this->auth->getUserInfo()['cid'] != 0) {
  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. * 删除
  190. */
  191. public function del($ids = "")
  192. {
  193. if (!$this->request->isPost()) {
  194. $this->error(__("Invalid parameters"));
  195. }
  196. $ids = $ids ? $ids : $this->request->post("ids");
  197. if ($ids) {
  198. $pk = $this->model->getPk();
  199. $adminIds = $this->getDataLimitAdminIds();
  200. if (is_array($adminIds)) {
  201. $this->model->where($this->dataLimitField, 'in', $adminIds);
  202. }
  203. if ($this->auth->getUserInfo()['cid'] == 0)
  204. $list = $this->model->where($pk, 'in', $ids)->select();
  205. else
  206. $list = $this->model->where($pk, 'in', $ids)->where('cid', $this->auth->getUserInfo()['cid'])->select();
  207. $count = 0;
  208. Db::startTrans();
  209. try {
  210. foreach ($list as $k => $v) {
  211. $count += $v->delete();
  212. }
  213. Db::commit();
  214. } catch (PDOException $e) {
  215. Db::rollback();
  216. $this->error($e->getMessage());
  217. } catch (Exception $e) {
  218. Db::rollback();
  219. $this->error($e->getMessage());
  220. }
  221. if ($count) {
  222. $this->success();
  223. } else {
  224. $this->error(__('No rows were deleted'));
  225. }
  226. }
  227. $this->error(__('Parameter %s can not be empty', 'ids'));
  228. }
  229. /**
  230. * 真实删除
  231. */
  232. public function destroy($ids = "")
  233. {
  234. if (!$this->request->isPost()) {
  235. $this->error(__("Invalid parameters"));
  236. }
  237. $ids = $ids ? $ids : $this->request->post("ids");
  238. $pk = $this->model->getPk();
  239. $adminIds = $this->getDataLimitAdminIds();
  240. if (is_array($adminIds)) {
  241. $this->model->where($this->dataLimitField, 'in', $adminIds);
  242. }
  243. if ($ids) {
  244. $this->model->where($pk, 'in', $ids);
  245. }
  246. if (!$this->auth->getUserInfo()['cid'] == 0)
  247. $this->model->where('cid', $this->auth->getUserInfo()['cid']);
  248. $count = 0;
  249. Db::startTrans();
  250. try {
  251. $list = $this->model->onlyTrashed()->select();
  252. foreach ($list as $k => $v) {
  253. $count += $v->delete(true);
  254. }
  255. Db::commit();
  256. } catch (PDOException $e) {
  257. Db::rollback();
  258. $this->error($e->getMessage());
  259. } catch (Exception $e) {
  260. Db::rollback();
  261. $this->error($e->getMessage());
  262. }
  263. if ($count) {
  264. $this->success();
  265. } else {
  266. $this->error(__('No rows were deleted'));
  267. }
  268. $this->error(__('Parameter %s can not be empty', 'ids'));
  269. }
  270. /**
  271. * Selectpage搜索
  272. *
  273. * @internal
  274. */
  275. public function selectpage()
  276. {
  277. return parent::selectpage();
  278. }
  279. }