OneArticle.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\ArticleIntro;
  4. use app\common\model\DatumIntro;
  5. use library\Controller;
  6. use library\tools\Data;
  7. use think\Db;
  8. use app\common\model\ArticleCate;
  9. /**
  10. * 文章管理
  11. * Class NewsManage
  12. * @package app\Nutrition\controller
  13. */
  14. class OneArticle extends Controller
  15. {
  16. /**
  17. * 绑定数据表
  18. * @var string
  19. */
  20. protected $table = 'ArticleIntro';
  21. /**
  22. * 列表
  23. * @auth true
  24. * @menu true
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. * @throws \think\exception\PDOException
  30. */
  31. public function index()
  32. {
  33. $this->title = '文章列表';
  34. $sel_where = [];
  35. $sel_where[] = ['is_deleted','=',0];
  36. $sel_where[] = ['type','=',1];
  37. if($title = $this->request->get('title')) $sel_where[] = ['title','like','%'.$title.'%'];
  38. $this->is_vip = $is_vip = $this->request->get('is_vip',-1);
  39. if($is_vip >= 0) $sel_where[] = ['is_vip','=',$is_vip ];
  40. $query = $this->_query($this->table);
  41. $query->where($sel_where)->order('status desc ,is_top desc ,sort desc,id desc')->page();
  42. }
  43. /**
  44. * 数据列表处理
  45. * @auth true
  46. * @menu true
  47. * @param array $data
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. protected function _index_page_filter(&$data)
  53. {
  54. foreach ($data as &$v) {
  55. $v['item_id'] = \app\common\model\ArticleItem::where(['article_id'=>$v['id']])->value('id');
  56. }
  57. }
  58. /**
  59. * 添加
  60. * @auth true
  61. * @menu true
  62. * @throws \think\Exception
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. * @throws \think\exception\PDOException
  67. */
  68. public function add()
  69. {
  70. $this->title = '添加文章';
  71. $this->all_cate = ArticleCate::field('id,title')->where('is_deleted',0)->select();
  72. $this->_form($this->table, 'form');
  73. }
  74. /**
  75. * 编辑
  76. * @auth true
  77. * @menu true
  78. * @throws \think\Exception
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. * @throws \think\exception\PDOException
  83. */
  84. public function edit()
  85. {
  86. $this->title = '编辑文章';
  87. $this->_form($this->table, 'form');
  88. }
  89. /**
  90. * 禁用
  91. * @auth true
  92. * @menu true
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. * @throws \think\exception\PDOException
  98. */
  99. public function forbidden()
  100. {
  101. $this->_save($this->table, ['status' => '0']);
  102. }
  103. /**
  104. * 启用
  105. * @auth true
  106. * @menu true
  107. * @throws \think\Exception
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. * @throws \think\exception\PDOException
  112. */
  113. public function enable()
  114. {
  115. $this->_save($this->table, ['status' => 1]);
  116. }
  117. /**
  118. * 删除
  119. * @auth true
  120. * @menu true
  121. * @throws \think\Exception
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. * @throws \think\exception\PDOException
  126. */
  127. public function del()
  128. {
  129. $this->_save($this->table, ['is_deleted' => 1]);
  130. }
  131. /**
  132. * 表单数据处理
  133. * @auth true
  134. * @menu true
  135. * @param array $data
  136. */
  137. protected function _form_filter(&$data)
  138. {
  139. if($this->request->isGet()){
  140. $all_cate = ArticleCate::where(['is_deleted'=>0])->order('sort desc ,id desc')->select();
  141. $this->r = input('get.r',0);
  142. $this->cate_tree = make_tree($all_cate);
  143. // 视频
  144. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  145. ->where(['is_deleted'=>0])->order('id desc')
  146. ->select()->toArray();
  147. // 资料
  148. $this->datum_list = DatumIntro::with('urlArr')
  149. ->where(['is_deleted'=>0])->order('id desc')
  150. ->select()->toArray();
  151. }
  152. if($this->request->isPost())
  153. {
  154. if(!$data['images']) $this->error('请上传图片');
  155. $data['cover'] = explode('|',$data['images'])[0];
  156. }
  157. }
  158. protected function _form_result($result){
  159. $article_info = ArticleIntro::where('id',$result)->find()->toArray();
  160. list($data) = [$this->request->post()];
  161. $item = [
  162. 'article_id' => $article_info['id'],
  163. 'title' => $article_info['title'],
  164. 'cover' => $article_info['cover'],
  165. 'content' => $article_info['content'],
  166. 'is_vip' => $article_info['is_vip'],
  167. 'images' => $article_info['images'],
  168. 'video_id' => $data['video_id'],
  169. 'video_item' => $data['video_item'],
  170. 'datum_id' => isset($data['datum_id']) ?$data['datum_id'] : 0,
  171. 'datum_item' => isset($data['datum_item']) ?$data['datum_item']:0,
  172. 'user_id' => isset($data['user_id']) ?$data['user_id']:0
  173. ];
  174. Data::save('ArticleItem',$item,'article_id',['article_id'=>$article_info['id']]);
  175. $this->success('操作成功!', 'javascript:history.back()');
  176. }
  177. }