fix(cafe24): 대표이미지 적용 안 됨 + 옵션 썸네일 "없음" 오표시
- 대표이미지: NNEditor 업로드 경로를 PUT detail_image 에 넣던 방식은 카페24가
반영하지 않았다. 문서의 전용 API POST /admin/products/{no}/images 에
base64 data URI + image_upload_type=A 로 등록하고, 응답의 새 경로 4개로
화면·스냅샷을 맞춘다(응답에 updated_date 가 없어 스냅샷 시각은 등록 시각).
- 옵션 썸네일: 카페24는 option_link_image 등을 "/web/product/big/…" 상대경로로
준다. 그대로 src 에 넣어 우리 도메인에서 404 → "없음"으로 보였다. 옵션 GET 에
image_base(쇼핑몰 주소)를 내려 붙여 그리고, 후보(버튼→연결→품목 이미지)가
깨지면 다음 후보로 넘어간다. 카페24로 되보내는 hidden 값은 원본 그대로.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,6 +28,18 @@
|
||||
|
||||
var DEFAULT_TYPE = "S";
|
||||
|
||||
// 썸네일 <img> onerror — data-fallback(줄바꿈 구분)의 다음 후보로, 없으면 "없음".
|
||||
window.cf24ThumbFallback = function (img) {
|
||||
var rest = (img.getAttribute("data-fallback") || "").split("\n").filter(Boolean);
|
||||
if (rest.length) {
|
||||
img.setAttribute("data-fallback", rest.slice(1).join("\n"));
|
||||
img.src = rest[0];
|
||||
return;
|
||||
}
|
||||
img.onerror = null;
|
||||
img.outerHTML = '<div class="cf24-opt-thumb-empty">없음</div>';
|
||||
};
|
||||
|
||||
window.cf24SetupOptions = function (ctx) {
|
||||
var root = ctx.root, no = ctx.productNo, modal = ctx.modal;
|
||||
var apiJson = ctx.apiJson, esc = ctx.escHtml, setMsg = ctx.setMsg;
|
||||
@@ -38,7 +50,7 @@
|
||||
var count = root.querySelector("#cf24-options-count");
|
||||
if (!openBtn || !body) return;
|
||||
|
||||
var state = { option: null, variants: [], displayTypes: {}, orphans: [] };
|
||||
var state = { option: null, variants: [], displayTypes: {}, orphans: [], imageBase: "" };
|
||||
|
||||
openBtn.addEventListener("click", function () {
|
||||
var nameEl = document.getElementById("cf24-name-text");
|
||||
@@ -53,6 +65,7 @@
|
||||
state.option = data.option;
|
||||
state.variants = data.variants || [];
|
||||
state.displayTypes = data.display_types || {};
|
||||
state.imageBase = data.image_base || "";
|
||||
render();
|
||||
})
|
||||
.catch(function (err) {
|
||||
@@ -139,13 +152,29 @@
|
||||
return "";
|
||||
}
|
||||
|
||||
// 카페24가 경로는 주지만 파일이 없는 경우가 있다(깨진 그림) — 실패하면 "없음"으로 바꾼다.
|
||||
// 카페24 이미지 경로는 "/web/product/big/…" 처럼 **상대경로**로 오기도 한다(옵션 연결
|
||||
// 이미지 등). 그대로 src 에 넣으면 우리 도메인에서 찾다가 깨진다 — 쇼핑몰 주소를 붙인다.
|
||||
// hidden 값(카페24에 다시 보내는 값)은 받은 그대로 둔다.
|
||||
function imageUrl(path) {
|
||||
path = String(path || "").trim();
|
||||
if (!path) return "";
|
||||
if (path.indexOf("//") === 0) return "https:" + path;
|
||||
if (path.charAt(0) === "/") return state.imageBase ? state.imageBase + path : "";
|
||||
return path;
|
||||
}
|
||||
|
||||
// 후보(버튼 이미지 → 연결 이미지 → 품목 image) 중 첫 번째를 보여주고, 깨지면 다음
|
||||
// 후보로 넘어간다(cf24ThumbFallback). 전부 실패하면 "없음".
|
||||
var THUMB_EMPTY = '<div class="cf24-opt-thumb-empty">없음</div>';
|
||||
function thumbHtml(src, title) {
|
||||
return src
|
||||
? '<img class="cf24-opt-thumb" src="' + esc(src) + '" alt="" title="' + esc(title || "") +
|
||||
'" onerror="this.outerHTML=\'' + THUMB_EMPTY.replace(/"/g, """) + '\'" />'
|
||||
: THUMB_EMPTY;
|
||||
function thumbHtml(candidates, title) {
|
||||
var urls = [];
|
||||
(Array.isArray(candidates) ? candidates : [candidates]).forEach(function (c) {
|
||||
var u = imageUrl(c);
|
||||
if (u && urls.indexOf(u) < 0) urls.push(u);
|
||||
});
|
||||
if (!urls.length) return THUMB_EMPTY;
|
||||
return '<img class="cf24-opt-thumb" src="' + esc(urls[0]) + '" alt="" title="' + esc(title || "") +
|
||||
'" data-fallback="' + esc(urls.slice(1).join("\n")) + '" onerror="cf24ThumbFallback(this)" />';
|
||||
}
|
||||
|
||||
// 썸네일 파일 선택 → 업로드 → hidden 두 개(option_image_file, option_link_image)에 경로
|
||||
@@ -220,8 +249,8 @@
|
||||
var v = r.value || { option_text: "", option_image_file: "", option_link_image: "", option_color: "" };
|
||||
var variant = r.variant;
|
||||
var isNewRow = r.index == null;
|
||||
var thumbSrc = v.option_image_file || v.option_link_image ||
|
||||
(state.option && state.option.has_option ? variantImageFor(state.option.options[0].option_name, v.option_text) : "");
|
||||
var thumbSrc = [v.option_image_file, v.option_link_image,
|
||||
state.option && state.option.has_option ? variantImageFor(state.option.options[0].option_name, v.option_text) : ""];
|
||||
var code = variant ? variant.variant_code : "";
|
||||
// data-index: options 응답 안 위치(옵션 PUT 짝맞춤용, 새 행은 빈 값) / data-pos: 처음 그린 순서(순서 변경 판정용)
|
||||
return '<tr class="cf24-u-row' + (isNewRow ? " is-new" : "") + '" data-index="' + (isNewRow ? "" : r.index) + '" data-pos="' + (position == null ? "" : position) + '" data-code="' + esc(code) + '">' +
|
||||
@@ -801,7 +830,7 @@
|
||||
'<label class="cf24-side-field"><span>옵션명</span><input type="text" name="option_name" value="' + esc(o.option_name) + '" /></label>' +
|
||||
'<label class="cf24-side-field cf24-side-field-type"><span>표시방식</span>' + typeSelect(o.option_display_type || DEFAULT_TYPE, "option_display_type") + "</label></div>";
|
||||
o.option_value.forEach(function (v, vi) {
|
||||
var src = v.option_image_file || v.option_link_image || variantImageFor(o.option_name, v.option_text);
|
||||
var src = [v.option_image_file, v.option_link_image, variantImageFor(o.option_name, v.option_text)];
|
||||
html += '<div class="cf24-opt-value" data-value="' + vi + '"><span class="cf24-opt-thumb-slot">' + thumbHtml(src) + "</span>" +
|
||||
'<input type="text" name="option_text" value="' + esc(v.option_text) + '" placeholder="옵션값 이름" />' +
|
||||
'<input type="hidden" name="option_image_file" value="' + esc(v.option_image_file) + '" />' +
|
||||
@@ -822,7 +851,7 @@
|
||||
variants.forEach(function (v) {
|
||||
var label = v.options.map(function (o) { return o.value; }).join(" / ") || v.variant_code;
|
||||
html += '<tr class="cf24-v-item" data-code="' + esc(v.variant_code) + '">' +
|
||||
'<td class="cf24-v-imgcol">' + (v.image ? '<img class="cf24-v-img" src="' + esc(v.image) + '" alt="" />' : '<span class="cf24-v-img cf24-v-img-empty"></span>') + "</td>" +
|
||||
'<td class="cf24-v-imgcol">' + (imageUrl(v.image) ? '<img class="cf24-v-img" src="' + esc(imageUrl(v.image)) + '" alt="" />' : '<span class="cf24-v-img cf24-v-img-empty"></span>') + "</td>" +
|
||||
'<td class="cf24-v-name" title="' + esc(label) + '">' + esc(label) + "</td>" +
|
||||
'<td class="cf24-v-syscode"><button type="button" class="cf24-v-copy" data-copy-text="' + esc(v.variant_code) + '">' + esc(v.variant_code) + "</button></td>" +
|
||||
'<td class="cf24-v-code"><input type="text" name="custom_variant_code" maxlength="40" value="' + esc(v.custom_variant_code) + '" /></td>' +
|
||||
|
||||
Reference in New Issue
Block a user