123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php namespace App\Http\Controllers\Api\Article;
- use App\Http\Controllers\Api\Api;
- use App\Models\Article as Model;
- use App\Models\ArticleEvent as ArticleEvent;
- use Illuminate\Support\Facades\DB;
- /**
- * 分享设置
- *
- * @author huanglei
- *
- */
- class Comment extends Api{
- const EVENT_TYPE = [
- '1' => '阅读',
- '2' => '点赞',
- '3' => '分享',
- '4' => '推荐',
- '5' => '取消推荐',
- '6' => '取消点赞'
- ];
- public function get_list(Model $Model){
- // 查询条件
- $map = [['status','=','0']];
- // 查询文章数据
- $Paginator = $Model->query()->where($map)->orderByDesc('id')->paginate(request('limit',config('page_num',10)));
- // 循环处理数据
- foreach ($Paginator as $key => $value) {
- // 海报图
- $value['poster'] = $value['poster'] ? path_compat($value['poster']) : '';
- // 如果没有缩略图,使用海报图
- $value['thmub'] = $value['thmub'] ? path_compat($value['thmub']) : $value['poster'];
- // 获取数据
- $value['insert_time'] = date('Y-m-d',$value['insert_time']);
- // 重组
- $Paginator[$key] = $value;
- }
- // 获取数据
- $data['total'] = $Paginator->total();
- $data['current_page'] = $Paginator->currentPage();
- $data['per_page'] = $Paginator->perPage();
- $data['last_page'] = $Paginator->lastPage();
- $data['data'] = $Paginator->items();
- // 分配数据
- return json_send(['code'=>'success','msg'=>'列表数据','data'=>$data]);
-
-
-
- }
- /**
- * 数据详情
- *
- * */
- public function get_detail(Model $Model, ArticleEvent $ArticleEvent){
- // 检查登录
- $uid = $this->checkLogin();
- // 接受参数
- $id = request('id',0);
- // 获取旧数据
- $articleData = $Model->query()->where([['status','=',0]])->find($id,['id','title','poster','content','path','appid','read_count','hand_count','like_count','share_count','insert_time']);
- // 用户是否点赞
- if( !$articleData ) return json_send(['code'=>'error','msg'=>'文章不存在或者已下架']);
- // 转数组
- $articleData = $articleData->toArray();
- // 阅读数 + 1
- $Model->query()->where([['id','=',$id]])->increment('read_count');
- // 如果事件不存在,新增此次事件
- $ArticleEvent->add(['custom_uid'=>$uid,'article_id'=>$id,'type_id'=>1,'status'=>0]);
- // 用户是否点赞和推荐
- $articleData['is_hand'] = $uid ? (int) $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$id],['type_id','=',2],['status','=',0]])->value('id') : 0;
- $articleData['is_like'] = $uid ? (int) $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$id],['type_id','=',4],['status','=',0]])->value('id') : 0;
- $articleData['read_count'] += 1;
- $articleData['insert_time'] = date('Y年m月d日 H:s',$articleData['insert_time']);
- $articleData['poster'] = $articleData['poster'] ? path_compat($articleData['poster']) : '';
- // 返回结果
- return json_send(['code'=>'success','msg'=>'暂无','data'=>$articleData]);
-
- }
-
- /**
- * 数据详情
- *
- * */
- public function update_event(Model $Model, ArticleEvent $ArticleEvent){
- // 检查登录
- $uid = $this->checkLogin();
- // 接收参数
- $typeId = request('type_id',0);
- $articleId = request('article_id',0);
- // 如果用户不存在
- $oldData = $uid ? $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$articleId],['type_id','=',$typeId]])->first(['id','status']) : [];
- // 判断事件。
- $oldData = $oldData ? $oldData->toArray() : [];
- // 事件已存在,并且是点赞或者喜欢
- if( $oldData && in_array($typeId,[2,4]) ){
- // 更新事件状态
- $ArticleEvent->edit($oldData['id'],['status'=>$oldData['status']?0:1]);
- }else{
- // 新增事件
- $ArticleEvent->add(['custom_uid'=>$uid,'article_id'=>$articleId,'type_id'=>$typeId,'status'=>0]);
- }
- // 判断数据类型,点赞
- if( $typeId == 2 ) {
- // 如果旧数据是未点赞或者取消点赞,继续点赞
- ( !$oldData || !empty($oldData['status']) ) ? $Model->query()->where([['id','=',$articleId]])->increment('hand_count') : $Model->query()->where([['id','=',$articleId],['hand_count','>=',1]])->decrement('hand_count');
- }
- // 判断数据类型,分享
- if( $typeId == 3 ) $Model->query()->where([['id','=',$articleId]])->increment('share_count');
- // 判断数据类型,喜欢
- if( $typeId == 4 ) {
- // 如果旧数据是未喜欢或者取消喜欢或,继续喜欢
- ( !$oldData || !empty($oldData['status']) ) ? $Model->query()->where([['id','=',$articleId]])->increment('like_count') : $Model->query()->where([['id','=',$articleId],['like_count','>=',1]])->decrement('like_count') ;
- }
- // 返回结果
- return json_send(['code'=>'success','msg'=>'操作成功','data'=>'']);
- }
-
- }
|