123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. "img_pc" => $item->news_img_pc_url,
  74. "img_mobile" => $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. $locate = $locate == "tw" ? "zh_TW" : $locate;
  84. $result = [];
  85. //年份清單
  86. $yearList = News::select(DB::raw("DATE_FORMAT(post_date, '%Y/%m') as years"))->distinct()->orderBy("years", "desc")->pluck("years");
  87. //文章列表
  88. $news = News::where("visible", 1)->where('on_top', 1)->first();
  89. if($news)
  90. $result["data"] = [
  91. "meta" => [
  92. "meta_title" => $news->getTranslation("meta_title", $locate, false),
  93. "meta_keyword" => $news->getTranslation("meta_keyword", $locate, false),
  94. "meta_description" => $news->getTranslation("meta_description", $locate, false),
  95. "img_pc" => $news->news_meta_img,
  96. ],
  97. "yearList" => $yearList,
  98. "top" => [
  99. "id" => $news->id,
  100. "categoryId" => $news->newsCategory->id,
  101. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  102. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  103. "title" => $news->getTranslation("title", $locate, false),
  104. "img_pc" => $news->news_img_pc_url,
  105. "img_mobile" => $news->news_img_mobile_url,
  106. "written" => $news->getTranslation("written_by", $locate, false),
  107. "description" => $news->getTranslation("description", $locate, false),
  108. ]
  109. ];
  110. return response()->json($result, 200);
  111. }
  112. public function detail(Request $request, $locale = 'tw', $id){
  113. $isPreview = $request->input("preview","");
  114. $now = Carbon::now();
  115. if($isPreview){
  116. $news = News::where('id', $id)
  117. ->first();
  118. }else{
  119. $news = News::where('id', $id)
  120. ->where("visible", 1)
  121. ->where(fn($q) =>
  122. $q->whereNull('start_date')
  123. ->orWhere('start_date', '<=', $now)
  124. )->where(fn($q) =>
  125. $q->whereNull('end_date')
  126. ->orWhere('end_date', '>=', $now)
  127. )
  128. ->first();
  129. }
  130. $locate = $locale == "tw" ? "zh_TW" : $locale;
  131. $otherNewsList = [];
  132. if(!$news){
  133. return response()->json([
  134. 'message' => "news not found"], 404);
  135. }
  136. // dd($news);
  137. //取得前一篇
  138. $previous = News::where(function ($query) use ($news, $now) {
  139. $query->where('order', $news->order)
  140. ->where('post_date', $news->post_date)
  141. ->where('id', '>', $news->id)
  142. ->where("visible", 1)
  143. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  144. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  145. })
  146. ->orWhere(function ($query) use ($news, $now) {
  147. $query->where('order', $news->order)
  148. ->where('post_date', '>', $news->post_date)
  149. ->where("visible", 1)
  150. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  151. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  152. })
  153. ->orWhere(function ($query) use ($news, $now) {
  154. $query->where('order', '>', $news->order)
  155. ->where("visible", 1)
  156. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  157. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  158. })
  159. ->orderBy('order', 'asc')
  160. ->orderBy('post_date', 'asc')
  161. ->orderBy('id', 'asc')
  162. ->first();
  163. //取得下一篇
  164. $next = News::where(function ($query) use ($news, $now) {
  165. $query->where('order', $news->order)
  166. ->where('post_date', $news->post_date)
  167. ->where('id', '<', $news->id)
  168. ->where("visible", 1)
  169. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  170. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  171. })
  172. ->orWhere(function ($query) use ($news, $now) {
  173. $query->where('order', $news->order)
  174. ->where('post_date', '<', $news->post_date)
  175. ->where("visible", 1)
  176. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  177. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  178. })
  179. ->orWhere(function ($query) use ($news, $now) {
  180. $query->where('order', '<', $news->order)
  181. ->where("visible", 1)
  182. ->where(fn($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
  183. ->where(fn($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
  184. })
  185. ->orderBy('order', 'desc')
  186. ->orderBy('post_date', 'desc')
  187. ->orderBy('id', 'desc')
  188. ->first();
  189. if($previous)$otherNewsList["previous"] = [
  190. "id" => $previous->id,
  191. "categoryId" => $previous->newsCategory->id,
  192. "category" => $previous->newsCategory->getTranslation("name", $locate, false),
  193. "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
  194. "title" => $previous->getTranslation("title", $locate, false),
  195. "img_pc" => $previous->news_img_pc_url,
  196. "img_mobile" => $previous->news_img_mobile_url,
  197. "written" => $previous->getTranslation("written_by", $locate, false),
  198. "description" => $previous->getTranslation("description", $locate, false),
  199. ];
  200. if($next)$otherNewsList["next"] = [
  201. "id" => $next->id,
  202. "categoryId" => $next->newsCategory->id,
  203. "category" => $next->newsCategory->getTranslation("name", $locate, false),
  204. "postDate" => Carbon::parse($next->post_date)->format("Y/m/d"),
  205. "title" => $next->getTranslation("title", $locate, false),
  206. "img_pc" => $next->news_img_pc_url,
  207. "img_mobile" => $next->news_img_mobile_url,
  208. "written" => $next->getTranslation("written_by", $locate, false),
  209. "description" => $next->getTranslation("description", $locate, false),
  210. ];
  211. $paragraphs = [];
  212. foreach($news->paragraphs as $paragraph){
  213. $content = [];
  214. switch ($paragraph->contentType()){
  215. case "text":
  216. $paragraphs[] = [
  217. "type" => $paragraph->contentType(),
  218. "content" => nl2br($paragraph->getTextContent($locate)),
  219. ];
  220. break;
  221. case "image":
  222. $photos = $paragraph->photos;
  223. $images = [];
  224. foreach($photos as $photo){
  225. $images[] = ["img_url" => $photo->news_photo_img, "alt" => $photo->getTranslation("image_alt", $locate, false)];
  226. }
  227. $paragraphs[] = [
  228. "type" => $paragraph->contentType(),
  229. "images" => $images,
  230. ];
  231. break;
  232. case "video":
  233. $paragraphs[] = [
  234. "type" => $paragraph->contentType(),
  235. "video_img_url" => $paragraph->paragraph_video_img,
  236. "is_yt" => $paragraph->video_type == 1 ? true : false,
  237. "content" => $paragraph->getTranslation("video_img_alt", $locate, false),
  238. "video_url" => $paragraph->getTranslation("link", $locate, false),
  239. ];
  240. break;
  241. }
  242. }
  243. $result = [
  244. "categoryId" => $news->newsCategory->id,
  245. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  246. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  247. "title" => $news->getTranslation("title", $locate, false),
  248. "description" => $news->getTranslation("description", $locate, false),
  249. "written" => $news->getTranslation("written_by", $locate, false),
  250. "img_pc" => $news->news_img_pc_url,
  251. "img_mobile" => $news->news_img_mobile_url,
  252. "metaTitle" => $news->getTranslation("meta_title", $locate, false),
  253. "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
  254. "metaDesc" => $news->getTranslation("meta_description", $locate, false),
  255. "metaImg" => $news->meta_img,
  256. "paragraphs" => $paragraphs
  257. ];
  258. if(!empty($otherNewsList)) $result["otherNews"] = $otherNewsList;
  259. return response()->json(["data" => $result], 200);
  260. }
  261. }