ArticleItem.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 app\common\model\SeriesArticleCate;
  8. /**
  9. * 图文管理
  10. * Class ArticleItem
  11. * @package app\Nutrition\controller
  12. */
  13. class ArticleItem extends Controller
  14. {
  15. /**
  16. * 绑定数据表
  17. * @var string
  18. */
  19. protected $table = 'ArticleItem';
  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. $this->article_id = input('article_id');
  34. $sel_where = [];
  35. $sel_where[] = ['is_deleted','=',0];
  36. $sel_where[] = ['article_id','=',$this->article_id];
  37. $query = $this->_query($this->table);
  38. $query->where($sel_where)->order('sort desc,id asc')->page(false);
  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->_form($this->table, 'form');
  66. }
  67. /**
  68. * 编辑
  69. * @auth true
  70. * @menu true
  71. * @throws \think\Exception
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. * @throws \think\exception\PDOException
  76. */
  77. public function edit()
  78. {
  79. $this->title = '编辑图文';
  80. $this->_form($this->table, 'form') ;
  81. }
  82. /**
  83. * 删除图文
  84. * @auth true
  85. * @menu true
  86. * @throws \think\Exception
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. * @throws \think\exception\PDOException
  91. */
  92. public function del()
  93. {
  94. $article_id = \app\common\model\ArticleItem::where('id',input('id'))->value('article_id');
  95. ArticleIntro::where('id',$article_id)->setDec('item_num');
  96. $this->_save($this->table, ['is_deleted' => 1]);
  97. }
  98. /**
  99. * 表单数据处理
  100. * @auth true
  101. * @menu true
  102. * @param array $data
  103. */
  104. protected function _form_filter(&$data)
  105. {
  106. if($this->request->isGet()){
  107. $this->article_id = input('article_id');
  108. $this->article_info = ArticleIntro::where('id',$this->article_id)->find()->toArray();
  109. // 视频
  110. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  111. ->where(['is_deleted'=>0])->order('id desc')
  112. ->select()->toArray();
  113. // 资料
  114. $this->datum_list = DatumIntro::with('urlArr')
  115. ->where(['is_deleted'=>0])->order('id desc')
  116. ->select()->toArray();
  117. }
  118. if($this->request->isPost()){
  119. $is_vip = ArticleIntro::where('id',$this->request->post('article_id'))->value('is_vip');
  120. if($is_vip) $data['is_vip'] = 1;
  121. if(empty($data['images'])) $this->error('请上传图片');
  122. $data['cover'] = explode('|',$data['images'])[0];
  123. if(!empty($data['phone'])) {
  124. $user_id = User::where('phone|email',$data['phone'])->value('id');
  125. if(!$user_id) $this->error('账号未注册');
  126. $data['user_id'] = $user_id;
  127. }else{
  128. $data['user_id'] = '';
  129. }
  130. if($data['content_type'] == 2) $data['content']='';
  131. }
  132. }
  133. protected function _form_result($result)
  134. {
  135. $article_num = \app\common\model\ArticleItem::field('id,read_num')->where(['article_id'=>$this->request->post('article_id'),'is_deleted'=>0])->select()->toArray();
  136. $article_info = ArticleIntro::where('id',$this->request->post('article_id'))->find()->toArray();
  137. $up = [];
  138. $up['item_num'] = count($article_num);
  139. if(!$article_info['cover']) $up['cover'] = explode('|',$this->request->post('images'))[0];
  140. if(!$article_info['video_id']) {
  141. $up['video_id'] = $this->request->post('video_id');
  142. $up['video_item'] = $this->request->post('video_item');
  143. $up['datum_id'] = $this->request->post('datum_id');
  144. $up['datum_item'] = $this->request->post('datum_item');
  145. }
  146. $up['read_num'] = array_sum(array_column($article_num,'read_num'));
  147. ArticleIntro::where('id',$this->request->post('article_id'))->update($up);
  148. $this->success('操作成功', 'javascript:history.back()');
  149. }
  150. /**
  151. * 禁用
  152. * @auth true
  153. * @menu true
  154. * @throws \think\Exception
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. * @throws \think\exception\DbException
  158. * @throws \think\exception\PDOException
  159. */
  160. public function forbid()
  161. {
  162. $this->_save($this->table, ['status' => '0']);
  163. }
  164. /**
  165. * 启用
  166. * @auth true
  167. * @menu true
  168. * @throws \think\Exception
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. * @throws \think\exception\PDOException
  173. */
  174. public function resume()
  175. {
  176. $this->_save($this->table, ['status' => 1]);
  177. }
  178. }