[251018] fix: EnergyLabel
🕐 커밋 시간: 2025. 10. 18. 23:31:25 📊 변경 통계: • 총 파일: 7개 • 추가: +387줄 • 삭제: -93줄 📁 추가된 파일: + com.twin.app.shoptime/assets/mock/EnergyLabelSample.pdf 📝 수정된 파일: ~ com.twin.app.shoptime/src/actions/actionTypes.js ~ com.twin.app.shoptime/src/actions/convertActions.js ~ com.twin.app.shoptime/src/api/TAxios.js ~ com.twin.app.shoptime/src/components/TItemCard/TItemCard.module.less ~ com.twin.app.shoptime/src/components/TItemCard/TItemCard.new.jsx ~ com.twin.app.shoptime/src/reducers/convertReducer.js 🔧 함수 변경 내용: 📄 com.twin.app.shoptime/src/components/TItemCard/TItemCard.new.jsx (javascript): ✅ Added: hashCode() 🔧 주요 변경 내용: • 타입 시스템 안정성 강화 • 핵심 비즈니스 로직 개선 • API 서비스 레이어 개선 • UI 컴포넌트 아키텍처 개선
This commit is contained in:
@@ -1,72 +1,137 @@
|
||||
import { URLS } from '../api/apiConfig';
|
||||
import { TAxios } from '../api/TAxios';
|
||||
import { get } from '../utils/fp';
|
||||
import { types } from './actionTypes';
|
||||
import { changeAppStatus } from './commonActions';
|
||||
|
||||
/**
|
||||
* PDF를 이미지로 변환하는 액션
|
||||
* PDF를 이미지로 변환
|
||||
* @param {string} pdfUrl - 변환할 PDF URL
|
||||
* @param {function} callback - 성공/실패 후 실행할 콜백 함수
|
||||
* @param {function} callback - 성공/실패 후 실행할 콜백 (error, imageUrl)
|
||||
*/
|
||||
export const convertPdfToImage = (pdfUrl, callback) => (dispatch, getState) => {
|
||||
// 로딩 시작
|
||||
dispatch(changeAppStatus({ showLoadingPanel: { show: true } }));
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE,
|
||||
payload: pdfUrl,
|
||||
});
|
||||
|
||||
const onSuccess = (response) => {
|
||||
const blob = new Blob([response.data], { type: "image/png;charset=UTF-8" });
|
||||
const imageUrl = URL.createObjectURL(blob);
|
||||
console.log("convertPdfToImage onSuccess", response.data);
|
||||
console.log("imageUrl onSuccess", imageUrl);
|
||||
// const imageData = get("data.data", imageUrl);
|
||||
// retCode 체크 (프로젝트 API 규약: 200이어도 retCode로 성공/실패 구분)
|
||||
const retCode = response.headers?.retcode || response.headers?.retCode;
|
||||
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE_SUCCESS,
|
||||
// payload: imageData,
|
||||
payload: imageUrl,
|
||||
});
|
||||
if (retCode !== undefined && retCode !== 0 && retCode !== '0') {
|
||||
const error = new Error(`API Error: retCode=${retCode}`);
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE_FAILURE,
|
||||
payload: { pdfUrl, error },
|
||||
});
|
||||
callback && callback(error, null);
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
|
||||
let imageUrl;
|
||||
try {
|
||||
if (response.data instanceof Blob) {
|
||||
if (response.data.size < 100) {
|
||||
throw new Error('Invalid image data (size too small)');
|
||||
}
|
||||
imageUrl = URL.createObjectURL(response.data);
|
||||
} else if (response.data instanceof ArrayBuffer) {
|
||||
if (response.data.byteLength < 100) {
|
||||
throw new Error('Invalid image data (size too small)');
|
||||
}
|
||||
const blob = new Blob([response.data], { type: 'image/png' });
|
||||
imageUrl = URL.createObjectURL(blob);
|
||||
} else {
|
||||
const blob = new Blob([response.data], { type: 'image/png' });
|
||||
imageUrl = URL.createObjectURL(blob);
|
||||
}
|
||||
|
||||
// 콜백이 있으면 변환된 이미지 데이터와 함께 호출
|
||||
// callback && callback(null, imageData);
|
||||
callback && callback(null, imageUrl);
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE_SUCCESS,
|
||||
payload: { pdfUrl, imageUrl },
|
||||
});
|
||||
|
||||
callback && callback(null, imageUrl);
|
||||
} catch (error) {
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE_FAILURE,
|
||||
payload: { pdfUrl, error },
|
||||
});
|
||||
callback && callback(error, null);
|
||||
}
|
||||
};
|
||||
|
||||
const onFail = (error) => {
|
||||
console.error("convertPdfToImage onFail", error);
|
||||
|
||||
dispatch({
|
||||
type: types.CONVERT_PDF_TO_IMAGE_FAILURE,
|
||||
payload: error,
|
||||
payload: { pdfUrl, error },
|
||||
});
|
||||
|
||||
dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
|
||||
|
||||
// 콜백이 있으면 에러와 함께 호출
|
||||
callback && callback(error, null);
|
||||
};
|
||||
|
||||
// API 요청 파라미터
|
||||
const params = {
|
||||
pdfUrl: pdfUrl,
|
||||
};
|
||||
|
||||
TAxios(
|
||||
dispatch,
|
||||
getState,
|
||||
"post",
|
||||
'post',
|
||||
URLS.CONVERT_IMG,
|
||||
{},
|
||||
params,
|
||||
{ pdfUrl },
|
||||
onSuccess,
|
||||
onFail
|
||||
onFail,
|
||||
false,
|
||||
'blob'
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 변환된 이미지 데이터를 초기화하는 액션
|
||||
* 여러 PDF를 순차적으로 변환 (백그라운드)
|
||||
* @param {Array<string>} pdfUrls - 변환할 PDF URL 배열
|
||||
* @param {function} callback - 완료 후 실행할 콜백 (errors, results)
|
||||
*/
|
||||
export const convertMultiplePdfs = (pdfUrls, callback) => async (dispatch, getState) => {
|
||||
if (!pdfUrls || pdfUrls.length === 0) {
|
||||
callback && callback(null, []);
|
||||
return;
|
||||
}
|
||||
|
||||
const results = [];
|
||||
const errors = [];
|
||||
|
||||
for (let i = 0; i < pdfUrls.length; i++) {
|
||||
const pdfUrl = pdfUrls[i];
|
||||
|
||||
await new Promise((resolve) => {
|
||||
dispatch(
|
||||
convertPdfToImage(pdfUrl, (error, imageUrl) => {
|
||||
if (error) {
|
||||
errors.push({ pdfUrl, error });
|
||||
} else {
|
||||
results.push({ pdfUrl, imageUrl });
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
if (i < pdfUrls.length - 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
}
|
||||
|
||||
callback && callback(errors.length > 0 ? errors : null, results);
|
||||
};
|
||||
|
||||
/**
|
||||
* 변환된 이미지 데이터 초기화 (전체)
|
||||
*/
|
||||
export const clearConvertedImage = () => ({
|
||||
type: types.CLEAR_CONVERTED_IMAGE,
|
||||
});
|
||||
|
||||
/**
|
||||
* 특정 PDF의 변환된 이미지 데이터 제거
|
||||
* @param {string} pdfUrl - 제거할 PDF URL
|
||||
*/
|
||||
export const clearConvertedImageByUrl = (pdfUrl) => ({
|
||||
type: types.CLEAR_CONVERTED_IMAGE_BY_URL,
|
||||
payload: pdfUrl,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user