123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php namespace App\Http\Controllers\Api\Article;
- use App\Http\Controllers\Api\Api;
- use App\Models\Article as Model;
- use App\Models\ArticleComment as ArticleCommentModel;
- /**
- * 分享设置
- *
- * @author huanglei
- *
- */
- class Comment extends Api{
- const EVENT_TYPE = [
- '1' => '阅读',
- '2' => '点赞',
- '3' => '分享',
- '4' => '推荐',
- '5' => '取消推荐',
- '6' => '取消点赞'
- ];
- public function list(Model $Model, ArticleCommentModel $ArticleCommentModel){
-
- // 接受参数
- $code = request('article_code','');
- $status = request('status');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- // 编码转ID
- $id = $code ? $Model->codeToId($code) : 0;
- // 查询条件
- $map = [];
- // 编码ID
- if( $id ) $map[] = ['id','=',$id];
- if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['status','=',$status];
- // 查询文章数据
- $list = $Model->query()->where($map);
- $list = $list->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
-
- // 循环处理数据
- foreach ($list as $key => $value) {
- // id转编号
- $value['article_id'] = $value['id'];
- // 重组
- $value['poster'] = $value['poster']? path_compat($value['poster']):'';
-
- $value['read_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$value['id'],'event_type'=>1])
- ->count();
- $value['like_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$value['id'],'event_type'=>2])
- ->count();
-
- $value['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
- $list[$key] = $value;
- }
-
- // 分配数据
- return json_send(['code'=>'success','msg'=>'列表数据','data'=>$list]);
-
-
-
- }
- /**
- * 数据详情
- *
- * */
- public function detail(Model $Model, ArticleCommentModel $ArticleCommentModel){
- // 接受参数
- $tid = request('article_id',0);
- $uid = $this->checkLogin(); // custom_uid
-
- // 查询条件
- $map = [];
- // 编码ID
- if( $tid ) $map[] = ['id','=',$tid];
-
-
-
- $data = $Model->query()->where($map)->find($tid,['id','title','poster','content']);
- $data['like_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$tid,'event_type'=>2])
- ->count();
- $data['read_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$tid,'event_type'=>1])
- ->count();
- $data['share_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$tid,'event_type'=>3])
- ->count();
- $data['recommend_count'] = $ArticleCommentModel->query()
- ->where(['article_id'=>$tid,'event_type'=>4])
- ->count();
-
-
- //用户是否点赞和推荐
- if ($uid){
- $user_envent = $ArticleCommentModel->query()->where(['uid'=>$uid,'article_id'=>$tid])
- ->get();
- foreach ($user_envent as $value){
-
- if ($value['event_type'] == 2){
- $data['like_type'] = 2;
- }else{
- $data['like_type'] = 6;
- }
- if ($value['event_type'] == 4){
- $data['recommend_type'] = 4;
- }else{
- $data['recommend_type'] = 5;
- }
- }
- //写入浏览记录
- $read = [
- 'article_id' => $tid,
- 'event_type' => 1,
- 'uid' => $uid,
- 'insert_time' => time()
- ];
- //查询用户是否浏览过
- $read_type = $ArticleCommentModel->query()->where(['uid'=>$uid,'article_id'=>$tid])->value('event_type');
-
- if (!$read_type){
- $comment_id = $ArticleCommentModel->add($read);
- }
-
-
- }else{
-
- $read['event_type'] = 0;//禁止分享、推荐和点赞
- }
- // 加载模板
-
- if ($data){
- return json_send(['code'=>'success','msg'=>'数据','data'=>$data]);
- }
- return json_send(['code'=>'success','msg'=>'暂无','data'=>[]]);
-
- }
- /**
- * 数据详情
- *
- * */
- public function update_event(Model $Model, ArticleCommentModel $ArticleCommentModel){
- // '1' => '阅读',
- // '2' => '点赞',
- // '3' => '分享',
- // '4' => '推荐',
- // '5' => '取消推荐',
- // '6' => '取消点赞'
- $event = request('event_type',0);
- $tid = request('article_id',0);
- $uid = $this->checkLogin(); // custom_uid
- // 查询条件
- $map = [];
- $map[] = ['uid','=',$uid];
- $map[] = ['article_id','=',$tid];
- $map[] = ['event_type','=',$event];
- $user_envent = $ArticleCommentModel->query()->where($map)->get();
- switch ($event){
- case 2:
-
- if ($user_envent){
- $Res = $ArticleCommentModel::query()
- ->where(['uid'=>$uid,'article_id'=>$tid])
- ->update(['event_type'=>'6']);
- return json_send(['code'=>'error','msg'=>'取消点赞','data'=>[]]);
- }
- break;
- case 4:
- if ($user_envent){
- $Res = $ArticleCommentModel::query()
- ->where(['uid'=>$uid,'article_id'=>$tid])
- ->update(['event_type'=>'4']);
- return json_send(['code'=>'error','msg'=>'取消推荐','data'=>[]]);
- }
- break;
- case 6:
- $map[] = ['uid','=',$uid];
- $map[] = ['article_id','=',$tid];
- $map[] = ['event_type','=',$event];
- }
- $user_envent = $ArticleCommentModel->query()->where($map)->get();
- if( $tid ) $map[] = ['id','=',$tid];
- // $orderRes = $Model::query()->where('article_id','=',$tid)->update(['status'=>'1']);
- //用户是否点赞和推荐
-
- return json_send(['code'=>'success','msg'=>'成功',
- 'data'=>[
- 'event_type'=>$event,'article_id'=>$tid,'uid'=>$uid]
- ]);
-
- }
-
- }
|