Post.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\admin\controller\blog;
  3. use app\admin\model\BlogCategory;
  4. use app\common\controller\Backend;
  5. use fast\Tree;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. * 文章管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Post extends Backend
  14. {
  15. /**
  16. * BlogPost模型对象
  17. */
  18. protected $model = null;
  19. protected $categorylist = [];
  20. protected $noNeedRight = ['selectpage', 'check_element_available'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('BlogPost');
  25. $this->view->assign("flagList", $this->model->getFlagList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $tree = Tree::instance();
  28. $tree->init(collection(BlogCategory::order('weigh desc,id desc')->select())->toArray(), 'pid');
  29. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  30. $categorydata = [];
  31. foreach ($this->categorylist as $k => $v) {
  32. $categorydata[$v['id']] = $v;
  33. }
  34. $this->view->assign("categoryList", $categorydata);
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. $this->relationSearch = true;
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $total = $this->model
  51. ->with('category')
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->count();
  55. $list = $this->model
  56. ->with('category')
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->select();
  61. $result = array("total" => $total, "rows" => $list);
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 检测元素是否可用
  68. * @internal
  69. */
  70. public function check_element_available()
  71. {
  72. $id = $this->request->request('id');
  73. $name = $this->request->request('name');
  74. $value = $this->request->request('value');
  75. $name = substr($name, 4, -1);
  76. if (!$name) {
  77. $this->error(__('Parameter %s can not be empty', 'name'));
  78. }
  79. if ($id) {
  80. $this->model->where('id', '<>', $id);
  81. }
  82. $exist = $this->model->where($name, $value)->find();
  83. if ($exist) {
  84. $this->error(__('The data already exist'));
  85. } else {
  86. $this->success();
  87. }
  88. }
  89. }