Press.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\DatumIntro;
  4. use app\common\model\Supplier;
  5. use app\common\model\SupplierGoods;
  6. use app\common\model\User;
  7. use library\Controller;
  8. use think\Db;
  9. /**
  10. * 新闻
  11. * Class Press
  12. * @package app\operate\controller
  13. */
  14. class Press extends Controller
  15. {
  16. protected $table = 'Press';
  17. /**
  18. * 列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $this->title = '列表';
  30. $where = [];
  31. $where[] = ['f.is_deleted','=',0];
  32. if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%'];
  33. $query = $this->_query($this->table)->alias('f')
  34. ->field('f.*')
  35. ->where($where)
  36. ->order('sort desc,f.id desc')->page();
  37. }
  38. /**
  39. * 添加
  40. * @auth true
  41. * @menu true
  42. * @throws \think\Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. * @throws \think\exception\PDOException
  47. */
  48. public function add()
  49. {
  50. $this->title = '添加';
  51. $this->_form($this->table, 'form');
  52. }
  53. /**
  54. * 编辑
  55. * @auth true
  56. * @menu true
  57. * @throws \think\Exception
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @throws \think\exception\DbException
  61. * @throws \think\exception\PDOException
  62. */
  63. public function edit()
  64. {
  65. $this->title = '编辑';
  66. $this->_form($this->table, 'form');
  67. }
  68. /**
  69. * 删除
  70. * @auth true
  71. * @throws \think\Exception
  72. * @throws \think\exception\PDOException
  73. */
  74. public function del()
  75. {
  76. \app\common\model\Press::where('id',input('id'))->update(['is_deleted'=>1]);
  77. \app\common\model\Press::esAdd(input('id'));
  78. \app\common\model\TopSearch::saveData(input('id'),'press');
  79. $this->success('已删除!');
  80. }
  81. /**
  82. * 删除
  83. * @auth true
  84. * @throws \think\Exception
  85. * @throws \think\exception\PDOException
  86. */
  87. public function remove()
  88. {
  89. $ids = input('id');
  90. foreach (explode(',',$ids) as $id) {
  91. \app\common\model\Press::where('id',$id)->update(['is_deleted'=>1]);
  92. \app\common\model\Press::esAdd($id);
  93. \app\common\model\TopSearch::saveData($id,'press');
  94. }
  95. $this->success('已删除!');
  96. }
  97. /**
  98. * 启用
  99. * @auth true
  100. * @menu true
  101. * @throws \think\Exception
  102. * @throws \think\exception\PDOException
  103. */
  104. public function enable()
  105. {
  106. \app\common\model\Press::where('id',input('id'))->update(['status'=>1]);
  107. \app\common\model\Press::esAdd(input('id'));
  108. $this->success('已上架!');
  109. }
  110. /**
  111. * 禁用
  112. * @auth true
  113. * @menu true
  114. * @throws \think\Exception
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws \think\exception\DbException
  118. * @throws \think\exception\PDOException
  119. */
  120. public function forbidden()
  121. {
  122. \app\common\model\Press::where('id',input('id'))->update(['status'=>0]);
  123. \app\common\model\Press::esAdd(input('id'));
  124. \app\common\model\TopSearch::saveData(input('id'),'press');
  125. $this->success('已下架!');
  126. }
  127. protected function _form_filter(&$data){
  128. // 文章列表
  129. $this->article_list =\app\common\model\ArticleIntro::with('itemChildren')
  130. ->field('id,title')
  131. ->where(['is_deleted'=>0])->order('id asc')
  132. ->select()->toArray();
  133. // 视频
  134. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  135. ->where(['is_deleted'=>0])->order('id desc')
  136. ->select()->toArray();
  137. // 资料
  138. $this->datum_list = DatumIntro::with('urlArr')
  139. ->where(['is_deleted'=>0])->order('id desc')
  140. ->select()->toArray();
  141. $this->supplier = Supplier::with('goodsList')
  142. ->where(['is_deleted'=>0])
  143. ->where('status',1)
  144. ->select()->toArray();
  145. $this->supplier_goods = SupplierGoods::getSupplierGoodsList($this->request->action() == 'add' ? ['s.is_deleted'=>0,'g.is_deleted'=>0]: ['s.is_deleted'=>0,'g.is_deleted'=>0]);
  146. if($this->request->isPost())
  147. {
  148. if(empty($data['title']))$this->error('请输入标题');
  149. if(empty($data['images']))$this->error('请上传封面');
  150. if(!$data['release_time']) $data['release_time'] = date("Y-m-d H:i:s");
  151. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  152. if(!empty($data['phone'])) {
  153. $user_id = User::where('phone|email',$data['phone'])->value('id');
  154. if(!$user_id) $this->error('账号未注册');
  155. $data['user_id'] = $user_id;
  156. }else{
  157. $data['user_id'] = '';
  158. }
  159. list($post) = [$this->request->post()];
  160. //定时热搜
  161. if(!$post['hot_num']){
  162. $post['hot_num'] = 0;
  163. }
  164. if(isset($post['id'])){
  165. $info = \app\common\model\Press::where('id',$data['id'])->find();
  166. 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'])){
  167. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  168. $startdate = strtotime($data['regular_hot_start_time']);
  169. $enddate = strtotime($post['regular_hot_end_time']);
  170. $diff_seconds = ($enddate-$startdate)/60;
  171. $min_num = ceil($diff_seconds/10);
  172. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  173. $num = ceil($hot_num/$min_num);
  174. $data['regular_num'] = $num;
  175. }
  176. }else{
  177. if($post['regular_hot_end_time'] && $post['hot_target_num']){
  178. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  179. $startdate = strtotime($data['regular_hot_start_time']);
  180. $enddate = strtotime($post['regular_hot_end_time']);
  181. $diff_seconds = ($enddate-$startdate)/60;
  182. $min_num = ceil($diff_seconds/10);
  183. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  184. $num = ceil($hot_num/$min_num);
  185. $data['regular_num'] = $num;
  186. }
  187. }
  188. //定时热搜end
  189. }
  190. }
  191. protected function _form_result(&$data)
  192. {
  193. \app\common\model\Press::esAdd($data);
  194. \app\common\model\TopSearch::saveData($data,'press');
  195. $this->success('操作成功', 'javascript:history.back()');
  196. }
  197. }