123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use app\api\controller\Base;
- use think\Db;
- use think\Model;
- /**
- * @title 首页(新闻)管理
- * @controller Index
- * @group base
- */
- class Index extends Base
- {
- /**
- * @title 新闻分类
- * @desc 新闻分类
- * @author HG
- * @url /api/Index/cate
- * @method GET
- * @tag 新闻分类
- * @return name:-- type:array default:-- desc:新闻列表(id:新闻ID,title:新闻标题)
- */
- public function cate(){
- $cate = Db::name('store_goods_cate')->field('id,title')->where('status',1)->where('is_deleted',0)->order('sort','desc')->select();
- $this->success('获取成功',$cate);
- }
- /**
- * @title 新闻列表
- * @desc 新闻列表
- * @author HG
- * @url /api/Index/news_list
- * @method GET
- * @tag 新闻列表
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:0 default:-- desc:新闻分类ID(搜索不用穿,其他必须传)
- * @param name:last_id type:int require:0 default:1 desc:最新的新闻id(为获取新的新闻数量)
- * @param name:keyword type:string require:0 default:-- desc:搜索的关键词
- * @param name:page type:int require:0 default:1 desc:页数(默认为1)
- * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
- * @return name:last_id type:string default:-- desc:最后一个新闻的id(用于下次刷新获取更新的新闻数量)
- * @return name:num type:string default:-- desc:新增新闻的数量
- * @return name:list type:array default:-- desc:新闻列表(id:新闻ID,title:新闻标题,image:新闻图片(数组格式),video:视频地址,comment_num:评论数量,is_top:是否置顶(0:不置顶,1:置顶),media_logo:媒体图标,media_title:媒体名称,duration:发布时长)
- */
- public function news_list(){
- $res = md5(md5('lmq123456') . '7fHgX9');
- echo $res;exit();
- $id = input('id');
- $last_id = input('last_id');
- $keyword = input('keyword');
- $page = input('page',1);
- $pageSize = input('page_size',10);
- if($keyword){
- $where = "title like '%".$keyword."%'";
- //保存搜索的关键词
- $uid = $this->get_uid();
- if($uid){
- $search_id = Db::name('store_search_keyword')->where('user_id',$uid)->where('type',1)->where('name',$keyword)->value('id');
- if(empty($search_id)){
- Db::name('store_search_keyword')->insert(array('user_id' => $uid, 'name' => $keyword));
- }
- }
- }else{
- $where = "id > 0";
- }
- if($id){
- $where .= " and cate_id = ".$id;
- }
- $field = 'id,title,user_id,image,video,is_top,create_at';
- $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();
- //新的新闻数量
- $num = 0;
- if($last_id){
- $num = Db::name('store_goods')->field($field)->where('status',1)->where('cate_id',$id)->where('is_deleted',0)->where('id','>',$last_id)->count('id');
- }
- //获取最大ID
- $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');
- if($list){
- foreach($list as &$value) {
- //获取评论数量
- $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
- $value['image'] = image_path($value['image']);
- //媒体信息
- $media_info = Db::name('store_media')->field('logo,title')->where('id',$value['media_id'])->find();
- $value['media_logo'] = $media_info['logo'];
- $value['media_title'] = $media_info['title'];
- $remain_time = time() - strtotime($value['create_at']);
- $value['duration'] = get_stay_time($remain_time);
- unset($value['user_id']);
- unset($value['create_at']);
- }
- }
- $data = array(
- 'last_id' => $result_last_id?$result_last_id:0,
- 'num' => $num,
- 'list' => $list
- );
- $this->success('获取成功',$data);
- }
- /**
- * @title 新闻详情
- * @desc 新闻详情
- * @author HG
- * @url /api/Index/news_detail
- * @method GET
- * @tag 新闻详情
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:新闻ID
- * @return name:id type:int default:-- desc:新闻的id
- * @return name:title type:string default:-- desc:新闻标题
- * @return name:user_id type:int default:-- desc:媒体ID
- * @return name:content type:string default:-- desc:新增内容(富文本)
- * @return name:comment_num type:int default:-- desc:评论数量
- * @return name:like_num type:int default:-- desc:点赞数量
- * @return name:transpond_num type:int default:-- desc:转发数量
- * @return name:is_attention type:int default:-- desc:是否关注媒体(0:未关注,1:已关注)
- * @return name:is_like type:int default:-- desc:是否点赞(0:未点赞,1:已点赞)
- * @return name:is_collect type:int default:-- desc:是否收藏(0:未收藏,1:已收藏)
- * @return name:media_logo type:string default:-- desc:媒体logo
- * @return name:media_title type:string default:-- desc:媒体名称
- * @return name:duration type:string default:-- desc:发布时长
- */
- public function news_detail(){
- $uid = $this->get_uid();
- $id = input('id');
- if(empty($id)){
- $this->error('参数错误');
- }
- $field = 'id,title,user_id,content,create_at';
- $news_info = Db::name('store_goods')->field($field)->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
- if(empty($news_info)){
- $this->error('新闻信息有误');
- }
- //获取评论数量
- $news_info['comment_num'] = Db::name('store_comment')->where('forum_id',$id)->where('type',1)->where('forum_type',1)->count('id');
- //点赞数量
- $news_info['like_num'] = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
- //转发数量
- $news_info['transpond_num'] = Db::name('store_transpond')->where('forum_id',$id)->where('type',1)->where('status',1)->count('id');
- $news_info['is_attention'] = 0;
- $news_info['is_like'] = 0;
- $news_info['is_collect'] = 0;
- if($uid){
- //是否关注
- $is_attention = Db::name('store_attention')->where('from_user_id',$news_info['user_id'])->where('user_id',$uid)->where('status',1)->count('id');
- if($is_attention){
- $news_info['is_attention'] = 1;
- }
- //是否点赞
- $is_like = Db::name('store_like')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
- if($is_like){
- $news_info['is_like'] = 1;
- }
- //是否收藏
- $is_collect = Db::name('store_collect')->where('forum_id',$id)->where('type',1)->where('user_id',$uid)->where('status',1)->count('id');
- if($is_collect){
- $news_info['is_collect'] = 1;
- }
- }
- //媒体信息
- $media_info = Db::name('store_media')->field('logo,title')->where('id',$news_info['user_id'])->find();
- $news_info['media_logo'] = $media_info['logo'];
- $news_info['media_title'] = $media_info['title'];
- $remain_time = time() - strtotime($news_info['create_at']);
- $news_info['duration'] = get_stay_time($remain_time);
- unset($news_info['create_at']);
- if($uid && $uid != $news_info['user_id']){
- //增加浏览量
- $browse_id = Db::name('store_browse')->where('user_id',$uid)->where('forum_id',$id)->where('type',1)->value('id');
- if(empty($browse_id)){
- $data = array(
- 'user_id' => $uid,
- 'from_user_id' => $news_info['user_id'],
- 'forum_id' => $id,
- 'type' => 1,
- 'status' => 1
- );
- Db::name('store_browse')->insert($data);
- }
- }
- $this->success('获取成功',$news_info);
- }
- /**
- * @title 新闻评论列表
- * @desc 新闻评论列表
- * @author QGF
- * @url /api/Index/news_comment_list
- * @method GET
- * @tag 新闻评论列表
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int require:0 default:1 desc:页数(默认为1)
- * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
- * @param name:id type:int require:1 default:-- desc:新闻ID
- * @return name:-- type:array default:-- desc:id:评论ID,content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称,apply_num:这条评论回复的数量
- */
- public function news_comment_list(){
- $id = input('id');
- $page = input('page',1);
- $pageSize = input('page_size',10);
- if(empty($id)){
- $this->error('参数错误');
- }
- $news_info = Db::name('store_goods')->field('id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
- if(empty($news_info)){
- $this->error('新闻信息有误');
- }
- //评论列表
- $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();
- if($comment_list){
- foreach ($comment_list as $key=>&$value){
- $user_info = Db::name('store_member')->field('name,headimg,privacy_switch')->where('id',$value['user_id'])->find();
- if($user_info['privacy_switch']){
- unset($comment_list[$key]);
- continue;
- }
- $value['user_name'] = $user_info['name'];
- $value['user_headimg'] = $user_info['headimg'];
- //获取该条评论回复的数量
- $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');
- unset($value['user_id']);
- }
- }
- $this->success('获取成功',array_values($comment_list));
- }
- /**
- * @title 新闻回复评论列表
- * @desc 新闻回复评论列表
- * @author QGF
- * @url /api/Index/news_reply_comment_list
- * @method GET
- * @tag 新闻回复评论列表
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int require:0 default:1 desc:页数(默认为1)
- * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
- * @param name:id type:int require:1 default:-- desc:评论ID
- * @return name:comment type:array default:-- desc:id:评论ID,content:评论内容,content:评论内容,headimg:评论者头像,name:评论者名称
- * @return name:list type:array default:-- desc:content:评论内容,create_time:评论时间,headimg:评论者头像,name:评论者名称
- */
- public function news_reply_comment_list(){
- $id = input('id');
- $page = input('page',1);
- $pageSize = input('page_size',10);
- if(empty($id)){
- $this->error('参数错误');
- }
- //查看评论的信息
- $comment_info = Db::name('store_comment')->field('id,user_id,content')->where('id',$id)->where('type',1)->where('forum_type',1)->find();
- if(empty($comment_info)){
- $this->error('评论信息有误');
- }
- $member_info = Db::name('store_member')->field('name,headimg')->where('id',$comment_info['user_id'])->find();
- $comment_info['user_name'] = $member_info['name'];
- $comment_info['user_headimg'] = $member_info['headimg'];
- unset($comment_info['user_id']);
- //回复评论列表
- $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();
- if($comment_list){
- foreach ($comment_list as &$value){
- $user_info = Db::name('store_member')->field('name,headimg')->where('id',$value['user_id'])->find();
- $value['user_name'] = $user_info['name'];
- $value['user_headimg'] = $user_info['headimg'];
- unset($value['user_id']);
- }
- }
- $data = array(
- 'comment' => $comment_info,
- 'list' => $comment_list
- );
- $this->success('获取成功',$data);
- }
- /**
- * @title 关注详情
- * @desc 关注详情
- * @author QGF
- * @url /api/Index/attention_detail
- * @method GET
- * @tag 关注详情
- * @header name:Authorization require:1 desc:Token
- * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
- * @return name:user_type type:int default:-- desc:关注类型(1:用户,2:媒体(用户只发布咨询,媒体有新闻和咨询))
- * @return name:user_name type:string default:-- desc:姓名
- * @return name:user_headimg type:string default:-- desc:头像地址
- * @return name:is_attention type:int default:-- desc:是否关注(0:未关注,1:已关注)
- * @return name:like_num type:int default:-- desc:获得点赞的数量
- * @return name:attention_num type:int default:-- desc:获得关注的数量
- */
- public function attention_detail(){
- $user_id = input('user_id');
- if(empty($user_id)){
- $this->error('参数错误');
- }
- $user_type = user_type($user_id);//1:用户 2:媒体
- if($user_type == 1){
- $user_info = Db::name('store_member')->field('name,headimg')->where('id',$user_id)->find();
- $user_name = $user_info['name'];
- $user_headimg = $user_info['headimg'];
- }else{
- $media_info = Db::name('store_media')->field('logo,title')->where('id',$user_id)->find();
- $user_name = $media_info['title'];
- $user_headimg = $media_info['logo'];
- }
- //是否关注
- $is_attention = 0;
- $uid = $this->get_uid();
- if($uid){
- $attention_id = Db::name('store_attention')->where('from_user_id',$user_id)->where('user_id',$uid)->where('status',1)->count('id');
- if($attention_id){
- $is_attention = 1;
- }
- }
- //共获得点赞数
- $like_num = Db::name('store_like')->where('from_user_id',$user_id)->where('status',1)->count('id');
- //共关注的数
- $attention_num = Db::name('store_attention')->where('from_user_id',$user_id)->where('status',1)->count('id');
- $data = array(
- 'user_type' => $user_type,
- 'user_name' => $user_name,
- 'user_headimg' => $user_headimg,
- 'is_attention' => $is_attention,
- 'like_num' => $like_num,
- 'attention_num' => $attention_num
- );
- $this->success('获取成功',$data);
- }
- /**
- * @title 被关注者发布的新闻列表
- * @desc 被关注者发布的新闻列表
- * @author QGF
- * @url /api/Index/attention_news_list
- * @method GET
- * @tag 被关注者发布的新闻列表
- * @header name:Authorization require:1 desc:Token
- * @param name:user_id type:int require:1 default:-- desc:媒体或用户ID
- * @return name:id type:string default:-- desc:新闻ID
- * @return name:title type:string default:-- desc:新闻标题
- * @return name:image type:string default:-- desc:新闻图片(一个图片地址)
- * @return name:comment_num type:int default:-- desc:评论数量
- * @return name:duration type:string default:-- desc:发布的时长
- */
- public function attention_news_list(){
- $user_id = input('user_id');
- $page = input('page',1);
- $pageSize = input('page_size',10);
- if(empty($user_id)){
- $this->error('参数错误');
- }
- $user_type = user_type($user_id);//1:用户 2:媒体
- if($user_type == 1){
- $this->error('发布者身份有误');
- }
- $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();
- if($list){
- foreach($list as &$value) {
- //获取评论数量
- $value['comment_num'] = Db::name('store_comment')->where('forum_id',$value['id'])->where('type',1)->where('forum_type',1)->count('id');
- $value['image'] = image_path($value['image'])[0];
- $remain_time = time() - strtotime($value['create_at']);
- $value['duration'] = get_stay_time($remain_time);
- unset($value['create_at']);
- }
- }
- $this->success('获取成功',$list);
- }
- }
|