NewsManage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\NewsChannel;
  4. use app\common\model\NutritionCase;
  5. use library\Controller;
  6. use think\Db;
  7. use app\common\model\NewsCate as NTC;
  8. /**
  9. * 资讯管理
  10. * Class NewsManage
  11. * @package app\Nutrition\controller
  12. */
  13. class NewsManage extends Controller
  14. {
  15. /**
  16. * 绑定数据表
  17. * @var string
  18. */
  19. protected $table = 'InformationArticle';
  20. /**
  21. * 列表
  22. * @auth true
  23. * @menu true
  24. * @throws \think\Exception
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. * @throws \think\exception\DbException
  28. * @throws \think\exception\PDOException
  29. */
  30. public function index()
  31. {
  32. $this->title = '资讯列表';
  33. $sel_where = [];
  34. $sel_where[] = ['is_deleted','=',0];
  35. if($title = $this->request->get('title')) $sel_where[] = ['title','like','%'.$title.'%'];
  36. if($type = $this->request->get('type')) $sel_where[] = ['type','=',$type];
  37. $query = $this->_query($this->table);
  38. $query->where($sel_where)->order('status desc ,is_top desc ,sort desc,id desc')->page();
  39. }
  40. /**
  41. * 数据列表处理
  42. * @auth true
  43. * @menu true
  44. * @param array $data
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. protected function _index_page_filter(&$data)
  50. {
  51. }
  52. /**
  53. * 添加
  54. * @auth true
  55. * @menu true
  56. * @throws \think\Exception
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @throws \think\exception\DbException
  60. * @throws \think\exception\PDOException
  61. */
  62. public function add()
  63. {
  64. $this->title = '添加资讯';
  65. $this->all_cate = NTC::field('id,title')->where('is_deleted',0)->select();
  66. $this->_form($this->table, 'form');
  67. }
  68. /**
  69. * 编辑
  70. * @auth true
  71. * @menu true
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. * @throws \think\exception\PDOException
  77. */
  78. public function edit()
  79. {
  80. $this->title = '编辑资讯';
  81. $this->_form($this->table, 'form');
  82. }
  83. /**
  84. * 禁用
  85. * @auth true
  86. * @menu true
  87. * @throws \think\Exception
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. * @throws \think\exception\PDOException
  92. */
  93. public function forbidden()
  94. {
  95. $this->_save($this->table, ['status' => '0']);
  96. }
  97. /**
  98. * 启用
  99. * @auth true
  100. * @menu true
  101. * @throws \think\Exception
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @throws \think\exception\DbException
  105. * @throws \think\exception\PDOException
  106. */
  107. public function enable()
  108. {
  109. $this->_save($this->table, ['status' => 1]);
  110. }
  111. /**
  112. * 删除
  113. * @auth true
  114. * @menu true
  115. * @throws \think\Exception
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. * @throws \think\exception\PDOException
  120. */
  121. public function del()
  122. {
  123. $this->_save($this->table, ['is_deleted' => 1]);
  124. }
  125. /**
  126. * 表单数据处理
  127. * @auth true
  128. * @menu true
  129. * @param array $data
  130. */
  131. protected function _form_filter(&$data)
  132. {
  133. if($this->request->isGet()){
  134. $all_cate = NTC::where(['is_deleted'=>0])->order('sort desc ,id desc')->select();
  135. $this->cate_tree = make_tree($all_cate);
  136. //选中的标签
  137. $this->label_arr = isset($data['label']) ? explode('|',trim($data['label'],'|')) : [];
  138. $this->case = NutritionCase::column('title','id');
  139. $this->news_channel = NewsChannel::where(['is_deleted'=>0,'status'=>1])->order('sort desc ,id desc')->column('title','id');
  140. }
  141. if($this->request->isPost())
  142. {
  143. if($data['type'] == 1 && !$data['cover']){
  144. $this->error('请上传封面');
  145. $data['video_url'] = '';
  146. }
  147. if($data['type'] == 2 && !$data['video_url']){
  148. $this->error('请上传视频');
  149. $data['detail'] = '';
  150. }
  151. // 服务标签
  152. $label_sever = [];
  153. if(isset($data['label']) && !empty($data['label'])){
  154. foreach ($data['label'] as $key=>$value){
  155. if($value) $label_sever[] = $key;
  156. }
  157. }
  158. $data['label'] = '|'.implode('|',$label_sever).'|';
  159. }
  160. }
  161. protected function _form_result($result){
  162. $this->success('商品编辑成功!', 'javascript:history.back()');
  163. }
  164. }