NewsController.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. //文章列表
  30. $news = News::where("visible", 1);
  31. if($month > 0 || $year > 0){
  32. $dateFilter = Carbon::now();
  33. if($year > 0){
  34. $dateFilter->setYear(intval($year));
  35. if($month > 0){
  36. $dateFilter->setMonth(intval($month));
  37. $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfMonth(), $dateFilter->copy()->endOfMonth()]);
  38. }else{
  39. $news = $news->whereBetween("post_date", [$dateFilter->copy()->startOfYear(), $dateFilter->copy()->endOfYear()]);
  40. }
  41. }
  42. }
  43. $news = $news->orderByDesc("order")->orderByDesc("post_date");
  44. $result["pagination"] = [
  45. "page" => $page,
  46. "per_page" => $per_page
  47. ];
  48. if($per_page == -1){
  49. $news = $news->get();
  50. $result["pagination"]["total"] = $news->count();
  51. $result["pagination"]["total_page"] =1;
  52. }else{
  53. $news = $news->paginate($per_page, ['*'], 'page', $page);
  54. $result["pagination"]["total"] = $news->total();
  55. $result["pagination"]["total_page"] =$news->lastPage();
  56. }
  57. foreach($news as $item){
  58. $result["list"][] = [
  59. "id" => $item->id,
  60. "categoryId" => $item->newsCategory->id,
  61. "category" => $item->newsCategory->getTranslation("name", $locate, false),
  62. "postDate" => Carbon::parse($item->post_date)->format("Y.m.d"),
  63. "title" => $item->getTranslation("title", $locate, false),
  64. "imgPC" => $item->news_img_pc_url,
  65. "imgMobile" => $item->news_img_mobile_url,
  66. "written" => $item->getTranslation("written_by", $locate, false),
  67. "description" => $item->getTranslation("description", $locate, false),
  68. ];
  69. }
  70. return response()->json(["data" => $result], 200);
  71. }
  72. public function getTop(Request $request, $locate = 'tw')
  73. {
  74. $categoryId = $request->input("categoryId", "");
  75. $locate = $locate == "tw" ? "zh_TW" : $locate;
  76. $result = [];
  77. //年份清單
  78. $yearList = News::select(DB::raw("DATE_FORMAT(post_date, '%Y/%m') as years"))->distinct()->orderBy("years", "desc")->pluck("years");
  79. //文章列表
  80. $news = News::where("visible", 1)->where('on_top', 1)->first();
  81. if($news)
  82. $result["data"] = [
  83. "meta" => [
  84. "meta_title" => $news->getTranslation("meta_title", $locate, false),
  85. "meta_keyword" => $news->getTranslation("meta_keyword", $locate, false),
  86. "meta_description" => $news->getTranslation("meta_description", $locate, false),
  87. "imgPC" => $news->news_meta_img,
  88. ],
  89. "yearList" => $yearList,
  90. "top" => [
  91. "id" => $news->id,
  92. "categoryId" => $news->newsCategory->id,
  93. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  94. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  95. "title" => $news->getTranslation("title", $locate, false),
  96. "imgPC" => $news->news_img_pc_url,
  97. "imgMobile" => $news->news_img_mobile_url,
  98. "written" => $news->getTranslation("written_by", $locate, false),
  99. "description" => $news->getTranslation("description", $locate, false),
  100. ]
  101. ];
  102. return response()->json($result, 200);
  103. }
  104. public function detail($locale = 'tw', $id){
  105. $locate = $locale == "tw" ? "zh_TW" : $locale;
  106. $news = News::find($id);
  107. $otherNewsList = [];
  108. //取得前一篇
  109. $previous = News::where(function ($query) use ($news) {
  110. $query->where('order', $news->order)
  111. ->where('post_date', $news->post_date)
  112. ->where('id', '>', $news->id);
  113. })
  114. ->orWhere(function ($query) use ($news) {
  115. $query->where('order', $news->order)
  116. ->where('post_date', '>', $news->post_date);
  117. })
  118. ->orWhere(function ($query) use ($news) {
  119. $query->where('order', '>', $news->order);
  120. })
  121. ->orderBy('order', 'asc')
  122. ->orderBy('post_date', 'asc')
  123. ->orderBy('id', 'asc')
  124. ->first();
  125. //取得下一篇
  126. $next = News::where(function ($query) use ($news) {
  127. $query->where('order', $news->order)
  128. ->where('post_date', $news->post_date)
  129. ->where('id', '<', $news->id);
  130. })
  131. ->orWhere(function ($query) use ($news) {
  132. $query->where('order', $news->order)
  133. ->where('post_date', '<', $news->post_date);
  134. })
  135. ->orWhere(function ($query) use ($news) {
  136. $query->where('order', '<', $news->order);
  137. })
  138. ->orderBy('order', 'desc')
  139. ->orderBy('post_date', 'desc')
  140. ->orderBy('id', 'desc')
  141. ->first();
  142. if($previous)$otherNewsList["previous"] = [
  143. "id" => $previous->id,
  144. "categoryId" => $previous->newsCategory->id,
  145. "category" => $previous->newsCategory->getTranslation("name", $locate, false),
  146. "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
  147. "title" => $previous->getTranslation("title", $locate, false),
  148. "imgPC" => $previous->news_img_pc_url,
  149. "imgMobile" => $previous->news_img_mobile_url,
  150. "written" => $previous->getTranslation("written_by", $locate, false),
  151. "description" => $previous->getTranslation("description", $locate, false),
  152. ];
  153. if($next)$otherNewsList["next"] = [
  154. "id" => $next->id,
  155. "categoryId" => $next->newsCategory->id,
  156. "category" => $next->newsCategory->getTranslation("name", $locate, false),
  157. "postDate" => Carbon::parse($previous->post_date)->format("Y/m/d"),
  158. "title" => $next->getTranslation("title", $locate, false),
  159. "imgPC" => $next->news_img_pc_url,
  160. "imgMobile" => $next->news_img_mobile_url,
  161. "written" => $next->getTranslation("written_by", $locate, false),
  162. "description" => $next->getTranslation("description", $locate, false),
  163. ];
  164. // {
  165. // "type": "text",
  166. // "content": ""
  167. // },
  168. // {
  169. // "type": "image",
  170. // "images":[{"img_url":"", "alt":""}, {"img_url":"", "alt":""}]
  171. // },
  172. // {
  173. // "type": "video",
  174. // "is_yt":false,
  175. // "video_img_url":"",
  176. // "video_url":"",
  177. // "content": ""
  178. // }
  179. $paragraphs = [];
  180. foreach($news->paragraphs as $paragraph){
  181. $content = [];
  182. switch ($paragraph->contentType()){
  183. case "text":
  184. $paragraphs[] = [
  185. "type" => $paragraph->contentType(),
  186. "content" => nl2br($paragraph->getTranslation("text_content", $locate, false)),
  187. ];
  188. break;
  189. case "image":
  190. $photos = $paragraph->photos;
  191. $images = [];
  192. foreach($photos as $photo){
  193. $images[] = ["img_url" => $photo->img_url, "alt" => $photo->getTranslation("image_alt", $locate, false)];
  194. }
  195. $paragraphs[] = [
  196. "type" => $paragraph->contentType(),
  197. "images" => $images,
  198. ];
  199. break;
  200. case "video":
  201. $paragraphs[] = [
  202. "type" => $paragraph->contentType(),
  203. "video_img_url" => $paragraph->paragraph_video_img,
  204. "is_yt" => $paragraph->video_type == 1 ? true : false,
  205. "content" => $paragraph->getTranslation("video_img_alt", $locate, false),
  206. "video_url" => $paragraph->paragraph_video_url,
  207. ];
  208. break;
  209. }
  210. }
  211. $result = [
  212. "categoryId" => $news->newsCategory->id,
  213. "category" => $news->newsCategory->getTranslation("name", $locate, false),
  214. "postDate" => Carbon::parse($news->post_date)->format("Y/m/d"),
  215. "title" => $news->getTranslation("title", $locate, false),
  216. "description" => $news->getTranslation("description", $locate, false),
  217. "written" => $news->getTranslation("written_by", $locate, false),
  218. "imgPC" => $news->news_img_pc_url,
  219. "imgMobile" => $news->news_img_mobile_url,
  220. "metaTitle" => $news->getTranslation("meta_title", $locate, false),
  221. "metaKeyword" => $news->getTranslation("meta_keyword", $locate, false),
  222. "metaDesc" => $news->getTranslation("meta_description", $locate, false),
  223. "metaImg" => $news->meta_img,
  224. "paragraphs" => $paragraphs,
  225. "otherNews" => $otherNewsList
  226. ];
  227. return response()->json(["data" => $result], 200);
  228. }
  229. }