Index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\api\controller\Base;
  16. use think\Db;
  17. use think\Model;
  18. /**
  19. * @title 首页(新闻)管理
  20. * @controller Index
  21. * @group base
  22. */
  23. class Index extends Base
  24. {
  25. /**
  26. * @title 新闻分类
  27. * @desc 新闻分类
  28. * @author HG
  29. * @url /api/Index/cate
  30. * @method GET
  31. * @tag 新闻分类
  32. * @return name:-- type:array default:-- desc:新闻列表(id:新闻ID,title:新闻标题)
  33. */
  34. public function cate(){
  35. $cate = Db::name('store_goods_cate')->field('id,title')->where('status',1)->where('is_deleted',0)->order('sort','desc')->select();
  36. $this->success('获取成功',$cate);
  37. }
  38. /**
  39. * @title 新闻列表
  40. * @desc 新闻列表
  41. * @author HG
  42. * @url /api/Index/news_list
  43. * @method GET
  44. * @tag 新闻列表
  45. * @header name:Authorization require:1 desc:Token
  46. * @param name:id type:int require:0 default:-- desc:新闻分类ID(搜索不用穿,其他必须传)
  47. * @param name:last_id type:int require:0 default:1 desc:最新的新闻id(为获取新的新闻数量)
  48. * @param name:keyword type:string require:0 default:-- desc:搜索的关键词
  49. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  50. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  51. * @return name:last_id type:string default:-- desc:最后一个新闻的id(用于下次刷新获取更新的新闻数量)
  52. * @return name:num type:string default:-- desc:新增新闻的数量
  53. * @return name:list type:array default:-- desc:新闻列表(id:新闻ID,title:新闻标题,image:新闻图片(数组格式),video:视频地址,comment_num:评论数量,is_top:是否置顶(0:不置顶,1:置顶),media_logo:媒体图标,media_title:媒体名称,duration:发布时长)
  54. */
  55. public function news_list(){
  56. $id = input('id');
  57. $last_id = input('last_id');
  58. $keyword = input('keyword');
  59. $page = input('page',1);
  60. $pageSize = input('page_size',10);
  61. if($keyword){
  62. $where = "title like '%".$keyword."%'";
  63. //保存搜索的关键词
  64. $uid = $this->get_uid();
  65. if($uid){
  66. $search_id = Db::name('store_search_keyword')->where('user_id',$uid)->where('type',1)->where('name',$keyword)->value('id');
  67. if(empty($search_id)){
  68. Db::name('store_search_keyword')->insert(array('user_id' => $uid, 'name' => $keyword));
  69. }
  70. }
  71. }else{
  72. $where = "id > 0";
  73. }
  74. if($id){
  75. $where .= " and cate_id = ".$id;
  76. }
  77. $field = 'id,title,user_id,image,video,is_top,create_at';
  78. $list = Db::name('store_goods')->field($field)->where('status',1)->where('is_deleted',0)->where($where)->page($page,$pageSize)->order(['is_top'=>'desc','sort'=>'desc','id'=>'desc'])->select();
  79. //新的新闻数量
  80. $num = 0;
  81. if($last_id){
  82. $num = Db::name('store_goods')->field($field)->where('status',1)->where('cate_id',$id)->where('is_deleted',0)->where('id','>',$last_id)->count('id');
  83. }
  84. //获取最大ID
  85. $result_last_id = Db::name('store_goods')->field($field)->where('status',1)->where('cate_id',$id)->where('is_deleted',0)->order('id desc')->value('id');
  86. if($list){
  87. foreach($list as &$value) {
  88. //获取评论数量
  89. $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
  90. $value['image'] = image_path($value['image']);
  91. //媒体信息
  92. $media_info = Db::name('store_media')->field('logo,title')->where('id',$value['user_id'])->find();
  93. $value['media_logo'] = $media_info['logo'];
  94. $value['media_title'] = $media_info['title'];
  95. $remain_time = time() - strtotime($value['create_at']);
  96. $value['duration'] = get_stay_time($remain_time);
  97. unset($value['user_id']);
  98. unset($value['create_at']);
  99. }
  100. }
  101. $data = array(
  102. 'last_id' => $result_last_id?$result_last_id:0,
  103. 'num' => $num,
  104. 'list' => $list
  105. );
  106. $this->success('获取成功',$data);
  107. }
  108. /**
  109. * @title 新闻详情
  110. * @desc 新闻详情
  111. * @author HG
  112. * @url /api/Index/news_detail
  113. * @method GET
  114. * @tag 新闻详情
  115. * @header name:Authorization require:1 desc:Token
  116. * @param name:id type:int require:1 default:-- desc:新闻ID
  117. * @return name:id type:int default:-- desc:新闻的id
  118. * @return name:title type:string default:-- desc:新闻标题
  119. * @return name:user_id type:int default:-- desc:媒体ID
  120. * @return name:content type:string default:-- desc:新增内容(富文本)
  121. * @return name:comment_num type:int default:-- desc:评论数量
  122. * @return name:like_num type:int default:-- desc:点赞数量
  123. * @return name:transpond_num type:int default:-- desc:转发数量
  124. * @return name:is_attention type:int default:-- desc:是否关注媒体(0:未关注,1:已关注)
  125. * @return name:is_like type:int default:-- desc:是否点赞(0:未点赞,1:已点赞)
  126. * @return name:is_collect type:int default:-- desc:是否收藏(0:未收藏,1:已收藏)
  127. * @return name:media_logo type:string default:-- desc:媒体logo
  128. * @return name:media_title type:string default:-- desc:媒体名称
  129. * @return name:duration type:string default:-- desc:发布时长
  130. */
  131. public function news_detail(){
  132. $uid = $this->get_uid();
  133. $id = input('id');
  134. if(empty($id)){
  135. $this->error('参数错误');
  136. }
  137. $field = 'id,title,user_id,content,create_at';
  138. $news_info = Db::name('store_goods')->field($field)->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  139. if(empty($news_info)){
  140. $this->error('新闻信息有误');
  141. }
  142. //获取评论数量
  143. $news_info['comment_num'] = Db::name('store_comment')->where('forum_id',$id)->where('type',1)->where('forum_type',1)->count('id');
  144. //点赞数量
  145. $news_info['like_num'] = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
  146. //转发数量
  147. $news_info['transpond_num'] = Db::name('store_transpond')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
  148. $news_info['is_attention'] = 0;
  149. $news_info['is_like'] = 0;
  150. $news_info['is_collect'] = 0;
  151. if($uid){
  152. //是否关注
  153. $is_attention = Db::name('store_attention')->where('from_user_id',$news_info['user_id'])->where('user_id',$uid)->where('status',1)->count('id');
  154. if($is_attention){
  155. $news_info['is_attention'] = 1;
  156. }
  157. //是否点赞
  158. $is_like = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
  159. if($is_like){
  160. $news_info['is_like'] = 1;
  161. }
  162. //是否收藏
  163. $is_collect = Db::name('store_collect')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
  164. if($is_collect){
  165. $news_info['is_collect'] = 1;
  166. }
  167. }
  168. //媒体信息
  169. $media_info = Db::name('store_media')->field('logo,title')->where('id',$news_info['user_id'])->find();
  170. $news_info['media_logo'] = $media_info['logo'];
  171. $news_info['media_title'] = $media_info['title'];
  172. $remain_time = time() - strtotime($news_info['create_at']);
  173. $news_info['duration'] = get_stay_time($remain_time);
  174. unset($news_info['create_at']);
  175. if($uid && $uid != $news_info['user_id']){
  176. //增加浏览量
  177. $browse_id = Db::name('store_browse')->where('user_id',$uid)->where('forum_id',$id)->where('type',1)->value('id');
  178. if(empty($browse_id)){
  179. $data = array(
  180. 'user_id' => $uid,
  181. 'from_user_id' => $news_info['user_id'],
  182. 'forum_id' => $id,
  183. 'type' => 1,
  184. 'status' => 1
  185. );
  186. Db::name('store_browse')->insert($data);
  187. }
  188. }
  189. $this->success('获取成功',$news_info);
  190. }
  191. /**
  192. * @title 新闻评论列表
  193. * @desc 新闻评论列表
  194. * @author QGF
  195. * @url /api/Index/news_comment_list
  196. * @method GET
  197. * @tag 新闻评论列表
  198. * @header name:Authorization require:1 desc:Token
  199. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  200. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  201. * @param name:id type:int require:1 default:-- desc:新闻ID
  202. * @return name:-- type:array default:-- desc:id:评论ID,content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称,apply_num:这条评论回复的数量
  203. */
  204. public function news_comment_list(){
  205. $id = input('id');
  206. $page = input('page',1);
  207. $pageSize = input('page_size',10);
  208. if(empty($id)){
  209. $this->error('参数错误');
  210. }
  211. $news_info = Db::name('store_goods')->field('id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  212. if(empty($news_info)){
  213. $this->error('新闻信息有误');
  214. }
  215. //评论列表
  216. $comment_list = Db::name('store_comment')->field('id,user_id,content,create_time')->where('forum_id',$id)->where('type',1)->where('forum_type',1)->page($page,$pageSize)->order(['id'=>'desc'])->select();
  217. if($comment_list){
  218. foreach ($comment_list as $key=>&$value){
  219. $user_info = Db::name('store_member')->field('name,headimg,privacy_switch')->where('id',$value['user_id'])->find();
  220. /*if($user_info['privacy_switch']){
  221. unset($comment_list[$key]);
  222. continue;
  223. }*/
  224. $value['user_name'] = $user_info['name'];
  225. $value['user_headimg'] = $user_info['headimg'];
  226. //获取该条评论回复的数量
  227. $value['apply_num'] = Db::name('store_comment')->where('forum_id',$id)->where('type',2)->where('forum_type',1)->where('root_comment_id',$value['id'])->count('id');
  228. unset($value['user_id']);
  229. }
  230. }
  231. $this->success('获取成功',array_values($comment_list));
  232. }
  233. /**
  234. * @title 新闻回复评论列表
  235. * @desc 新闻回复评论列表
  236. * @author QGF
  237. * @url /api/Index/news_reply_comment_list
  238. * @method GET
  239. * @tag 新闻回复评论列表
  240. * @header name:Authorization require:1 desc:Token
  241. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  242. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  243. * @param name:id type:int require:1 default:-- desc:评论ID
  244. * @return name:comment type:array default:-- desc:id:评论ID,content:评论内容,content:评论内容,headimg:评论者头像,name:评论者名称
  245. * @return name:list type:array default:-- desc:content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称
  246. */
  247. public function news_reply_comment_list(){
  248. $id = input('id');
  249. $page = input('page',1);
  250. $pageSize = input('page_size',10);
  251. if(empty($id)){
  252. $this->error('参数错误');
  253. }
  254. //查看评论的信息
  255. $comment_info = Db::name('store_comment')->field('id,user_id,content')->where('id',$id)->where('type',1)->where('forum_type',1)->find();
  256. if(empty($comment_info)){
  257. $this->error('评论信息有误');
  258. }
  259. $member_info = Db::name('store_member')->field('name,headimg')->where('id',$comment_info['user_id'])->find();
  260. $comment_info['user_name'] = $member_info['name'];
  261. $comment_info['user_headimg'] = $member_info['headimg'];
  262. unset($comment_info['user_id']);
  263. //回复评论列表
  264. $comment_list = Db::name('store_comment')->field('user_id,content,create_time')->where('root_comment_id',$id)->where('type',2)->page($page,$pageSize)->order(['id'=>'desc'])->select();
  265. if($comment_list){
  266. foreach ($comment_list as &$value){
  267. $user_info = Db::name('store_member')->field('name,headimg')->where('id',$value['user_id'])->find();
  268. $value['user_name'] = $user_info['name'];
  269. $value['user_headimg'] = $user_info['headimg'];
  270. unset($value['user_id']);
  271. }
  272. }
  273. $data = array(
  274. 'comment' => $comment_info,
  275. 'list' => $comment_list
  276. );
  277. $this->success('获取成功',$data);
  278. }
  279. /**
  280. * @title 关注详情
  281. * @desc 关注详情
  282. * @author QGF
  283. * @url /api/Index/attention_detail
  284. * @method GET
  285. * @tag 关注详情
  286. * @header name:Authorization require:1 desc:Token
  287. * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
  288. * @return name:user_type type:int default:-- desc:关注类型(1:用户,2:媒体(用户只发布咨询,媒体有新闻和咨询))
  289. * @return name:user_name type:string default:-- desc:姓名
  290. * @return name:user_headimg type:string default:-- desc:头像地址
  291. * @return name:is_attention type:int default:-- desc:是否关注(0:未关注,1:已关注)
  292. * @return name:like_num type:int default:-- desc:获得点赞的数量
  293. * @return name:attention_num type:int default:-- desc:获得关注的数量
  294. */
  295. public function attention_detail(){
  296. $user_id = input('user_id');
  297. if(empty($user_id)){
  298. $this->error('参数错误');
  299. }
  300. $user_type = user_type($user_id);//1:用户 2:媒体
  301. if($user_type == 1){
  302. $user_info = Db::name('store_member')->field('name,headimg')->where('id',$user_id)->find();
  303. $user_name = $user_info['name'];
  304. $user_headimg = $user_info['headimg'];
  305. }else{
  306. $media_info = Db::name('store_media')->field('logo,title')->where('id',$user_id)->find();
  307. $user_name = $media_info['title'];
  308. $user_headimg = $media_info['logo'];
  309. }
  310. //是否关注
  311. $is_attention = 0;
  312. $uid = $this->get_uid();
  313. if($uid){
  314. $attention_id = Db::name('store_attention')->where('from_user_id',$user_id)->where('user_id',$uid)->where('status',1)->count('id');
  315. if($attention_id){
  316. $is_attention = 1;
  317. }
  318. }
  319. //共获得点赞数
  320. $like_num = Db::name('store_like')->where('from_user_id',$user_id)->where('status',1)->count('id');
  321. //共关注的数
  322. $attention_num = Db::name('store_attention')->where('from_user_id',$user_id)->where('status',1)->count('id');
  323. $data = array(
  324. 'user_type' => $user_type,
  325. 'user_name' => $user_name,
  326. 'user_headimg' => $user_headimg,
  327. 'is_attention' => $is_attention,
  328. 'like_num' => $like_num,
  329. 'attention_num' => $attention_num
  330. );
  331. $this->success('获取成功',$data);
  332. }
  333. /**
  334. * @title 被关注者发布的新闻列表
  335. * @desc 被关注者发布的新闻列表
  336. * @author QGF
  337. * @url /api/Index/attention_news_list
  338. * @method GET
  339. * @tag 被关注者发布的新闻列表
  340. * @header name:Authorization require:1 desc:Token
  341. * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
  342. * @return name:id type:string default:-- desc:新闻ID
  343. * @return name:title type:string default:-- desc:新闻标题
  344. * @return name:image type:string default:-- desc:新闻图片(一个图片地址)
  345. * @return name:comment_num type:int default:-- desc:评论数量
  346. * @return name:duration type:string default:-- desc:发布的时长
  347. */
  348. public function attention_news_list(){
  349. $user_id = input('user_id');
  350. $page = input('page',1);
  351. $pageSize = input('page_size',10);
  352. if(empty($user_id)){
  353. $this->error('参数错误');
  354. }
  355. $user_type = user_type($user_id);//1:用户 2:媒体
  356. if($user_type == 1){
  357. $this->error('发布者身份有误');
  358. }
  359. $list = Db::name('store_goods')->field('id,title,image,create_at')->where('status',1)->where('is_deleted',0)->where('user_id',$user_id)->page($page,$pageSize)->order(['is_top'=>'desc','sort'=>'desc','id'=>'desc'])->select();
  360. if($list){
  361. foreach($list as &$value) {
  362. //获取评论数量
  363. $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
  364. $value['image'] = image_path($value['image'])[0];
  365. $remain_time = time() - strtotime($value['create_at']);
  366. $value['duration'] = get_stay_time($remain_time);
  367. unset($value['create_at']);
  368. }
  369. }
  370. $this->success('获取成功',$list);
  371. }
  372. }