AnniversaryController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers\Service;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Cache;
  6. use App\Http\Controllers\Controller;
  7. /*
  8. * 한국천문연구원_특일 정보
  9. * https://www.data.go.kr/data/15012690/openapi.do
  10. */
  11. class AnniversaryController extends Controller
  12. {
  13. const API_KEY = 'vQz8tIxrdhjerG6DE1w1hcVEli5S27LtIsCvx0axiieZmRgOpB4vToQ77VmvknAkIC9YjxlPx2gDZcl06S88Xw==';
  14. const API_HOST = 'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService';
  15. const OPERATION = [
  16. 1 => 'getHoliDeInfo', // 국경일
  17. 2 => 'getRestDeInfo', // 공휴일
  18. 3 => 'getAnniversaryInfo', // 기념일
  19. 4 => 'get24DivisionsInfo', // 24절기
  20. 5 => 'getSundryDayInfo' // 잡절
  21. ];
  22. const MAP_OPERATION = [
  23. 1 => '국경일',
  24. 2 => '공휴일',
  25. 3 => '기념일',
  26. 4 => '24절기',
  27. 5 => '잡절'
  28. ];
  29. public function __construct()
  30. {
  31. $this->middleware('front');
  32. }
  33. /**
  34. * 공휴일/특일
  35. * @method GET
  36. * @see /service/anniversary
  37. */
  38. public function index(Request $request)
  39. {
  40. $year = $request->get('year', now()->format('Y'));
  41. $data[] = null;
  42. foreach(self::OPERATION as $i => $operation) {
  43. $cacheName = sprintf('anniversary-%d-%d', $year, $i);
  44. if (!$data[$i] = Cache::get($cacheName)) {
  45. $response = Http::get(self::API_HOST . DIRECTORY_SEPARATOR . $operation, [
  46. 'ServiceKey' => self::API_KEY,
  47. 'solYear' => $year,
  48. '_type' => 'json',
  49. 'numOfRows' => 100
  50. ]);
  51. $total = 0;
  52. $list = null;
  53. if ($response->ok()) {
  54. $response = $response->json('response')['body'];
  55. if(($total = $response['totalCount']) > 0) {
  56. if($total == 1) {
  57. $result = [$response['items']['item']];
  58. }else{
  59. $result = $response['items']['item'];
  60. }
  61. foreach($result as $item) {
  62. $item['locdate'] = date_format(date_create($item['locdate']), 'Y.m.d');
  63. $item['isHoliday'] = ($item['isHoliday'] == 'Y' ? '예' : '아니오');
  64. $list[] = $item;
  65. }
  66. }
  67. }
  68. $data[$i] = [
  69. 'total' => $total,
  70. 'list' => $list
  71. ];
  72. Cache::put($cacheName, $data[$i], CACHE_EXPIRE_TIME);
  73. }
  74. }
  75. return view(layout('service.anniversary.index'), [
  76. 'menuID' => 'anniversary',
  77. 'data' => $data,
  78. 'year' => $year,
  79. 'mapOperation' => self::MAP_OPERATION
  80. ]);
  81. }
  82. }