NewsCate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use library\Controller;
  4. use think\Db;
  5. use app\common\model\NewsCate as NTC;
  6. /**
  7. * 营养百科分类
  8. * Class NewsCate
  9. * @package app\Nutrition\controller
  10. */
  11. class NewsCate extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. protected $table = 'NewsCate';
  18. /**
  19. * 分类列表
  20. * @auth true
  21. * @menu true
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * @throws \think\exception\PDOException
  27. */
  28. public function index()
  29. {
  30. $this->title = '分类列表';
  31. $sel_where = [];
  32. $sel_where[] = ['is_deleted','=',0];
  33. $sel_where[] = ['pid','=',0];
  34. if($title = $this->request->get('title')) $sel_where[] = ['title','like','%'.$title.'%'];
  35. $query = $this->_query($this->table)->where($sel_where);
  36. $query->dateBetween('create_at')->order('status desc ,sort desc , id desc')->page();
  37. }
  38. /**
  39. * 数据列表处理
  40. * @auth true
  41. * @menu true
  42. * @param array $data
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. protected function _index_page_filter(&$data)
  48. {
  49. foreach ($data as $k=>&$v){
  50. $v['children'] = NTC::where(['pid'=>$v['id'],'is_deleted'=>0])
  51. ->order('status desc ,sort desc , id desc')
  52. ->select();
  53. }
  54. }
  55. /**
  56. * 添加分类
  57. * @auth true
  58. * @menu true
  59. * @throws \think\Exception
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @throws \think\exception\DbException
  63. * @throws \think\exception\PDOException
  64. */
  65. public function add()
  66. {
  67. $this->title = '添加分类';
  68. $this->all_cate = NTC::field('id,title')->where('is_deleted',0)->select();
  69. $this->_form($this->table, 'form');
  70. }
  71. /**
  72. * 编辑分类
  73. * @auth true
  74. * @menu true
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. * @throws \think\exception\PDOException
  80. */
  81. public function edit()
  82. {
  83. $this->title = '编辑分类';
  84. $this->_form($this->table, 'form');
  85. }
  86. /**
  87. * 禁用分类
  88. * @auth true
  89. * @menu true
  90. * @throws \think\Exception
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. * @throws \think\exception\PDOException
  95. */
  96. public function forbidden()
  97. {
  98. $this->_save($this->table, ['status' => '0']);
  99. }
  100. /**
  101. * 启用分类
  102. * @auth true
  103. * @menu true
  104. * @throws \think\Exception
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @throws \think\exception\DbException
  108. * @throws \think\exception\PDOException
  109. */
  110. public function enable()
  111. {
  112. $this->_save($this->table, ['status' => 1]);
  113. }
  114. /**
  115. * 删除分类
  116. * @auth true
  117. * @menu true
  118. * @throws \think\Exception
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. * @throws \think\exception\DbException
  122. * @throws \think\exception\PDOException
  123. */
  124. public function del()
  125. {
  126. $this->_save($this->table, ['is_deleted' => 1]);
  127. }
  128. /**
  129. * 表单数据处理
  130. * @auth true
  131. * @menu true
  132. * @param array $data
  133. */
  134. protected function _form_filter(&$data)
  135. {
  136. if($this->request->get()){
  137. $this->pid = input('pid',0);
  138. $this->pname = NTC::where(['id'=>$this->pid])->value('title');
  139. $this->is_desc = 1;
  140. if($this->request->action() == 'add'){
  141. $this->is_desc = $this->pid ?1:0;
  142. }else{
  143. $this->is_desc = $data['pid'] > 0 ? 1:0;
  144. }
  145. }
  146. }
  147. }