client.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use client';
  2. import { clsx, type ClassValue } from 'clsx';
  3. import { twMerge } from 'tailwind-merge';
  4. import { TokenData, ResultDto } from '@/dtos/response/common';
  5. import { CLAIM_NAME_IDENTIFIER, CLAIM_EMAIL, CLAIM_NAME } from '@/constants/common';
  6. import useAuth from '@/hooks/useAuth';
  7. export function cn(...inputs: ClassValue[]) {
  8. return twMerge(clsx(inputs))
  9. }
  10. // JWT 토큰에서 사용자 정보 추출
  11. export function decodeAccessToken(token: string): TokenData|null {
  12. try {
  13. const base64URL = token.split('.')[1]; // JWT의 Payload 부분
  14. const base64 = base64URL.replace(/-/g, '+').replace(/_/g, '/'); // Base64 형식 변환
  15. const jsonPayload = decodeURIComponent(
  16. atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')
  17. );
  18. const payload = JSON.parse(jsonPayload);
  19. return {
  20. id: payload[CLAIM_NAME_IDENTIFIER] || null,
  21. email: payload[CLAIM_EMAIL] || null,
  22. name: payload[CLAIM_NAME] || null
  23. };
  24. } catch {
  25. return null;
  26. }
  27. }
  28. export function formatDate(dateString: string|null): string {
  29. if (!dateString) {
  30. return '-';
  31. }
  32. const date = new Date(dateString);
  33. const now = new Date();
  34. // 두 자리 숫자 포맷 함수
  35. const pad = (n: number) => n.toString().padStart(2, '0');
  36. // 오늘인지 검사
  37. const isToday =
  38. date.getFullYear() === now.getFullYear() &&
  39. date.getMonth() === now.getMonth() &&
  40. date.getDate() === now.getDate();
  41. if (isToday) {
  42. // 오늘이면 'HH:mm'
  43. const hh = pad(date.getHours());
  44. const mm = pad(date.getMinutes());
  45. return `${hh}:${mm}`;
  46. } else {
  47. // 오늘이 아니면 'YYYY.MM.DD'
  48. const yyyy = date.getFullYear();
  49. const mo = pad(date.getMonth() + 1);
  50. const dd = pad(date.getDate());
  51. return `${yyyy}.${mo}.${dd}`;
  52. }
  53. }
  54. // 2025.01.01. 12:00:00 으로 조회
  55. export function getDateTime(dateString: string|null) {
  56. if (!dateString) {
  57. return '-';
  58. }
  59. const date = new Date(dateString);
  60. return date.toLocaleString('ko-KR', {
  61. year: 'numeric',
  62. month: '2-digit',
  63. day: '2-digit',
  64. hour: '2-digit',
  65. minute: '2-digit',
  66. second: '2-digit',
  67. hour12: false
  68. }).replace(/\. /g, '.');
  69. }
  70. // 파일 이름 생성
  71. export function getDateFilename(prefix: string): string {
  72. if (!prefix) {
  73. return '';
  74. }
  75. const now = new Date();
  76. const yyyy = now.getFullYear();
  77. const MM = String(now.getMonth() + 1).padStart(2, '0');
  78. const dd = String(now.getDate()).padStart(2, '0');
  79. const hh = String(now.getHours()).padStart(2, '0');
  80. const mm = String(now.getMinutes()).padStart(2, '0');
  81. const ss = String(now.getSeconds()).padStart(2, '0');
  82. return `${prefix}-${yyyy}${MM}${dd}${hh}${mm}${ss}.png`;
  83. }
  84. // HTML 태그 제거
  85. export function stripHtmlTags(str?: string|null): string {
  86. if (!str) {
  87. return '';
  88. }
  89. const div = document.createElement('div');
  90. div.innerHTML = str;
  91. return div.textContent || div.innerText || '';
  92. }
  93. // 비밀번호 정책 안내 문구 생성성
  94. export function getPasswordPolicyMessage(policy: {
  95. passwordUppercaseLength: number;
  96. passwordNumbersLength: number;
  97. passwordSpecialcharsLength: number;
  98. }) {
  99. const parts: string[] = [];
  100. if (policy.passwordUppercaseLength > 0) {
  101. parts.push(`영문 대문자 ${policy.passwordUppercaseLength}자 이상`);
  102. }
  103. if (policy.passwordNumbersLength > 0) {
  104. parts.push(`숫자 ${policy.passwordNumbersLength}자 이상`);
  105. }
  106. if (policy.passwordSpecialcharsLength > 0) {
  107. parts.push(`특수문자 ${policy.passwordSpecialcharsLength}자 이상`);
  108. }
  109. if (parts.length > 0) {
  110. return `비밀번호는 ${parts.join(', ')}를 포함해야 합니다.`;
  111. }
  112. return '';
  113. }
  114. // 특수문자 제거
  115. export function filterSpecialCharacters(str: string) {
  116. return str.replace(/[^\p{L}\p{N}\s]/gu, '');
  117. }
  118. // ResultDto에서 첫 번째 오류 조회
  119. export function getError(errors: Record<string, string[]>|null|undefined, index = 0): string|undefined {
  120. if (!errors) {
  121. return undefined;
  122. }
  123. const keys = Object.keys(errors);
  124. if (keys.length > 0) {
  125. const key = keys[index];
  126. return errors[key]?.[0];
  127. }
  128. return undefined;
  129. }
  130. // 서버 응답 메시지 분석
  131. export function throwError<T>(res: ResultDto<T>): void {
  132. if (res.ok) {
  133. return;
  134. }
  135. let message:string|null|undefined = getError(res.errors);
  136. if (!message && !res.message) {
  137. switch (res.status) {
  138. case 400:
  139. message = '잘못된 요청입니다.';
  140. break;
  141. case 401:
  142. message = '로그인 후 이용해 주세요.';
  143. location.replace('/login');
  144. break;
  145. case 403:
  146. message = '권한이 없습니다.';
  147. break;
  148. case 404:
  149. message = '요청하신 페이지를 찾을 수 없습니다.';
  150. break;
  151. case 500:
  152. message = '서버 오류가 발생했습니다. 다시 시도해 주세요.';
  153. break;
  154. }
  155. if ([400, 403, 404, 500].includes(res.status)) {
  156. location.replace('/');
  157. }
  158. } else if(!message || message == '') {
  159. message = res.message;
  160. }
  161. if (message) {
  162. throw new Error(message);
  163. }
  164. }
  165. // 1000회 이상 조회된 게시글인지 확인
  166. export function isHotPost(showHotIcon: boolean, post: { views: number; createdAt: string }): boolean {
  167. return (showHotIcon && post.views >= 1000 && Date.now() - new Date(post.createdAt).getTime() < 7 * 24 * 60 * 60 * 1000);
  168. }
  169. // 24시간 이내에 작성된 게시글인지 확인
  170. export function isNewPost(showNewIcon: boolean, post: { createdAt: string }): boolean {
  171. return (showNewIcon && Date.now() - new Date(post.createdAt).getTime() < 24 * 60 * 60 * 1000);
  172. }
  173. // a 태그에 target="_blank" 추가
  174. export function forceTargetBlank(html: string) {
  175. return html.replace(/<a\s+(?![^>]*target=)([^>]*href=["'][^"']+["'])/gi, '<a target="_blank" rel="noopener noreferrer" $1');
  176. };
  177. // 날짜가 현재 날짜보다 지났는지 확인
  178. export function isDateOverdue(dateString: string|null, days = 0): boolean {
  179. if (!dateString) {
  180. return false;
  181. }
  182. const limitDate = new Date(new Date(dateString));
  183. limitDate.setDate(limitDate.getDate() + days);
  184. const now = new Date();
  185. return now > limitDate;
  186. }
  187. // 로그인 페이지로 이동 시키기
  188. export function loginCheck() {
  189. const { isAuthenticated } = useAuth();
  190. return () => {
  191. if (!isAuthenticated) {
  192. if (confirm("로그인 후 이용 가능합니다.")) {
  193. window.location.href = `/login?returnUrl=${encodeURIComponent(window.location.pathname + window.location.search)}`;
  194. }
  195. return false;
  196. }
  197. return true;
  198. };
  199. }