OneArticle.php 7.2 KB

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