fix: API URL 하드코딩 문제 해결 및 API 통합 개선

- API URL 생성 로직에서 localhost 환경 감지 개선
- 모든 페이지에서 하드코딩된 API URL 제거
- ManagementAPI, InboxAPI 추가로 API 호출 통합
- ProjectsAPI 사용으로 프로젝트 로드 통일
- permissions.js에서 API URL 동적 생성 적용
This commit is contained in:
hyungi
2025-11-08 15:34:37 +09:00
parent 637b690eda
commit 1299ac261c
6 changed files with 111 additions and 118 deletions

View File

@@ -339,30 +339,20 @@
async function loadProjects() { async function loadProjects() {
try { try {
// API에서 최신 프로젝트 데이터 가져오기 // API에서 최신 프로젝트 데이터 가져오기
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api'; const apiUrl = window.API_BASE_URL || (() => {
const response = await fetch(`${apiUrl}/projects/`, { const hostname = window.location.hostname;
headers: { if (hostname === 'm.hyungi.net') {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`, return 'https://m-api.hyungi.net/api';
'Content-Type': 'application/json'
} }
}); return '/api';
})();
// ProjectsAPI 사용 (모든 프로젝트 로드)
projects = await ProjectsAPI.getAll(false);
console.log('프로젝트 로드 완료:', projects.length, '개');
console.log('활성 프로젝트:', projects.filter(p => p.is_active).length, '개');
if (response.ok) { // localStorage에도 캐시 저장
projects = await response.json(); localStorage.setItem('work-report-projects', JSON.stringify(projects));
console.log('프로젝트 로드 완료:', projects.length, '개');
console.log('활성 프로젝트:', projects.filter(p => p.is_active).length, '개');
// localStorage에도 캐시 저장
localStorage.setItem('work-report-projects', JSON.stringify(projects));
} else {
console.error('프로젝트 로드 실패:', response.status);
// 실패 시 localStorage에서 로드
const saved = localStorage.getItem('work-report-projects');
if (saved) {
projects = JSON.parse(saved);
console.log('캐시에서 프로젝트 로드:', projects.length, '개');
}
}
} catch (error) { } catch (error) {
console.error('프로젝트 로드 오류:', error); console.error('프로젝트 로드 오류:', error);
// 오류 시 localStorage에서 로드 // 오류 시 localStorage에서 로드

View File

@@ -323,20 +323,16 @@
// 데이터 로드 함수들 // 데이터 로드 함수들
async function loadProjects() { async function loadProjects() {
try { try {
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api'; const apiUrl = window.API_BASE_URL || (() => {
const response = await fetch(`${apiUrl}/projects/`, { const hostname = window.location.hostname;
headers: { if (hostname === 'm.hyungi.net') {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`, return 'https://m-api.hyungi.net/api';
'Content-Type': 'application/json'
} }
}); return '/api';
})();
if (response.ok) { // ProjectsAPI 사용
projects = await response.json(); projects = await ProjectsAPI.getAll(false);
updateProjectFilter(); updateProjectFilter();
} else {
throw new Error('프로젝트 목록을 불러올 수 없습니다.');
}
} catch (error) { } catch (error) {
console.error('프로젝트 로드 실패:', error); console.error('프로젝트 로드 실패:', error);
} }
@@ -344,21 +340,18 @@
async function loadInProgressIssues() { async function loadInProgressIssues() {
try { try {
const response = await fetch('/api/issues/admin/all', { const apiUrl = window.API_BASE_URL || (() => {
headers: { const hostname = window.location.hostname;
'Authorization': `Bearer ${localStorage.getItem('access_token')}`, if (hostname === 'm.hyungi.net') {
'Content-Type': 'application/json' return 'https://m-api.hyungi.net/api';
} }
}); return '/api';
})();
if (response.ok) { // ManagementAPI 사용하여 관리함 이슈 로드
const allData = await response.json(); const managementData = await ManagementAPI.getAll();
// 진행 중 상태만 필터링 // 진행 중 상태만 필터링
allIssues = allData.filter(issue => issue.review_status === 'in_progress'); allIssues = managementData.filter(issue => issue.review_status === 'in_progress');
filteredIssues = [...allIssues]; filteredIssues = [...allIssues];
} else {
throw new Error('부적합 목록을 불러올 수 없습니다.');
}
} catch (error) { } catch (error) {
console.error('부적합 로드 실패:', error); console.error('부적합 로드 실패:', error);
} }

View File

@@ -668,24 +668,18 @@
async function loadProjects() { async function loadProjects() {
console.log('🔄 프로젝트 로드 시작'); console.log('🔄 프로젝트 로드 시작');
try { try {
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api'; const apiUrl = window.API_BASE_URL || (() => {
const response = await fetch(`${apiUrl}/projects/`, { const hostname = window.location.hostname;
headers: { if (hostname === 'm.hyungi.net') {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`, return 'https://m-api.hyungi.net/api';
'Content-Type': 'application/json'
} }
}); return '/api';
})();
console.log('📡 프로젝트 API 응답 상태:', response.status); // ProjectsAPI 사용 (모든 프로젝트 로드)
projects = await ProjectsAPI.getAll(false);
if (response.ok) { console.log('✅ 프로젝트 로드 성공:', projects.length, '개');
projects = await response.json(); console.log('📋 프로젝트 목록:', projects);
console.log('✅ 프로젝트 로드 성공:', projects.length, '개'); updateProjectFilter();
console.log('📋 프로젝트 목록:', projects);
updateProjectFilter();
} else {
console.error('❌ 프로젝트 API 응답 실패:', response.status, response.statusText);
}
} catch (error) { } catch (error) {
console.error('❌ 프로젝트 로드 실패:', error); console.error('❌ 프로젝트 로드 실패:', error);
} }

View File

@@ -472,66 +472,41 @@
// 프로젝트 로드 // 프로젝트 로드
async function loadProjects() { async function loadProjects() {
try { try {
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api'; // ProjectsAPI 사용 (모든 프로젝트 로드)
const response = await fetch(`${apiUrl}/projects/`, { projects = await ProjectsAPI.getAll(false);
headers: { updateProjectFilter();
'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
projects = await response.json();
updateProjectFilter();
}
} catch (error) { } catch (error) {
console.error('프로젝트 로드 실패:', error); console.error('프로젝트 로드 실패:', error);
} }
} }
// 부적합 목록 로드 (관리자는 모든 부적합 조회) // 부적합 목록 로드 (관리함 API 사용)
async function loadIssues() { async function loadIssues() {
try { try {
let endpoint = '/api/issues/admin/all'; // ManagementAPI 사용
const managementIssues = await ManagementAPI.getAll();
const response = await fetch(endpoint, { // 수신함에서 넘어온 순서대로 No. 재할당 (reviewed_at 기준)
headers: { managementIssues.sort((a, b) => new Date(a.reviewed_at) - new Date(b.reviewed_at));
'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
'Content-Type': 'application/json' // 프로젝트별로 그룹화하여 No. 재할당
const projectGroups = {};
managementIssues.forEach(issue => {
if (!projectGroups[issue.project_id]) {
projectGroups[issue.project_id] = [];
} }
projectGroups[issue.project_id].push(issue);
}); });
if (response.ok) { // 각 프로젝트별로 순번 재할당
const allIssues = await response.json(); Object.keys(projectGroups).forEach(projectId => {
// 관리함에서는 진행 중(in_progress)과 완료됨(completed) 상태만 표시 projectGroups[projectId].forEach((issue, index) => {
let filteredIssues = allIssues.filter(issue => issue.project_sequence_no = index + 1;
issue.review_status === 'in_progress' || issue.review_status === 'completed'
);
// 수신함에서 넘어온 순서대로 No. 재할당 (reviewed_at 기준)
filteredIssues.sort((a, b) => new Date(a.reviewed_at) - new Date(b.reviewed_at));
// 프로젝트별로 그룹화하여 No. 재할당
const projectGroups = {};
filteredIssues.forEach(issue => {
if (!projectGroups[issue.project_id]) {
projectGroups[issue.project_id] = [];
}
projectGroups[issue.project_id].push(issue);
}); });
});
// 각 프로젝트별로 순번 재할당
Object.keys(projectGroups).forEach(projectId => { issues = managementIssues;
projectGroups[projectId].forEach((issue, index) => { filterIssues();
issue.project_sequence_no = index + 1;
});
});
issues = filteredIssues;
filterIssues();
} else {
throw new Error('부적합 목록을 불러올 수 없습니다.');
}
} catch (error) { } catch (error) {
console.error('부적합 로드 실패:', error); console.error('부적합 로드 실패:', error);
alert('부적합 목록을 불러오는데 실패했습니다.'); alert('부적합 목록을 불러오는데 실패했습니다.');

View File

@@ -6,8 +6,8 @@ const API_BASE_URL = (() => {
console.log('🔧 API URL 생성 - hostname:', hostname, 'protocol:', protocol, 'port:', port); console.log('🔧 API URL 생성 - hostname:', hostname, 'protocol:', protocol, 'port:', port);
// 로컬 환경 (포트 있음) // 로컬 환경 (localhost 또는 127.0.0.1이고 포트 있음)
if (port === '16080') { if ((hostname === 'localhost' || hostname === '127.0.0.1') && port) {
const url = `${protocol}//${hostname}:${port}/api`; const url = `${protocol}//${hostname}:${port}/api`;
console.log('🏠 로컬 환경 URL:', url); console.log('🏠 로컬 환경 URL:', url);
return url; return url;
@@ -256,6 +256,41 @@ const DailyWorkAPI = {
} }
}; };
// Management API
const ManagementAPI = {
getAll: () => apiRequest('/management/'),
update: (issueId, updateData) => apiRequest(`/management/${issueId}`, {
method: 'PUT',
body: JSON.stringify(updateData)
}),
updateAdditionalInfo: (issueId, additionalInfo) => apiRequest(`/management/${issueId}/additional-info`, {
method: 'PUT',
body: JSON.stringify(additionalInfo)
})
};
// Inbox API
const InboxAPI = {
getAll: () => apiRequest('/inbox/'),
review: (issueId, reviewData) => apiRequest(`/inbox/${issueId}/review`, {
method: 'PUT',
body: JSON.stringify(reviewData)
}),
dispose: (issueId, disposeData) => apiRequest(`/inbox/${issueId}/dispose`, {
method: 'PUT',
body: JSON.stringify(disposeData)
}),
updateAdditionalInfo: (issueId, additionalInfo) => apiRequest(`/inbox/${issueId}/additional-info`, {
method: 'PUT',
body: JSON.stringify(additionalInfo)
})
};
// Reports API // Reports API
const ReportsAPI = { const ReportsAPI = {
getSummary: (startDate, endDate) => apiRequest('/reports/summary', { getSummary: (startDate, endDate) => apiRequest('/reports/summary', {

View File

@@ -46,7 +46,13 @@ class PagePermissionManager {
try { try {
// API에서 사용자별 페이지 권한 가져오기 // API에서 사용자별 페이지 권한 가져오기
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api'; const apiUrl = window.API_BASE_URL || (() => {
const hostname = window.location.hostname;
if (hostname === 'm.hyungi.net') {
return 'https://m-api.hyungi.net/api';
}
return '/api';
})();
const response = await fetch(`${apiUrl}/users/${this.currentUser.id}/page-permissions`, { const response = await fetch(`${apiUrl}/users/${this.currentUser.id}/page-permissions`, {
headers: { headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token')}` 'Authorization': `Bearer ${localStorage.getItem('access_token')}`