ArticleItem.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\ArticleIntro;
  4. use app\common\model\DatumIntro;
  5. use app\common\model\PlatformSwitch;
  6. use app\common\model\Supplier;
  7. use app\common\model\SupplierGoods;
  8. use app\common\model\User;
  9. use library\Controller;
  10. use app\common\model\SeriesArticleCate;
  11. /**
  12. * 图文管理
  13. * Class ArticleItem
  14. * @package app\Nutrition\controller
  15. */
  16. class ArticleItem extends Controller
  17. {
  18. /**
  19. * 绑定数据表
  20. * @var string
  21. */
  22. protected $table = 'ArticleItem';
  23. /**
  24. * 列表
  25. * @auth true
  26. * @menu true
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. * @throws \think\exception\PDOException
  32. */
  33. public function index()
  34. {
  35. $this->title = '图文列表';
  36. $this->article_id = input('article_id');
  37. $sel_where = [];
  38. $sel_where[] = ['is_deleted','=',0];
  39. $sel_where[] = ['article_id','=',$this->article_id];
  40. $query = $this->_query($this->table);
  41. $query->where($sel_where)->order('sort desc,id desc')->page(false);
  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. }
  55. /**
  56. * 添加
  57. * @auth true
  58. * @menu true
  59. * @throws \think\Exception
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @throws \think\exception\DbException
  63. * @throws \think\exception\PDOException
  64. */
  65. public function add()
  66. {
  67. $this->title = '添加图文';
  68. $this->_form($this->table, 'form');
  69. }
  70. /**
  71. * 编辑
  72. * @auth true
  73. * @menu true
  74. * @throws \think\Exception
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. * @throws \think\exception\PDOException
  79. */
  80. public function edit()
  81. {
  82. $this->title = '编辑图文';
  83. $this->_form($this->table, 'form') ;
  84. }
  85. /**
  86. * 删除图文
  87. * @auth true
  88. * @menu true
  89. * @throws \think\Exception
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. * @throws \think\exception\DbException
  93. * @throws \think\exception\PDOException
  94. */
  95. public function del()
  96. {
  97. $article_id = \app\common\model\ArticleItem::where('id',input('id'))->value('article_id');
  98. ArticleIntro::where('id',$article_id)->setDec('item_num');
  99. \app\common\model\ArticleItem::where('id',input('id'))->update(['is_deleted'=>1]);
  100. \app\common\model\ArticleItem::esAdd(input('id'));
  101. \app\common\model\TopSearch::saveData(input('id'),'article');
  102. $this->success('已删除!');
  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 remove()
  115. {
  116. $ids = input('id');
  117. foreach (explode(',',$ids) as $id) {
  118. $article_id = \app\common\model\ArticleItem::where('id',$id)->value('article_id');
  119. ArticleIntro::where('id',$article_id)->setDec('item_num');
  120. \app\common\model\ArticleItem::where('id',$id)->update(['is_deleted'=>1]);
  121. \app\common\model\ArticleItem::esAdd($id);
  122. \app\common\model\TopSearch::saveData($id,'article');
  123. }
  124. $this->success('已删除!');
  125. }
  126. /**
  127. * 表单数据处理
  128. * @auth true
  129. * @menu true
  130. * @param array $data
  131. */
  132. protected function _form_filter(&$data)
  133. {
  134. if($this->request->isGet() ){
  135. $this->article_id = input('article_id');
  136. $this->article_info = ArticleIntro::where('id',$this->article_id)->find()->toArray();
  137. // 视频
  138. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  139. ->where(['is_deleted'=>0])->order('id desc')
  140. ->select()->toArray();
  141. // 资料
  142. $this->datum_list = DatumIntro::with('urlArr')
  143. ->where(['is_deleted'=>0])
  144. ->where('status',1)
  145. ->order('id desc')
  146. ->select()->toArray();
  147. $this->supplier = Supplier::with('goodsList')
  148. ->where(['is_deleted'=>0])
  149. ->where('status',1)
  150. ->select()->toArray();
  151. $this->supplier_goods = SupplierGoods::getSupplierGoodsList($this->request->action() == 'add' ? ['s.is_deleted'=>0]: []);
  152. }
  153. if($this->request->isPost()){
  154. $is_vip = ArticleIntro::where('id',$this->request->post('article_id'))->value('is_vip');
  155. //if($is_vip) $data['is_vip'] = 1;
  156. if(empty($data['images'])) $this->error('请上传图片');
  157. $data['cover'] = explode('|',$data['images'])[0];
  158. if(!$data['release_time']) $data['release_time'] = date("Y-m-d H:i:s");
  159. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  160. if(!empty($data['phone'])) {
  161. $user_id = User::where('phone|email',$data['phone'])->value('id');
  162. if(!$user_id) $this->error('账号未注册');
  163. $data['user_id'] = $user_id;
  164. }else{
  165. $data['user_id'] = '';
  166. }
  167. if($data['content_type'] == 2) $data['content']='';
  168. }
  169. }
  170. protected function _form_result($result)
  171. {
  172. $article_num = \app\common\model\ArticleItem::field('id,read_num')->where(['article_id'=>$this->request->post('article_id'),'is_deleted'=>0])->select()->toArray();
  173. $article_info = ArticleIntro::where('id',$this->request->post('article_id'))->find()->toArray();
  174. $up = [];
  175. $up['item_num'] = count($article_num);
  176. if(!$article_info['cover']) $up['cover'] = explode('|',$this->request->post('images'))[0];
  177. if(!$article_info['video_id']) {
  178. $up['video_id'] = $this->request->post('video_id');
  179. $up['video_item'] = $this->request->post('video_item');
  180. $up['datum_id'] = $this->request->post('datum_id');
  181. $up['datum_item'] = $this->request->post('datum_item');
  182. }
  183. $up['read_num'] = array_sum(array_column($article_num,'read_num'));
  184. ArticleIntro::where('id',$this->request->post('article_id'))->update($up);
  185. \app\common\model\ArticleItem::esAdd($result);
  186. \app\common\model\TopSearch::saveData($result,'article');
  187. if($this->request->action() == 'add') PlatformSwitch::followMsg(3,$this->request->post('article_id'),$article_info['title'],$this->request->post('title'),$result);
  188. $this->success('操作成功', 'javascript:history.back()');
  189. }
  190. /**
  191. * 禁用
  192. * @auth true
  193. * @menu true
  194. * @throws \think\Exception
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\ModelNotFoundException
  197. * @throws \think\exception\DbException
  198. * @throws \think\exception\PDOException
  199. */
  200. public function forbid()
  201. {
  202. \app\common\model\ArticleItem::where('id',input('id'))->update(['status'=>0]);
  203. \app\common\model\ArticleItem::esAdd(input('id'));
  204. \app\common\model\TopSearch::saveData(input('id'),'article');
  205. $this->success('已禁用!');
  206. }
  207. /**
  208. * 启用
  209. * @auth true
  210. * @menu true
  211. * @throws \think\Exception
  212. * @throws \think\db\exception\DataNotFoundException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. * @throws \think\exception\DbException
  215. * @throws \think\exception\PDOException
  216. */
  217. public function resume()
  218. {
  219. \app\common\model\ArticleItem::where('id',input('id'))->update(['status'=>1]);
  220. \app\common\model\ArticleItem::esAdd(input('id'));
  221. \app\common\model\TopSearch::saveData(input('id'),'article');
  222. $this->success('已启用!');
  223. }
  224. /**
  225. * 富文本
  226. * @auth true
  227. * @menu true
  228. * @throws \think\Exception
  229. * @throws \think\db\exception\DataNotFoundException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. * @throws \think\exception\DbException
  232. * @throws \think\exception\PDOException
  233. */
  234. public function content()
  235. {
  236. $this->title = '编辑富文本';
  237. $this->_form($this->table, 'content') ;
  238. }
  239. }