Index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $res = md5(md5('lmq123456') . '7fHgX9');
  57. echo $res;exit();
  58. $id = input('id');
  59. $last_id = input('last_id');
  60. $keyword = input('keyword');
  61. $page = input('page',1);
  62. $pageSize = input('page_size',10);
  63. if($keyword){
  64. $where = "title like '%".$keyword."%'";
  65. //保存搜索的关键词
  66. $uid = $this->get_uid();
  67. if($uid){
  68. $search_id = Db::name('store_search_keyword')->where('user_id',$uid)->where('type',1)->where('name',$keyword)->value('id');
  69. if(empty($search_id)){
  70. Db::name('store_search_keyword')->insert(array('user_id' => $uid, 'name' => $keyword));
  71. }
  72. }
  73. }else{
  74. $where = "id > 0";
  75. }
  76. if($id){
  77. $where .= " and cate_id = ".$id;
  78. }
  79. $field = 'id,title,user_id,image,video,is_top,create_at';
  80. $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();
  81. //新的新闻数量
  82. $num = 0;
  83. if($last_id){
  84. $num = Db::name('store_goods')->field($field)->where('status',1)->where('cate_id',$id)->where('is_deleted',0)->where('id','>',$last_id)->count('id');
  85. }
  86. //获取最大ID
  87. $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');
  88. if($list){
  89. foreach($list as &$value) {
  90. //获取评论数量
  91. $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
  92. $value['image'] = image_path($value['image']);
  93. //媒体信息
  94. $media_info = Db::name('store_media')->field('logo,title')->where('id',$value['media_id'])->find();
  95. $value['media_logo'] = $media_info['logo'];
  96. $value['media_title'] = $media_info['title'];
  97. $remain_time = time() - strtotime($value['create_at']);
  98. $value['duration'] = get_stay_time($remain_time);
  99. unset($value['user_id']);
  100. unset($value['create_at']);
  101. }
  102. }
  103. $data = array(
  104. 'last_id' => $result_last_id?$result_last_id:0,
  105. 'num' => $num,
  106. 'list' => $list
  107. );
  108. $this->success('获取成功',$data);
  109. }
  110. /**
  111. * @title 新闻详情
  112. * @desc 新闻详情
  113. * @author HG
  114. * @url /api/Index/news_detail
  115. * @method GET
  116. * @tag 新闻详情
  117. * @header name:Authorization require:1 desc:Token
  118. * @param name:id type:int require:1 default:-- desc:新闻ID
  119. * @return name:id type:int default:-- desc:新闻的id
  120. * @return name:title type:string default:-- desc:新闻标题
  121. * @return name:user_id type:int default:-- desc:媒体ID
  122. * @return name:content type:string default:-- desc:新增内容(富文本)
  123. * @return name:comment_num type:int default:-- desc:评论数量
  124. * @return name:like_num type:int default:-- desc:点赞数量
  125. * @return name:transpond_num type:int default:-- desc:转发数量
  126. * @return name:is_attention type:int default:-- desc:是否关注媒体(0:未关注,1:已关注)
  127. * @return name:is_like type:int default:-- desc:是否点赞(0:未点赞,1:已点赞)
  128. * @return name:is_collect type:int default:-- desc:是否收藏(0:未收藏,1:已收藏)
  129. * @return name:media_logo type:string default:-- desc:媒体logo
  130. * @return name:media_title type:string default:-- desc:媒体名称
  131. * @return name:duration type:string default:-- desc:发布时长
  132. */
  133. public function news_detail(){
  134. $uid = $this->get_uid();
  135. $id = input('id');
  136. if(empty($id)){
  137. $this->error('参数错误');
  138. }
  139. $field = 'id,title,user_id,content,create_at';
  140. $news_info = Db::name('store_goods')->field($field)->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  141. if(empty($news_info)){
  142. $this->error('新闻信息有误');
  143. }
  144. //获取评论数量
  145. $news_info['comment_num'] = Db::name('store_comment')->where('forum_id',$id)->where('type',1)->where('forum_type',1)->count('id');
  146. //点赞数量
  147. $news_info['like_num'] = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
  148. //转发数量
  149. $news_info['transpond_num'] = Db::name('store_transpond')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
  150. $news_info['is_attention'] = 0;
  151. $news_info['is_like'] = 0;
  152. $news_info['is_collect'] = 0;
  153. if($uid){
  154. //是否关注
  155. $is_attention = Db::name('store_attention')->where('from_user_id',$news_info['user_id'])->where('user_id',$uid)->where('status',1)->count('id');
  156. if($is_attention){
  157. $news_info['is_attention'] = 1;
  158. }
  159. //是否点赞
  160. $is_like = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
  161. if($is_like){
  162. $news_info['is_like'] = 1;
  163. }
  164. //是否收藏
  165. $is_collect = Db::name('store_collect')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
  166. if($is_collect){
  167. $news_info['is_collect'] = 1;
  168. }
  169. }
  170. //媒体信息
  171. $media_info = Db::name('store_media')->field('logo,title')->where('id',$news_info['user_id'])->find();
  172. $news_info['media_logo'] = $media_info['logo'];
  173. $news_info['media_title'] = $media_info['title'];
  174. $remain_time = time() - strtotime($news_info['create_at']);
  175. $news_info['duration'] = get_stay_time($remain_time);
  176. unset($news_info['create_at']);
  177. if($uid && $uid != $news_info['user_id']){
  178. //增加浏览量
  179. $browse_id = Db::name('store_browse')->where('user_id',$uid)->where('forum_id',$id)->where('type',1)->value('id');
  180. if(empty($browse_id)){
  181. $data = array(
  182. 'user_id' => $uid,
  183. 'from_user_id' => $news_info['user_id'],
  184. 'forum_id' => $id,
  185. 'type' => 1,
  186. 'status' => 1
  187. );
  188. Db::name('store_browse')->insert($data);
  189. }
  190. }
  191. $this->success('获取成功',$news_info);
  192. }
  193. /**
  194. * @title 新闻评论列表
  195. * @desc 新闻评论列表
  196. * @author QGF
  197. * @url /api/Index/news_comment_list
  198. * @method GET
  199. * @tag 新闻评论列表
  200. * @header name:Authorization require:1 desc:Token
  201. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  202. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  203. * @param name:id type:int require:1 default:-- desc:新闻ID
  204. * @return name:-- type:array default:-- desc:id:评论ID,content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称,apply_num:这条评论回复的数量
  205. */
  206. public function news_comment_list(){
  207. $id = input('id');
  208. $page = input('page',1);
  209. $pageSize = input('page_size',10);
  210. if(empty($id)){
  211. $this->error('参数错误');
  212. }
  213. $news_info = Db::name('store_goods')->field('id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  214. if(empty($news_info)){
  215. $this->error('新闻信息有误');
  216. }
  217. //评论列表
  218. $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();
  219. if($comment_list){
  220. foreach ($comment_list as $key=>&$value){
  221. $user_info = Db::name('store_member')->field('name,headimg,privacy_switch')->where('id',$value['user_id'])->find();
  222. if($user_info['privacy_switch']){
  223. unset($comment_list[$key]);
  224. continue;
  225. }
  226. $value['user_name'] = $user_info['name'];
  227. $value['user_headimg'] = $user_info['headimg'];
  228. //获取该条评论回复的数量
  229. $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');
  230. unset($value['user_id']);
  231. }
  232. }
  233. $this->success('获取成功',array_values($comment_list));
  234. }
  235. /**
  236. * @title 新闻回复评论列表
  237. * @desc 新闻回复评论列表
  238. * @author QGF
  239. * @url /api/Index/news_reply_comment_list
  240. * @method GET
  241. * @tag 新闻回复评论列表
  242. * @header name:Authorization require:1 desc:Token
  243. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  244. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  245. * @param name:id type:int require:1 default:-- desc:评论ID
  246. * @return name:comment type:array default:-- desc:id:评论ID,content:评论内容,content:评论内容,headimg:评论者头像,name:评论者名称
  247. * @return name:list type:array default:-- desc:content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称
  248. */
  249. public function news_reply_comment_list(){
  250. $id = input('id');
  251. $page = input('page',1);
  252. $pageSize = input('page_size',10);
  253. if(empty($id)){
  254. $this->error('参数错误');
  255. }
  256. //查看评论的信息
  257. $comment_info = Db::name('store_comment')->field('id,user_id,content')->where('id',$id)->where('type',1)->where('forum_type',1)->find();
  258. if(empty($comment_info)){
  259. $this->error('评论信息有误');
  260. }
  261. $member_info = Db::name('store_member')->field('name,headimg')->where('id',$comment_info['user_id'])->find();
  262. $comment_info['user_name'] = $member_info['name'];
  263. $comment_info['user_headimg'] = $member_info['headimg'];
  264. unset($comment_info['user_id']);
  265. //回复评论列表
  266. $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();
  267. if($comment_list){
  268. foreach ($comment_list as &$value){
  269. $user_info = Db::name('store_member')->field('name,headimg')->where('id',$value['user_id'])->find();
  270. $value['user_name'] = $user_info['name'];
  271. $value['user_headimg'] = $user_info['headimg'];
  272. unset($value['user_id']);
  273. }
  274. }
  275. $data = array(
  276. 'comment' => $comment_info,
  277. 'list' => $comment_list
  278. );
  279. $this->success('获取成功',$data);
  280. }
  281. /**
  282. * @title 关注详情
  283. * @desc 关注详情
  284. * @author QGF
  285. * @url /api/Index/attention_detail
  286. * @method GET
  287. * @tag 关注详情
  288. * @header name:Authorization require:1 desc:Token
  289. * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
  290. * @return name:user_type type:int default:-- desc:关注类型(1:用户,2:媒体(用户只发布咨询,媒体有新闻和咨询))
  291. * @return name:user_name type:string default:-- desc:姓名
  292. * @return name:user_headimg type:string default:-- desc:头像地址
  293. * @return name:is_attention type:int default:-- desc:是否关注(0:未关注,1:已关注)
  294. * @return name:like_num type:int default:-- desc:获得点赞的数量
  295. * @return name:attention_num type:int default:-- desc:获得关注的数量
  296. */
  297. public function attention_detail(){
  298. $user_id = input('user_id');
  299. if(empty($user_id)){
  300. $this->error('参数错误');
  301. }
  302. $user_type = user_type($user_id);//1:用户 2:媒体
  303. if($user_type == 1){
  304. $user_info = Db::name('store_member')->field('name,headimg')->where('id',$user_id)->find();
  305. $user_name = $user_info['name'];
  306. $user_headimg = $user_info['headimg'];
  307. }else{
  308. $media_info = Db::name('store_media')->field('logo,title')->where('id',$user_id)->find();
  309. $user_name = $media_info['title'];
  310. $user_headimg = $media_info['logo'];
  311. }
  312. //是否关注
  313. $is_attention = 0;
  314. $uid = $this->get_uid();
  315. if($uid){
  316. $attention_id = Db::name('store_attention')->where('from_user_id',$user_id)->where('user_id',$uid)->where('status',1)->count('id');
  317. if($attention_id){
  318. $is_attention = 1;
  319. }
  320. }
  321. //共获得点赞数
  322. $like_num = Db::name('store_like')->where('from_user_id',$user_id)->where('status',1)->count('id');
  323. //共关注的数
  324. $attention_num = Db::name('store_attention')->where('from_user_id',$user_id)->where('status',1)->count('id');
  325. $data = array(
  326. 'user_type' => $user_type,
  327. 'user_name' => $user_name,
  328. 'user_headimg' => $user_headimg,
  329. 'is_attention' => $is_attention,
  330. 'like_num' => $like_num,
  331. 'attention_num' => $attention_num
  332. );
  333. $this->success('获取成功',$data);
  334. }
  335. /**
  336. * @title 被关注者发布的新闻列表
  337. * @desc 被关注者发布的新闻列表
  338. * @author QGF
  339. * @url /api/Index/attention_news_list
  340. * @method GET
  341. * @tag 被关注者发布的新闻列表
  342. * @header name:Authorization require:1 desc:Token
  343. * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
  344. * @return name:id type:string default:-- desc:新闻ID
  345. * @return name:title type:string default:-- desc:新闻标题
  346. * @return name:image type:string default:-- desc:新闻图片(一个图片地址)
  347. * @return name:comment_num type:int default:-- desc:评论数量
  348. * @return name:duration type:string default:-- desc:发布的时长
  349. */
  350. public function attention_news_list(){
  351. $user_id = input('user_id');
  352. $page = input('page',1);
  353. $pageSize = input('page_size',10);
  354. if(empty($user_id)){
  355. $this->error('参数错误');
  356. }
  357. $user_type = user_type($user_id);//1:用户 2:媒体
  358. if($user_type == 1){
  359. $this->error('发布者身份有误');
  360. }
  361. $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();
  362. if($list){
  363. foreach($list as &$value) {
  364. //获取评论数量
  365. $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
  366. $value['image'] = image_path($value['image'])[0];
  367. $remain_time = time() - strtotime($value['create_at']);
  368. $value['duration'] = get_stay_time($remain_time);
  369. unset($value['create_at']);
  370. }
  371. }
  372. $this->success('获取成功',$list);
  373. }
  374. }