ArticleItem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. list($post) = [$this->request->post()];
  155. $is_vip = ArticleIntro::where('id',$this->request->post('article_id'))->value('is_vip');
  156. //if($is_vip) $data['is_vip'] = 1;
  157. if(empty($data['images'])) $this->error('请上传图片');
  158. $data['cover'] = explode('|',$data['images'])[0];
  159. if(!$data['release_time']) $data['release_time'] = date("Y-m-d H:i:s");
  160. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  161. if(!empty($data['phone'])) {
  162. $user_id = User::where('phone|email',$data['phone'])->value('id');
  163. if(!$user_id) $this->error('账号未注册');
  164. $data['user_id'] = $user_id;
  165. }else{
  166. $data['user_id'] = '';
  167. }
  168. //定时热搜
  169. if(!$post['hot_num']){
  170. $post['hot_num'] = 0;
  171. }
  172. if(isset($post['id'])){
  173. $info = \app\common\model\ArticleItem::where('id',$data['id'])->find();
  174. if(($post['regular_hot_end_time'] && $post['hot_target_num'] && $info['regular_hot_end_time'] != $post['regular_hot_end_time']) || ($info['hot_num'] != $post['hot_num'] && $post['regular_hot_end_time'] && $post['hot_target_num'])){
  175. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  176. $startdate = strtotime($data['regular_hot_start_time']);
  177. $enddate = strtotime($post['regular_hot_end_time']);
  178. $diff_seconds = ($enddate-$startdate)/60;
  179. $min_num = ceil($diff_seconds/10);
  180. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  181. $num = ceil($hot_num/$min_num);
  182. if($num < 0){
  183. $num = 0;
  184. }
  185. $data['regular_num'] = $num;
  186. }
  187. }else{
  188. if($post['regular_hot_end_time'] && $post['hot_target_num']){
  189. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  190. $startdate = strtotime($data['regular_hot_start_time']);
  191. $enddate = strtotime($post['regular_hot_end_time']);
  192. $diff_seconds = ($enddate-$startdate)/60;
  193. $min_num = ceil($diff_seconds/10);
  194. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  195. $num = ceil($hot_num/$min_num);
  196. if($num < 0){
  197. $num = 0;
  198. }
  199. $data['regular_num'] = $num;
  200. }
  201. }
  202. if(!$post['regular_hot_end_time']){
  203. unset($data['regular_hot_end_time']);
  204. }
  205. //定时热搜end
  206. if($data['content_type'] == 2) $data['content']='';
  207. }
  208. }
  209. protected function _form_result($result)
  210. {
  211. $article_num = \app\common\model\ArticleItem::field('id,read_num')->where(['article_id'=>$this->request->post('article_id'),'is_deleted'=>0])->select()->toArray();
  212. $article_info = ArticleIntro::where('id',$this->request->post('article_id'))->find()->toArray();
  213. $up = [];
  214. $up['item_num'] = count($article_num);
  215. if(!$article_info['cover']) $up['cover'] = explode('|',$this->request->post('images'))[0];
  216. if(!$article_info['video_id']) {
  217. $up['video_id'] = $this->request->post('video_id');
  218. $up['video_item'] = $this->request->post('video_item');
  219. $up['datum_id'] = $this->request->post('datum_id');
  220. $up['datum_item'] = $this->request->post('datum_item');
  221. }
  222. $up['read_num'] = array_sum(array_column($article_num,'read_num'));
  223. ArticleIntro::where('id',$this->request->post('article_id'))->update($up);
  224. \app\common\model\ArticleItem::esAdd($result);
  225. \app\common\model\TopSearch::saveData($result,'article');
  226. if($this->request->action() == 'add') PlatformSwitch::followMsg(3,$this->request->post('article_id'),$article_info['title'],$this->request->post('title'),$result);
  227. $this->success('操作成功', 'javascript:history.back()');
  228. }
  229. /**
  230. * 禁用
  231. * @auth true
  232. * @menu true
  233. * @throws \think\Exception
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\ModelNotFoundException
  236. * @throws \think\exception\DbException
  237. * @throws \think\exception\PDOException
  238. */
  239. public function forbid()
  240. {
  241. \app\common\model\ArticleItem::where('id',input('id'))->update(['status'=>0]);
  242. \app\common\model\ArticleItem::esAdd(input('id'));
  243. \app\common\model\TopSearch::saveData(input('id'),'article');
  244. $this->success('已禁用!');
  245. }
  246. /**
  247. * 启用
  248. * @auth true
  249. * @menu true
  250. * @throws \think\Exception
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. * @throws \think\exception\DbException
  254. * @throws \think\exception\PDOException
  255. */
  256. public function resume()
  257. {
  258. \app\common\model\ArticleItem::where('id',input('id'))->update(['status'=>1]);
  259. \app\common\model\ArticleItem::esAdd(input('id'));
  260. \app\common\model\TopSearch::saveData(input('id'),'article');
  261. $this->success('已启用!');
  262. }
  263. /**
  264. * 富文本
  265. * @auth true
  266. * @menu true
  267. * @throws \think\Exception
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. * @throws \think\exception\DbException
  271. * @throws \think\exception\PDOException
  272. */
  273. public function content()
  274. {
  275. $this->title = '编辑富文本';
  276. $this->_form($this->table, 'content') ;
  277. }
  278. }