ArticleController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Article;
  4. use App\Models\ArticleLike;
  5. use Carbon\Carbon;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. //use Grid
  11. class ArticleController extends Controller{
  12. public function index(Request $request){
  13. $data=Article::find($request->id);
  14. if ($data){
  15. return view('index')->with(['data'=>$data]);
  16. }
  17. }
  18. /*
  19. *点赞最多
  20. */
  21. public function show_list(Request $request){
  22. $input=$request->all();
  23. $index=$input['page_index'];
  24. $page_size=$input['page_size'];
  25. $user=Auth::user();
  26. $data=Article::where('status',1)->orderByDesc('created_at')->paginate($page_size);
  27. // if ($request->type==2){
  28. //
  29. // }else{
  30. // if ($request->type==1){
  31. // $type='created_at';
  32. // }else{
  33. // $type='like';
  34. // }
  35. // $data=Article::orderByDesc("$type")->orderbyDesc('type')->where('label','<>','1')->where('status',1)->paginate($page_size);
  36. // }
  37. $datas=$data->toArray();
  38. $total=$datas['total'];
  39. $re=[];
  40. $da=[];
  41. foreach ($data as $k=>$v){
  42. $res=[];
  43. if ($v->type==2){
  44. $now=Carbon::now()->timestamp;
  45. $time=strtotime($v->startime);
  46. $time_now=$now-$time;
  47. Log::error('fasdfasdfa');
  48. $hou=$time_now/3600;
  49. if ($hou<10){
  50. Log::error('{==========f测试}}');
  51. $re[$k]['id']=$v->id;
  52. $re[$k]['title']=$v->title;
  53. $re[$k]['type']=$v->type;
  54. $re[$k]['off']=1;
  55. $re[$k]['avatar'] = $v->avatar;
  56. $re[$k]['summary']=$v->summary;
  57. $re[$k]['author']=$v->author;
  58. // $re[$k]['contents']=$v->contents;
  59. $re[$k]['created_at']=strtotime($v->created_at)*1000;
  60. $re[$k]['like_num']=$v->like;
  61. $re[$k]['imgurl']=json_encode($v->imgurl);
  62. $re[$k]['like']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('like'))==1?true:false;
  63. $re[$k]['collection']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('collection'))==1?true:false;
  64. }
  65. }else{
  66. Log::error('{==========f测试}}');
  67. $re[$k]['id']=$v->id;
  68. $re[$k]['title']=$v->title;
  69. $re[$k]['type']=$v->type;
  70. $re[$k]['off']=1;
  71. $re[$k]['avatar'] = $v->avatar;
  72. $re[$k]['summary']=$v->summary;
  73. $re[$k]['author']=$v->author;
  74. // $re[$k]['contents']=$v->contents;
  75. $re[$k]['created_at']=strtotime($v->created_at)*1000;
  76. $re[$k]['like_num']=$v->like;
  77. $re[$k]['imgurl']=json_encode($v->imgurl);
  78. // $re[$k]['like']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('like'))==1?true:false;
  79. // $re[$k]['collection']=(ArticleLike::where('uid',$user->id)->where('article_id',$v->id)->value('collection'))==1?true:false;
  80. }
  81. $da=array_merge($res,$re);
  82. }
  83. $total=count($da);
  84. Log::error('{-----点赞----------}');
  85. return $this->success_list($da,'',$total);
  86. }
  87. public function show_detail(Request $request){
  88. $input=$request->all();
  89. $data=Article::find($input['id']);
  90. // if ($request->type==2){
  91. //
  92. // }else{
  93. // if ($request->type==1){
  94. // $type='created_at';
  95. // }else{
  96. // $type='like';
  97. // }
  98. // $data=Article::orderByDesc("$type")->orderbyDesc('type')->where('label','<>','1')->where('status',1)->paginate($page_size);
  99. // }
  100. $user=Auth::user();
  101. $data->avatar='https://api.jiuweiyun.cn/public/uploads/images/topics/916.jpg';
  102. // $data->imgurl=json_encode($data->imgurl);
  103. $data->created_at=strtotime($data->created_at)*1000;
  104. $data->off=1;
  105. $data->like=(ArticleLike::where('uid',$user->id)->where('article_id',$data->id)->value('like'))==1?true:false;
  106. $data->collection=(ArticleLike::where('uid',$user->id)->where('article_id',$data->id)->value('collection'))==1?true:false;
  107. return $this->success($data);
  108. }
  109. /*
  110. * 点赞与收藏
  111. * */
  112. public function like(Request $request){
  113. $user=Auth::user();
  114. $re=ArticleLike::where('uid',$user->id)->where('article_id',$request->id)->first();
  115. if ($re){
  116. $article=ArticleLike::find($re->id);
  117. if ($request->type==1){
  118. if ($request->status==1){
  119. $article->collection=1;
  120. }else{
  121. $article->collection=0;
  122. }
  123. }else{
  124. if ($request->status==1){
  125. $article->like=1;
  126. $articles=Article::find($request->id);
  127. $articles->like=($articles->like)+1;
  128. }else{
  129. $article->like=0;
  130. $articles=Article::find($request->id);
  131. $articles->like=($articles->like)-1;
  132. $articles->save();
  133. }
  134. }
  135. $article->save();
  136. }else{
  137. $article=new ArticleLike();
  138. $article->uid=$user->id;
  139. $article->article_id=$request->id;
  140. if ($request->type==1){
  141. if ($request->status==1){
  142. $article->collection=1;
  143. }else{
  144. $article->collection=0;
  145. }
  146. }else{
  147. if ($request->status==1){
  148. $article->like=1;
  149. $articles=Article::find($request->id);
  150. $articles->like=($articles->like)+1;
  151. }else{
  152. $article->like=0;
  153. $articles=Article::find($request->id);
  154. $articles->like=($articles->like)-1;
  155. $articles->save();
  156. }
  157. }
  158. $article->save();
  159. }
  160. return $this->success([]);
  161. }
  162. /**
  163. *我的收藏
  164. */
  165. public function mycollection()
  166. {
  167. $user = Auth::user();
  168. $data = ArticleLike::where('uid', $user->id)->where('collection', '=', 1)->paginate(2);
  169. $datas=$data->toArray();
  170. $re=[];
  171. foreach ($data as $k => $v) {
  172. $re[$k]['id'] = $v->article_id;
  173. $article=Article::find($v->article_id);
  174. if ($article){
  175. $re[$k]['off']=1;
  176. $re[$k]['avatar'] = 'https://api.jiuweiyun.cn/public/uploads/images/topics/916.jpg';
  177. $re[$k]['title'] =$article->title;
  178. $re[$k]['author'] = $article->author;
  179. $re[$k]['contents'] = $article->contents;
  180. $re[$k]['created_at'] = strtotime($article->created_at)*1000;
  181. $re[$k]['like_num'] = $article->like;
  182. $re[$k]['type']=$article->type;
  183. $re[$k]['summary']=$article->summary;
  184. $re[$k]['type'] = $article->type;
  185. $re[$k]['imgurl'] = json_encode($article->imgurl);
  186. $re[$k]['collection'] = $v->collection == 1 ? true : false;
  187. $re[$k]['like'] = $v->like == 1 ? true : false;
  188. }
  189. }
  190. // Log::error(json_encode($re));
  191. return $this->success_list($re,'',$datas['total']);
  192. }
  193. public function article_copy(Request $request){
  194. $article=Article::find($request->id);
  195. return make_excerpt($article->contents,10000000);
  196. }
  197. }