123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Article;
- use App\Models\ArticleLike;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- //use Grid
- class ArticleController extends Controller{
- public function index(Request $request){
- $data=Article::find($request->id);
- if ($data){
- return view('index')->with(['data'=>$data]);
- }
- }
- /*
- *点赞最多
- */
- public function show_list(Request $request){
- $input=$request->all();
- $index=$input['page_index'];
- $page_size=$input['page_size'];
- $user=Auth::user();
- $data=Article::where('status',1)->orderByDesc('created_at')->paginate($page_size);
- // if ($request->type==2){
- //
- // }else{
- // if ($request->type==1){
- // $type='created_at';
- // }else{
- // $type='like';
- // }
- // $data=Article::orderByDesc("$type")->orderbyDesc('type')->where('label','<>','1')->where('status',1)->paginate($page_size);
- // }
- $datas=$data->toArray();
- $total=$datas['total'];
- $re=[];
- $da=[];
- foreach ($data as $k=>$v){
- $res=[];
- if ($v->type==2){
- $now=Carbon::now()->timestamp;
- $time=strtotime($v->startime);
- $time_now=$now-$time;
- Log::error('fasdfasdfa');
- $hou=$time_now/3600;
- if ($hou<10){
- Log::error('{==========f测试}}');
- $re[$k]['id']=$v->id;
- $re[$k]['title']=$v->title;
- $re[$k]['type']=$v->type;
- $re[$k]['off']=1;
- $re[$k]['avatar'] = $v->avatar;
- $re[$k]['summary']=$v->summary;
- $re[$k]['author']=$v->author;
- // $re[$k]['contents']=$v->contents;
- $re[$k]['created_at']=strtotime($v->created_at)*1000;
- $re[$k]['like_num']=$v->like;
- $re[$k]['imgurl']=json_encode($v->imgurl);
- $re[$k]['like']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('like'))==1?true:false;
- $re[$k]['collection']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('collection'))==1?true:false;
- }
- }else{
- Log::error('{==========f测试}}');
- $re[$k]['id']=$v->id;
- $re[$k]['title']=$v->title;
- $re[$k]['type']=$v->type;
- $re[$k]['off']=1;
- $re[$k]['avatar'] = $v->avatar;
- $re[$k]['summary']=$v->summary;
- $re[$k]['author']=$v->author;
- // $re[$k]['contents']=$v->contents;
- $re[$k]['created_at']=strtotime($v->created_at)*1000;
- $re[$k]['like_num']=$v->like;
- $re[$k]['imgurl']=json_encode($v->imgurl);
- // $re[$k]['like']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('like'))==1?true:false;
- // $re[$k]['collection']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('collection'))==1?true:false;
- }
- $da=array_merge($res,$re);
- }
- $total=count($da);
- Log::error('{-----点赞----------}');
- return $this->success_list($da,'',$total);
- }
- public function show_detail(Request $request){
- $input=$request->all();
- $data=Article::find($input['id']);
- // if ($request->type==2){
- //
- // }else{
- // if ($request->type==1){
- // $type='created_at';
- // }else{
- // $type='like';
- // }
- // $data=Article::orderByDesc("$type")->orderbyDesc('type')->where('label','<>','1')->where('status',1)->paginate($page_size);
- // }
- $user=Auth::user();
- $data->avatar='https://api.jiuweiyun.cn/public/uploads/images/topics/916.jpg';
- // $data->imgurl=json_encode($data->imgurl);
- $data->created_at=strtotime($data->created_at)*1000;
- $data->off=1;
- $data->like=(ArticleLike::where('uid',$user->id)->where('article_id',$data->id)->value('like'))==1?true:false;
- $data->collection=(ArticleLike::where('uid',$user->id)->where('article_id',$data->id)->value('collection'))==1?true:false;
- return $this->success($data);
- }
- /*
- * 点赞与收藏
- * */
- public function like(Request $request){
- $user=Auth::user();
- $re=ArticleLike::where('uid',$user->id)->where('article_id',$request->id)->first();
- if ($re){
- $article=ArticleLike::find($re->id);
- if ($request->type==1){
- if ($request->status==1){
- $article->collection=1;
- }else{
- $article->collection=0;
- }
- }else{
- if ($request->status==1){
- $article->like=1;
- $articles=Article::find($request->id);
- $articles->like=($articles->like)+1;
- }else{
- $article->like=0;
- $articles=Article::find($request->id);
- $articles->like=($articles->like)-1;
- $articles->save();
- }
- }
- $article->save();
- }else{
- $article=new ArticleLike();
- $article->uid=$user->id;
- $article->article_id=$request->id;
- if ($request->type==1){
- if ($request->status==1){
- $article->collection=1;
- }else{
- $article->collection=0;
- }
- }else{
- if ($request->status==1){
- $article->like=1;
- $articles=Article::find($request->id);
- $articles->like=($articles->like)+1;
- }else{
- $article->like=0;
- $articles=Article::find($request->id);
- $articles->like=($articles->like)-1;
- $articles->save();
- }
- }
- $article->save();
- }
- return $this->success([]);
- }
- /**
- *我的收藏
- */
- public function mycollection()
- {
- $user = Auth::user();
- $data = ArticleLike::where('uid', $user->id)->where('collection', '=', 1)->paginate(2);
- $datas=$data->toArray();
- $re=[];
- foreach ($data as $k => $v) {
- $re[$k]['id'] = $v->article_id;
- $article=Article::find($v->article_id);
- if ($article){
- $re[$k]['off']=1;
- $re[$k]['avatar'] = 'https://api.jiuweiyun.cn/public/uploads/images/topics/916.jpg';
- $re[$k]['title'] =$article->title;
- $re[$k]['author'] = $article->author;
- $re[$k]['contents'] = $article->contents;
- $re[$k]['created_at'] = strtotime($article->created_at)*1000;
- $re[$k]['like_num'] = $article->like;
- $re[$k]['type']=$article->type;
- $re[$k]['summary']=$article->summary;
- $re[$k]['type'] = $article->type;
- $re[$k]['imgurl'] = json_encode($article->imgurl);
- $re[$k]['collection'] = $v->collection == 1 ? true : false;
- $re[$k]['like'] = $v->like == 1 ? true : false;
- }
- }
- // Log::error(json_encode($re));
- return $this->success_list($re,'',$datas['total']);
- }
- public function article_copy(Request $request){
- $article=Article::find($request->id);
- return make_excerpt($article->contents,10000000);
- }
- }
|