123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
-
- namespace App\Http\Controllers\Api;
-
- use App\Http\Controllers\Controller;
- use App\Models\News;
- use App\Models\NewsCategory;
- use App\Supports\Response;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use App\Http\Helper\Helper;
-
- /**
- * @group Lottery Prize
- */
- class NewsController extends Controller
- {
- public function __construct(
- )
- {
- }
-
- public function list(Request $request, $locate = 'tw')
- {
- $page = $request->input("page", 1);
- $per_page = $request->input("per_page", 15);
- $year = $request->input("year", -1);
- $month = $request->input("month", -1);
- $locate = $locate == "tw" ? "zh_TW" : $locate;
- $result = [];
-
- //文章列表
- $news = News::where("visible", 1);
- if($month > 0 || $year > 0){
- $dateFilter = Carbon::now();
- if($year > 0){
- $dateFilter->setYear(intval($year));
- if($month > 0){
- $dateFilter->setMonth(intval($month));
- $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfMonth(), $dateFilter->copy()->endOfMonth()]);
- }else{
- $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfYear(), $dateFilter->copy()->endOfYear()]);
- }
- }
- }
- $news = $news->orderByDesc("order")->orderByDesc("post_date");
- $result["pagination"] = [
- "page" => $page,
- "per_page" => $per_page
- ];
- if($per_page == -1){
- $news = $news->get();
- $result["pagination"]["total"] = $news->count();
- $result["pagination"]["total_page"] =1;
- }else{
- $news = $news->paginate($per_page, ['*'], 'page', $page);
- $result["pagination"]["total"] = $news->total();
- $result["pagination"]["total_page"] =$news->lastPage();
-
- }
- foreach($news as $item){
- $result["list"][] = [
- "id" => $item->id,
- "categoryId" => $item->newsCategory->id,
- "category" => $item->newsCategory->getTranslation("name", $locate, false),
- "postDate" => Carbon::parse($item->post_date)->format("Y.m.d"),
- "title" => $item->getTranslation("title", $locate, false),
- "imgPC" => $item->news_img_pc_url,
- "imgMobile" => $item->news_img_mobile_url,
- "written" => $item->getTranslation("written_by", $locate, false),
- "description" => $item->getTranslation("description", $locate, false),
- ];
- }
- return response()->json(["data" => $result], 200);
- }
-
- public function getTop(Request $request, $locate = 'tw')
- {
- $categoryId = $request->input("categoryId", "");
- $locate = $locate == "tw" ? "zh_TW" : $locate;
- $result = [];
-
- //年份清單
- $yearList = News::select(DB::raw("DATE_FORMAT(post_date, '%Y/%m') as years"))->distinct()->orderBy("years", "desc")->pluck("years");
-
- //文章列表
- $news = News::where("visible", 1)->where('on_top', 1)->first();
- if($news)
- $result["data"] = [
- "meta" => [
- "meta_title" => $news->getTranslation("meta_title", $locate, false),
- "meta_keyword" => $news->getTranslation("meta_keyword", $locate, false),
- "meta_description" => $news->getTranslation("meta_description", $locate, false),
- "imgPC" => $news->news_meta_img,
- ],
- "yearList" =>[
- $yearList
- ],
- "top" => [
- "id" => $news->id,
- "categoryId" => $news->newsCategory->id,
- "category" => $news->newsCategory->getTranslation("name", $locate, false),
- "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
- "title" => $news->getTranslation("title", $locate, false),
- "imgPC" => $news->news_img_pc_url,
- "imgMobile" => $news->news_img_mobile_url,
- "written" => $news->getTranslation("written_by", $locate, false),
- "description" => $news->getTranslation("description", $locate, false),
- ]
- ];
- return response()->json($result, 200);
- }
-
- public function detail($locale = 'tw', $id){
- $locate = $locale == "tw" ? "zh_TW" : $locale;
- $news = News::find($id);
- $otherNewsList = [];
-
- //取得前一篇
- $previous = News::where(function ($query) use ($news) {
- $query->where('order', $news->order)
- ->where('post_date', $news->post_date)
- ->where('id', '>', $news->id);
- })
- ->orWhere(function ($query) use ($news) {
- $query->where('order', $news->order)
- ->where('post_date', '>', $news->post_date);
- })
- ->orWhere(function ($query) use ($news) {
- $query->where('order', '>', $news->order);
- })
- ->orderBy('order', 'asc')
- ->orderBy('post_date', 'asc')
- ->orderBy('id', 'asc')
- ->first();
- //取得下一篇
- $next = News::where(function ($query) use ($news) {
- $query->where('order', $news->order)
- ->where('post_date', $news->post_date)
- ->where('id', '<', $news->id);
- })
- ->orWhere(function ($query) use ($news) {
- $query->where('order', $news->order)
- ->where('post_date', '<', $news->post_date);
- })
- ->orWhere(function ($query) use ($news) {
- $query->where('order', '<', $news->order);
- })
- ->orderBy('order', 'desc')
- ->orderBy('post_date', 'desc')
- ->orderBy('id', 'desc')
- ->first();
-
- if($previous)$otherNewsList["previous"] = [
- "id" => $previous->id,
- "categoryId" => $previous->newsCategory->id,
- "category" => $previous->newsCategory->getTranslation("name", $locate, false),
- "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
- "title" => $previous->getTranslation("title", $locate, false),
- "imgPC" => $previous->news_img_pc_url,
- "imgMobile" => $previous->news_img_mobile_url,
- "written" => $previous->getTranslation("written_by", $locate, false),
- "description" => $previous->getTranslation("description", $locate, false),
- ];
- if($next)$otherNewsList["next"] = [
- "id" => $next->id,
- "categoryId" => $next->newsCategory->id,
- "category" => $next->newsCategory->getTranslation("name", $locate, false),
- "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
- "title" => $next->getTranslation("title", $locate, false),
- "imgPC" => $next->news_img_pc_url,
- "imgMobile" => $next->news_img_mobile_url,
- "written" => $next->getTranslation("written_by", $locate, false),
- "description" => $next->getTranslation("description", $locate, false),
- ];
-
-
- // {
- // "type": "text",
- // "content": ""
- // },
- // {
- // "type": "image",
- // "images":[{"img_url":"", "alt":""}, {"img_url":"", "alt":""}]
- // },
- // {
- // "type": "video",
- // "is_yt":false,
- // "video_img_url":"",
- // "video_url":"",
- // "content": ""
- // }
- $paragraphs = [];
- foreach($news->paragraphs as $paragraph){
- $content = [];
- switch ($paragraph->contentType()){
- case "text":
- $paragraphs[] = [
- "type" => $paragraph->contentType(),
- "content" => nl2br($paragraph->getTranslation("text_content", $locate, false)),
- ];
- break;
- case "image":
- $photos = $paragraph->photos;
- $images = [];
- foreach($photos as $photo){
- $images[] = ["img_url" => $photo->img_url, "alt" => $photo->getTranslation("image_alt", $locate, false)];
- }
- $paragraphs[] = [
- "type" => $paragraph->contentType(),
- "images" => $images,
- ];
- break;
- case "video":
- $paragraphs[] = [
- "type" => $paragraph->contentType(),
- "video_img_url" => $paragraph->paragraph_video_img,
- "is_yt" => $paragraph->video_type == 1 ? true : false,
- "content" => $paragraph->getTranslation("video_img_alt", $locate, false),
- "video_url" => $paragraph->paragraph_video_url,
- ];
- break;
- }
- }
- $result = [
- "categoryId" => $news->newsCategory->id,
- "category" => $news->newsCategory->getTranslation("name", $locate, false),
- "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
- "title" => $news->getTranslation("title", $locate, false),
- "description" => $news->getTranslation("description", $locate, false),
- "written" => $news->getTranslation("written_by", $locate, false),
- "imgPC" => $news->news_img_pc_url,
- "imgMobile" => $news->news_img_mobile_url,
- "metaTitle" => $news->getTranslation("meta_title", $locate, false),
- "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
- "metaDesc" => $news->getTranslation("meta_description", $locate, false),
- "metaImg" => $news->meta_img,
- "paragraphs" => $paragraphs,
- "otherNews" => $otherNewsList
- ];
- return response()->json(["data" => $result], 200);
- }
- }
|