Category.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\controller\general;
  12. use app\admin\traits\Actions;
  13. use app\common\controller\Backend;
  14. use app\common\library\Tree;
  15. use think\annotation\route\Group;
  16. use app\common\model\Category as CategoryModel;
  17. use think\annotation\route\Route;
  18. /**
  19. * 分类管理
  20. */
  21. #[Group("general/category")]
  22. class Category extends Backend
  23. {
  24. protected $noNeedRight = ['selectpage'];
  25. private $categorylist = [];
  26. use Actions{
  27. index as private _index;
  28. edit as private _edit;
  29. add as private _add;
  30. }
  31. protected function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new CategoryModel();
  35. $this->assign('typeList',site_config("dictionary.categorytype"));
  36. }
  37. #[Route("*","index")]
  38. public function index()
  39. {
  40. if($this->request->isAjax()){
  41. $result = ['total' => 1000, 'rows' => $this->getCatelist()];
  42. return json($result);
  43. }
  44. return $this->_index();
  45. }
  46. #[Route("POST,GET","add")]
  47. public function add()
  48. {
  49. if(!$this->request->isPost()){
  50. $catelist=$this->getCatelist();
  51. foreach ($catelist as $k => $v) {
  52. $categorydata[$v['id']] = $v;
  53. }
  54. $this->assign('parentList',$categorydata);
  55. }
  56. return $this->_add();
  57. }
  58. #[Route("POST,GET","edit")]
  59. public function edit()
  60. {
  61. if(!$this->request->isPost()){
  62. $catelist=$this->getCatelist();
  63. foreach ($catelist as $k => $v) {
  64. $categorydata[$v['id']] = $v;
  65. }
  66. $this->assign('parentList',$categorydata);
  67. }
  68. return $this->_edit();
  69. }
  70. private function getCatelist()
  71. {
  72. $tree = Tree::instance();
  73. $list=$this->model->order('weigh desc,id desc')->where(function ($query){
  74. $type = $this->filter("type");
  75. if($type){
  76. $query->where('type',$type);
  77. }
  78. })->select()->toArray();
  79. $tree->init($list, 'pid');
  80. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  81. return $categorylist;
  82. }
  83. }