up
This commit is contained in:
@@ -25,12 +25,7 @@ const props = defineProps({
|
||||
type: Object,
|
||||
required: true,
|
||||
},
|
||||
/** 回填费用列表(分为单位) */
|
||||
initialCosts: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
/** 回填图片列表 */
|
||||
/** 回填图片列表 [{ Sha256, Name, ... }] */
|
||||
initialPhotos: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
@@ -94,12 +89,12 @@ function removeCostEntry(index) {
|
||||
|
||||
/** 将当前 costEntries(元)转换为分并同步到父组件 */
|
||||
function syncCosts() {
|
||||
const converted = costEntries.map((h) => ({
|
||||
// 直接更新父组件的 form._costs,跳过 emit 链路
|
||||
props.modelValue._costs = costEntries.map((h) => ({
|
||||
...h,
|
||||
cost: Math.round(h.cost * 100),
|
||||
costt: Math.round(h.costt * 100),
|
||||
}));
|
||||
emit("update:modelValue", { ...props.modelValue, _costs: converted });
|
||||
}
|
||||
|
||||
watch(
|
||||
@@ -111,25 +106,24 @@ watch(
|
||||
},
|
||||
);
|
||||
|
||||
// 回填费用(分→元)
|
||||
watch(
|
||||
() => props.initialCosts,
|
||||
(list) => {
|
||||
if (!list || list.length === 0) return;
|
||||
costEntries.splice(0, costEntries.length);
|
||||
list.forEach((c) => {
|
||||
costEntries.push({
|
||||
type: c.costType,
|
||||
int: c.quantity,
|
||||
cost: parseFloat((c.price / 100).toFixed(2)),
|
||||
costt: parseFloat(((c.price * c.quantity) / 100).toFixed(2)),
|
||||
currencytype: c.currencyType,
|
||||
});
|
||||
// ==================== 外部初始化接口 ====================
|
||||
/**
|
||||
* 由父组件调用,用于回填已有费用数据(来自 API)
|
||||
* @param {Array} list 费用数组,单位:分
|
||||
*/
|
||||
function initCostEntries(list) {
|
||||
if (!list || list.length === 0) return;
|
||||
costEntries.splice(0, costEntries.length);
|
||||
list.forEach((c) => {
|
||||
costEntries.push({
|
||||
type: c.type ?? c.CostType ?? 1,
|
||||
int: c.int ?? c.Quantity ?? 1,
|
||||
cost: parseFloat(((c.cost ?? c.Price) / 100).toFixed(2)),
|
||||
costt: parseFloat(((c.costt ?? c.Price * (c.int ?? c.Quantity)) / 100).toFixed(2)),
|
||||
currencytype: c.currencytype ?? c.CurrencyType ?? 1,
|
||||
});
|
||||
syncCosts();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 图片上传 ====================
|
||||
const photosRef = ref(null);
|
||||
@@ -141,7 +135,7 @@ function getPhotoHashes() {
|
||||
return photosRef.value?.return_files().map((f) => f.hash) ?? [];
|
||||
}
|
||||
|
||||
defineExpose({ getPhotoHashes, costEntries });
|
||||
defineExpose({ getPhotoHashes, costEntries, initCostEntries });
|
||||
|
||||
// ==================== 表单字段双向绑定 ====================
|
||||
function update(field, value) {
|
||||
|
||||
@@ -53,6 +53,11 @@ const prop = defineProps({
|
||||
type: String,
|
||||
default: "/api/files/upload",
|
||||
},
|
||||
/** 初始已有文件 [{ hash, name, ... }] */
|
||||
initialFiles: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
});
|
||||
|
||||
// 初始化 Dropzone
|
||||
@@ -201,11 +206,43 @@ function return_files() {
|
||||
return files;
|
||||
}
|
||||
|
||||
// 加载初始已有文件(编辑场景)
|
||||
function loadInitialFiles() {
|
||||
if (!dropzoneInstance || !prop.initialFiles?.length) return;
|
||||
prop.initialFiles.forEach((f) => {
|
||||
// 构造 Dropzone 期望的 mock file 对象
|
||||
const mockFile = {
|
||||
name: f.Name || f.name || f.hash,
|
||||
size: f.Size || f.size || 0,
|
||||
type: f.Mime || f.mime || "image/jpeg",
|
||||
status: Dropzone.SUCCESS,
|
||||
accepted: true,
|
||||
upload: { uuid: f.Hash || f.hash || f.Sha256 },
|
||||
previewElement: null,
|
||||
_removeLink: null,
|
||||
};
|
||||
// 通知 Dropzone "这是一个已存在的文件,不要上传"
|
||||
dropzoneInstance.emit("addedfile", mockFile);
|
||||
dropzoneInstance.emit("complete", mockFile);
|
||||
dropzoneInstance.files.push(mockFile);
|
||||
// 填充上传结果字段
|
||||
const url = `/api/files/get/${f.Hash || f.hash || f.Sha256}`;
|
||||
files.push({
|
||||
uuid: f.Hash || f.hash || f.Sha256,
|
||||
hash: f.Hash || f.hash || f.Sha256,
|
||||
get_url: url,
|
||||
download_url: `/api/files/download/${f.Hash || f.hash || f.Sha256}`,
|
||||
file_name: f.Name || f.name || "",
|
||||
file_size: f.Size || f.size || 0,
|
||||
is_upload: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 组件挂载时初始化
|
||||
onMounted(() => {
|
||||
initDropzone();
|
||||
|
||||
//console.log(lightbox)
|
||||
loadInitialFiles();
|
||||
});
|
||||
|
||||
// 组件卸载时销毁
|
||||
|
||||
Reference in New Issue
Block a user