forked from AcaMate/AcaMate_Web
1. 로그인 동작을 위해서 viewmodel 로 관련 뷰에서 동작할 모든 로직을 viewmodel에서 관리 1.1. view 와 viewmodel의 관계는 1:N으로 동작하는것을 기반으로 두고 있음 2. API 접근하는 방식도 웹만의 접근 방법에서 수정 3. 로그인 동작 정보 받는 로직 수정
112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
window.postWithHeader = function(url, method, headerKey, headerValue) {
|
|
fetch(url, {
|
|
method: method,
|
|
headers: {
|
|
[headerKey] : headerValue
|
|
}
|
|
}).then(res => {
|
|
if (res.redirected) {
|
|
window.location.href = res.url;
|
|
}
|
|
});
|
|
};
|
|
|
|
window.fetchWithHeaderAndReturnUrl = async function(args) {
|
|
try {
|
|
let url = args.url;
|
|
const queryParams = Object.entries(args)
|
|
.filter(([key]) => !['url', 'method', 'headerKey', 'headerValue'].includes(key))
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
.join('&');
|
|
if (queryParams) {
|
|
url += (url.includes('?') ? '&' : '?') + queryParams;
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: args.method,
|
|
headers: {
|
|
[args.headerKey]: args.headerValue
|
|
}
|
|
});
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
if (!response.ok) {
|
|
console.error('API 호출 실패:', response.status, response.statusText);
|
|
return null;
|
|
}
|
|
if (!contentType || !contentType.includes('application/json')) {
|
|
const text = await response.text();
|
|
console.error('JSON이 아닌 응답:', text);
|
|
return null;
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log('API 응답 데이터:', data);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('API 호출 중 오류 발생:', error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
window.loadConfig = async function(configFile) {
|
|
try {
|
|
console.log('설정 파일 로드 시도:', configFile);
|
|
const response = await fetch(configFile);
|
|
if (!response.ok) {
|
|
console.error('설정 파일 로드 실패:', response.status, response.statusText);
|
|
return null;
|
|
}
|
|
const config = await response.json();
|
|
console.log('설정 파일 로드 성공:', configFile);
|
|
return config;
|
|
} catch (error) {
|
|
console.error('설정 파일 로드 중 오류 발생:', error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
window.encryptText = async function(text, key, iv) {
|
|
try {
|
|
// XOR 암호화 구현
|
|
let result = '';
|
|
for (let i = 0; i < text.length; i++) {
|
|
const charCode = text.charCodeAt(i) ^ key.charCodeAt(i % key.length);
|
|
result += String.fromCharCode(charCode);
|
|
}
|
|
|
|
// UTF-8로 인코딩 후 Base64로 변환
|
|
const utf8Encoder = new TextEncoder();
|
|
const bytes = utf8Encoder.encode(result);
|
|
const base64 = btoa(String.fromCharCode.apply(null, bytes));
|
|
return base64;
|
|
} catch (error) {
|
|
console.error('암호화 중 오류 발생:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
window.decryptText = async function(encryptedText, key, iv) {
|
|
try {
|
|
// Base64 디코딩 후 UTF-8 디코딩
|
|
const binary = atob(encryptedText);
|
|
const bytes = new Uint8Array(binary.length);
|
|
for (let i = 0; i < binary.length; i++) {
|
|
bytes[i] = binary.charCodeAt(i);
|
|
}
|
|
const utf8Decoder = new TextDecoder();
|
|
const text = utf8Decoder.decode(bytes);
|
|
|
|
// XOR 복호화
|
|
let result = '';
|
|
for (let i = 0; i < text.length; i++) {
|
|
const charCode = text.charCodeAt(i) ^ key.charCodeAt(i % key.length);
|
|
result += String.fromCharCode(charCode);
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
console.error('복호화 중 오류 발생:', error);
|
|
throw error;
|
|
}
|
|
};
|