NewsController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\News;
  5. use App\Models\NewsCategory;
  6. use App\Supports\Response;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use App\Http\Helper\Helper;
  12. /**
  13. * @group Lottery Prize
  14. */
  15. class NewsController extends Controller
  16. {
  17. public function __construct(
  18. )
  19. {
  20. }
  21. public function list(Request $request, $locate = 'tw')
  22. {
  23. $page = $request->input("page", 1);
  24. $per_page = $request->input("per_page", 15);
  25. $year = $request->input("year", -1);
  26. $month = $request->input("month", -1);
  27. $locate = $locate == "tw" ? "zh_TW" : $locate;
  28. $result = [];
  29. $dateFilter = Carbon::now();
  30. $now = Carbon::now();
  31. //文章列表
  32. $news = News::where("visible", 1);
  33. if($month > 0 || $year > 0){
  34. if($year > 0){
  35. $dateFilter->setYear(intval($year));
  36. if($month > 0){
  37. $dateFilter->setMonth(intval($month));
  38. $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfMonth(), $dateFilter->copy()->endOfMonth()]);
  39. }else{
  40. $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfYear(), $dateFilter->copy()->endOfYear()]);
  41. }
  42. }
  43. }
  44. $news = $news->where(fn($q) =>
  45. $q->whereNull('start_date')
  46. ->orWhere('start_date', '<=', $now)
  47. )->where(fn($q) =>
  48. $q->whereNull('end_date')
  49. ->orWhere('end_date', '>=', $now)
  50. );
  51. $news = $news->orderByDesc("order")->orderByDesc("post_date");
  52. $result["pagination"] = [
  53. "page" => $page,
  54. "per_page" => $per_page
  55. ];
  56. if($per_page == -1){
  57. $news = $news->get();
  58. $result["pagination"]["total"] = $news->count();
  59. $result["pagination"]["total_page"] =1;
  60. }else{
  61. $news = $news->paginate($per_page, ['*'], 'page', $page);
  62. $result["pagination"]["total"] = $news->total();
  63. $result["pagination"]["total_page"] =$news->lastPage();
  64. }
  65. $result["list"] = [];
  66. foreach($news as $item){
  67. $result["list"][] = [
  68. "id" => $item->id,
  69. "categoryId" => $item->newsCategory->id,
  70. "category" => $item->newsCategory->getTranslation("name", $locate, false),
  71. "postDate" => Carbon::parse($item->post_date)->format("Y.m.d"),
  72. "title" => $item->getTranslation("title", $locate, false),
  73. "imgPC" => $item->news_img_pc_url,
  74. "imgMobile" => $item->news_img_mobile_url,
  75. "written" => $item->getTranslation("written_by", $locate, false),
  76. "description" => $item->getTranslation("description", $locate, false),
  77. ];
  78. }
  79. return response()->json(["data" => $result], 200);
  80. }
  81. public function getTop(Request $request, $locate = 'tw')
  82. {
  83. $categoryId = $request->input("categoryId", "");
  84. $locate = $locate == "tw" ? "zh_TW" : $locate;
  85. $result = [];
  86. //年份清單
  87. $yearList = News::select(DB::raw("DATE_FORMAT(post_date, '%Y/%m') as years"))->distinct()->orderBy("years", "desc")->pluck("years");
  88. //文章列表
  89. $news = News::where("visible", 1)->where('on_top', 1)->first();
  90. if($news)
  91. $result["data"] = [
  92. "meta" => [
  93. "meta_title" => $news->getTranslation("meta_title", $locate, false),
  94. "meta_keyword" => $news->getTranslation("meta_keyword", $locate, false),
  95. "meta_description" => $news->getTranslation("meta_description", $locate, false),
  96. "imgPC" => $news->news_meta_img,
  97. ],
  98. "yearList" => $yearList,
  99. "top" => [
  100. "id" => $news->id,
  101. "categoryId" => $news->newsCategory->id,
  102. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  103. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  104. "title" => $news->getTranslation("title", $locate, false),
  105. "imgPC" => $news->news_img_pc_url,
  106. "imgMobile" => $news->news_img_mobile_url,
  107. "written" => $news->getTranslation("written_by", $locate, false),
  108. "description" => $news->getTranslation("description", $locate, false),
  109. ]
  110. ];
  111. return response()->json($result, 200);
  112. }
  113. public function detail(Request $request, $locale = 'tw', $id){
  114. $isPreview = $request->input("preview","");
  115. $now = Carbon::now();
  116. if($isPreview){
  117. $news = News::where('id', $id)
  118. ->first();
  119. }else{
  120. $news = News::where('id', $id)
  121. ->where("visible", 1)
  122. ->where(fn($q) =>
  123. $q->whereNull('start_date')
  124. ->orWhere('start_date', '<=', $now)
  125. )->where(fn($q) =>
  126. $q->whereNull('end_date')
  127. ->orWhere('end_date', '>=', $now)
  128. )
  129. ->first();
  130. }
  131. $locate = $locale == "tw" ? "zh_TW" : $locale;
  132. $otherNewsList = [];
  133. if(!$news){
  134. return response()->json([
  135. 'message' => "news not found"], 404);
  136. }
  137. // dd($news);
  138. //取得前一篇
  139. $previous = News::where(function ($query) use ($news, $now) {
  140. $query->where('order', $news->order)
  141. ->where('post_date', $news->post_date)
  142. ->where('id', '>', $news->id)
  143. ->where("visible", 1)
  144. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  145. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  146. })
  147. ->orWhere(function ($query) use ($news, $now) {
  148. $query->where('order', $news->order)
  149. ->where('post_date', '>', $news->post_date)
  150. ->where("visible", 1)
  151. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  152. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  153. })
  154. ->orWhere(function ($query) use ($news, $now) {
  155. $query->where('order', '>', $news->order)
  156. ->where("visible", 1)
  157. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  158. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  159. })
  160. ->orderBy('order', 'asc')
  161. ->orderBy('post_date', 'asc')
  162. ->orderBy('id', 'asc')
  163. ->first();
  164. //取得下一篇
  165. $next = News::where(function ($query) use ($news, $now) {
  166. $query->where('order', $news->order)
  167. ->where('post_date', $news->post_date)
  168. ->where('id', '<', $news->id)
  169. ->where("visible", 1)
  170. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  171. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  172. })
  173. ->orWhere(function ($query) use ($news, $now) {
  174. $query->where('order', $news->order)
  175. ->where('post_date', '<', $news->post_date)
  176. ->where("visible", 1)
  177. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  178. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  179. })
  180. ->orWhere(function ($query) use ($news, $now) {
  181. $query->where('order', '<', $news->order)
  182. ->where("visible", 1)
  183. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  184. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  185. })
  186. ->orderBy('order', 'desc')
  187. ->orderBy('post_date', 'desc')
  188. ->orderBy('id', 'desc')
  189. ->first();
  190. if($previous)$otherNewsList["previous"] = [
  191. "id" => $previous->id,
  192. "categoryId" => $previous->newsCategory->id,
  193. "category" => $previous->newsCategory->getTranslation("name", $locate, false),
  194. "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
  195. "title" => $previous->getTranslation("title", $locate, false),
  196. "imgPC" => $previous->news_img_pc_url,
  197. "imgMobile" => $previous->news_img_mobile_url,
  198. "written" => $previous->getTranslation("written_by", $locate, false),
  199. "description" => $previous->getTranslation("description", $locate, false),
  200. ];
  201. if($next)$otherNewsList["next"] = [
  202. "id" => $next->id,
  203. "categoryId" => $next->newsCategory->id,
  204. "category" => $next->newsCategory->getTranslation("name", $locate, false),
  205. "postDate" => Carbon::parse($next->post_date)->format("Y/m/d"),
  206. "title" => $next->getTranslation("title", $locate, false),
  207. "imgPC" => $next->news_img_pc_url,
  208. "imgMobile" => $next->news_img_mobile_url,
  209. "written" => $next->getTranslation("written_by", $locate, false),
  210. "description" => $next->getTranslation("description", $locate, false),
  211. ];
  212. $paragraphs = [];
  213. foreach($news->paragraphs as $paragraph){
  214. $content = [];
  215. switch ($paragraph->contentType()){
  216. case "text":
  217. $paragraphs[] = [
  218. "type" => $paragraph->contentType(),
  219. "content" => nl2br($paragraph->getTextContent($locate)),
  220. ];
  221. break;
  222. case "image":
  223. $photos = $paragraph->photos;
  224. $images = [];
  225. foreach($photos as $photo){
  226. $images[] = ["img_url" => $photo->news_photo_img, "alt" => $photo->getTranslation("image_alt", $locate, false)];
  227. }
  228. $paragraphs[] = [
  229. "type" => $paragraph->contentType(),
  230. "images" => $images,
  231. ];
  232. break;
  233. case "video":
  234. $paragraphs[] = [
  235. "type" => $paragraph->contentType(),
  236. "video_img_url" => $paragraph->paragraph_video_img,
  237. "is_yt" => $paragraph->video_type == 1 ? true : false,
  238. "content" => $paragraph->getTranslation("video_img_alt", $locate, false),
  239. "video_url" => $paragraph->paragraph_video_url,
  240. ];
  241. break;
  242. }
  243. }
  244. $result = [
  245. "categoryId" => $news->newsCategory->id,
  246. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  247. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  248. "title" => $news->getTranslation("title", $locate, false),
  249. "description" => $news->getTranslation("description", $locate, false),
  250. "written" => $news->getTranslation("written_by", $locate, false),
  251. "imgPC" => $news->news_img_pc_url,
  252. "imgMobile" => $news->news_img_mobile_url,
  253. "metaTitle" => $news->getTranslation("meta_title", $locate, false),
  254. "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
  255. "metaDesc" => $news->getTranslation("meta_description", $locate, false),
  256. "metaImg" => $news->meta_img,
  257. "paragraphs" => $paragraphs
  258. ];
  259. if(!empty($otherNewsList)) $result["otherNews"] = $otherNewsList;
  260. return response()->json(["data" => $result], 200);
  261. }
  262. }