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() {
try {
// API에서 최신 프로젝트 데이터 가져오기
const apiUrl = window.API_BASE_URL || 'http://localhost:16080/api';
const response = await fetch(`${apiUrl}/projects/`, {
headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
'Content-Type': 'application/json'
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';
})();
// ProjectsAPI 사용 (모든 프로젝트 로드)
projects = await ProjectsAPI.getAll(false);
console.log('프로젝트 로드 완료:', projects.length, '개');
console.log('활성 프로젝트:', projects.filter(p => p.is_active).length, '개');
if (response.ok) {
projects = await response.json();
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, '개');
}
}
// localStorage에도 캐시 저장
localStorage.setItem('work-report-projects', JSON.stringify(projects));
} catch (error) {
console.error('프로젝트 로드 오류:', error);
// 오류 시 localStorage에서 로드

View File

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

View File

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

View File

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

View File

@@ -6,8 +6,8 @@ const API_BASE_URL = (() => {
console.log('🔧 API URL 생성 - hostname:', hostname, 'protocol:', protocol, 'port:', port);
// 로컬 환경 (포트 있음)
if (port === '16080') {
// 로컬 환경 (localhost 또는 127.0.0.1이고 포트 있음)
if ((hostname === 'localhost' || hostname === '127.0.0.1') && port) {
const url = `${protocol}//${hostname}:${port}/api`;
console.log('🏠 로컬 환경 URL:', 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
const ReportsAPI = {
getSummary: (startDate, endDate) => apiRequest('/reports/summary', {

View File

@@ -46,7 +46,13 @@ class PagePermissionManager {
try {
// 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`, {
headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`