OneArticle.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $query = $this->_query($this->table);
  42. $query->where($sel_where)->order('sort desc,id desc')->page();
  43. }
  44. /**
  45. * 数据列表处理
  46. * @auth true
  47. * @menu true
  48. * @param array $data
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. protected function _index_page_filter(&$data)
  54. {
  55. foreach ($data as &$v) {
  56. $v['item_id'] = \app\common\model\ArticleItem::where(['article_id'=>$v['id']])->value('id');
  57. }
  58. }
  59. /**
  60. * 添加
  61. * @auth true
  62. * @menu true
  63. * @throws \think\Exception
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. * @throws \think\exception\PDOException
  68. */
  69. public function add()
  70. {
  71. $this->title = '添加文章';
  72. $this->all_cate = ArticleCate::field('id,title')->where('is_deleted',0)->select();
  73. $this->_form($this->table, 'form');
  74. }
  75. /**
  76. * 编辑
  77. * @auth true
  78. * @menu true
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. * @throws \think\exception\PDOException
  84. */
  85. public function edit()
  86. {
  87. $this->title = '编辑文章';
  88. $this->_form($this->table, 'form');
  89. }
  90. /**
  91. * 禁用
  92. * @auth true
  93. * @menu true
  94. * @throws \think\Exception
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. * @throws \think\exception\PDOException
  99. */
  100. public function forbidden()
  101. {
  102. $this->_save($this->table, ['status' => '0']);
  103. }
  104. /**
  105. * 启用
  106. * @auth true
  107. * @menu true
  108. * @throws \think\Exception
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. * @throws \think\exception\PDOException
  113. */
  114. public function enable()
  115. {
  116. $this->_save($this->table, ['status' => 1]);
  117. }
  118. /**
  119. * 删除
  120. * @auth true
  121. * @menu true
  122. * @throws \think\Exception
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. * @throws \think\exception\PDOException
  127. */
  128. public function del()
  129. {
  130. $this->_save($this->table, ['is_deleted' => 1]);
  131. }
  132. /**
  133. * 表单数据处理
  134. * @auth true
  135. * @menu true
  136. * @param array $data
  137. */
  138. protected function _form_filter(&$data)
  139. {
  140. if($this->request->isGet()){
  141. $all_cate = ArticleCate::where(['is_deleted'=>0])->order('sort desc ,id desc')->select();
  142. $this->r = input('get.r',0);
  143. $this->cate_tree = make_tree($all_cate);
  144. // 视频
  145. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  146. ->where(['is_deleted'=>0])->order('id desc')
  147. ->select()->toArray();
  148. // 资料
  149. $this->datum_list = DatumIntro::with('urlArr')
  150. ->where(['is_deleted'=>0])->order('id desc')
  151. ->select()->toArray();
  152. $this->phone = \app\common\model\ArticleItem::where('article_id',input('id'))->value('phone');
  153. }
  154. if($this->request->isPost())
  155. {
  156. if(!$data['images']) $this->error('请上传图片');
  157. $data['cover'] = explode('|',$data['images'])[0];
  158. if(!empty($data['phone'])) {
  159. $user_id = User::where('phone|email',$data['phone'])->value('id');
  160. if(!$user_id) $this->error('账号未注册');
  161. }
  162. }
  163. }
  164. protected function _form_result($result){
  165. $article_info = ArticleIntro::where('id',$result)->find()->toArray();
  166. list($data) = [$this->request->post()];
  167. if(!empty($data['phone'])) {
  168. $user_id = User::where('phone|email',$data['phone'])->value('id');
  169. }else{
  170. $user_id= '';
  171. }
  172. $item = [
  173. 'article_id' => $article_info['id'],
  174. 'title' => $article_info['title'],
  175. 'cover' => $article_info['cover'],
  176. 'content' => $article_info['content'],
  177. 'is_vip' => $article_info['is_vip'],
  178. 'images' => $article_info['images'],
  179. 'video_id' => $data['video_id'],
  180. 'video_item' => $data['video_item'],
  181. 'datum_id' => isset($data['datum_id']) ?$data['datum_id'] : 0,
  182. 'datum_item' => isset($data['datum_item']) ?$data['datum_item']:0,
  183. 'phone' => isset($data['phone']) ?$data['phone']:'',
  184. 'user_id' => $user_id
  185. ];
  186. Data::save('ArticleItem',$item,'article_id',['article_id'=>$article_info['id']]);
  187. $this->success('操作成功!', 'javascript:history.back()');
  188. }
  189. }