upper attachment-selector to lib
This commit is contained in:
@@ -68,18 +68,11 @@
|
||||
<t-textarea v-model="form.remark" borderless maxlength="255" autosize placeholder="可填写产品说明" />
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<media-selector
|
||||
v-model="attachmentUrlList"
|
||||
v-model:photo-items="attachmentItemList"
|
||||
<attachment-selector
|
||||
v-model:items="attachmentItemList"
|
||||
title="产品公共图片及附件"
|
||||
:edit-mode="true"
|
||||
:loading="isAttachmentUploading"
|
||||
:show-viewer-actions="false"
|
||||
:add-actions="attachmentAddActions"
|
||||
@add="openAttachmentPicker('OTHER')"
|
||||
@action="handleAttachmentAction"
|
||||
@capture-photo="capturePhoto"
|
||||
@capture-video="captureVideo"
|
||||
:busy="isAttachmentUploading"
|
||||
@select="handleAttachmentSelected($event.files, $event.source)"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label="纯色封面" label-width="8rem" content-align="right">
|
||||
@@ -245,9 +238,6 @@
|
||||
>
|
||||
<t-color-picker v-if="coverColorPickerVisible" v-model="coverColorDraft" format="HEX" type="multiple" :swatch-colors="colorSwatchList" @palette-bar-change="updateCoverColorDraftFromPalette" />
|
||||
</bottom-popup>
|
||||
<input ref="attachmentInputRef" class="file-input" type="file" accept="*/*" @change="handleAttachmentSelected" />
|
||||
<input ref="photoInputRef" class="file-input" type="file" accept="image/*" capture="environment" @change="handleAttachmentSelected" />
|
||||
<input ref="videoInputRef" class="file-input" type="file" accept="video/*" capture="environment" @change="handleAttachmentSelected" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -256,21 +246,20 @@ import { BottomPopup } from "timi-tdesign-mobile";
|
||||
import { DialogPlugin, type ColorObject } from "tdesign-mobile-vue";
|
||||
import { type Attachment, AttachmentAPI, useUserStore } from "timi-web-api";
|
||||
import ProductAPI from "@/api/ProductAPI";
|
||||
import MediaSelector from "@/components/mediaSelector/MediaSelector.vue";
|
||||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||||
import ProductSkuEditor from "@/pages/product/component/ProductSkuEditor.vue";
|
||||
import ProductSkuUnitEditor from "@/pages/product/component/ProductSkuUnitEditor.vue";
|
||||
import ProductSkuUnitBatchEditor from "@/pages/product/component/ProductSkuUnitBatchEditor.vue";
|
||||
import type { MediaSelectorAddAction } from "@/components/mediaSelector/types";
|
||||
import type { AttachmentSelectorSelection } from "@/components/attachmentSelector/types";
|
||||
import type { Product, ProductAttachmentType, ProductCategory, ProductSku, ProductSkuUnit, ProductSpec, ProductType } from "@/types/Product";
|
||||
import type { ProductAttachmentItem, ProductForm, ProductSkuForm as SkuForm, ProductSkuUnitForm as UnitForm, ProductSpecForm as SpecForm, ProductSpecValueForm as SpecValueForm } from "@/types/ProductEditor";
|
||||
import { compressUploadImage } from "@/utils/imageCompression";
|
||||
import { compressUploadImageList } from "@/utils/imageCompression";
|
||||
import { getAttachmentReadURL } from "@/utils/attachment";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { emitListDataChanged, listDataEventKeys } from "@/utils/listDataEvents";
|
||||
import { formatProductAmount, getProductStatusText, getProductTypeText, normalizeProductQuantity, parseProductAmount } from "@/utils/product";
|
||||
import { Message } from "@/utils/message";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { saveCapturedFilesFromInput } from "@/utils/twaBridge";
|
||||
|
||||
const pageNavBarRight = reactive({
|
||||
icon: "",
|
||||
@@ -345,10 +334,6 @@ const categoryList = ref<ProductCategory[]>([]);
|
||||
const typePickerVisible = ref(false);
|
||||
const categoryPickerVisible = ref(false);
|
||||
const statusPickerVisible = ref(false);
|
||||
const attachmentInputRef = ref<HTMLInputElement>();
|
||||
const photoInputRef = ref<HTMLInputElement>();
|
||||
const videoInputRef = ref<HTMLInputElement>();
|
||||
const attachmentPickerTarget = ref<ProductAttachmentType>("PHOTO");
|
||||
const isAttachmentUploading = ref(false);
|
||||
const coverColorPickerVisible = ref(false);
|
||||
const coverColorDraft = ref(DEFAULT_COVER_COLOR);
|
||||
@@ -364,17 +349,7 @@ const categoryOptions = computed(() => [{ label: "不设置分类", value: "" },
|
||||
const statusOptions = [{ label: "启用", value: "ACTIVE" }, { label: "停用", value: "INACTIVE" }];
|
||||
const colorSwatchList = ["#2F88FF", "#0EA5E9", "#10B981", "#22C55E", "#F59E0B", "#F97316", "#EF4444", "#EC4899", "#8B5CF6", "#64748B"];
|
||||
const selectedCategoryName = computed(() => categoryList.value.find(item => item.id === form.categoryId)?.name || "不设置分类");
|
||||
const attachmentAddActions = [
|
||||
{ icon: "file-attachment", text: "其他文件", emit: "add-other", theme: "default" }
|
||||
] as const;
|
||||
const attachmentItemList = ref<ProductAttachmentItem[]>([]);
|
||||
const attachmentUrlList = computed<string[]>({
|
||||
get: () => attachmentItemList.value.map(item => item.url),
|
||||
set: value => {
|
||||
const urlSet = new Set(value);
|
||||
attachmentItemList.value = attachmentItemList.value.filter(item => urlSet.has(item.url));
|
||||
}
|
||||
});
|
||||
const solidColorCoverColor = computed(() => attachmentItemList.value.find(item => item.isSolidColorCover)?.solidColor || "");
|
||||
const skuCombinationCount = computed(() => {
|
||||
if (!form.specList.length) {
|
||||
@@ -655,27 +630,6 @@ function confirmStatus(value: Array<string | number>): void {
|
||||
statusPickerVisible.value = false;
|
||||
}
|
||||
|
||||
function openAttachmentPicker(target: ProductAttachmentType): void {
|
||||
attachmentPickerTarget.value = target;
|
||||
attachmentInputRef.value?.click();
|
||||
}
|
||||
|
||||
function capturePhoto(): void {
|
||||
attachmentPickerTarget.value = "PHOTO";
|
||||
photoInputRef.value?.click();
|
||||
}
|
||||
|
||||
function captureVideo(): void {
|
||||
attachmentPickerTarget.value = "OTHER";
|
||||
videoInputRef.value?.click();
|
||||
}
|
||||
|
||||
function handleAttachmentAction(action: MediaSelectorAddAction): void {
|
||||
if (action.emit === "add-other") {
|
||||
openAttachmentPicker("OTHER");
|
||||
}
|
||||
}
|
||||
|
||||
function openCoverColorPicker(): void {
|
||||
coverColorDraft.value = solidColorCoverColor.value || DEFAULT_COVER_COLOR;
|
||||
coverColorPickerVisible.value = true;
|
||||
@@ -742,24 +696,30 @@ async function confirmCoverColorPicker(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleAttachmentSelected(event: Event): Promise<void> {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const file = input.files?.[0];
|
||||
if (!file) {
|
||||
async function handleAttachmentSelected(files: File[], source: AttachmentSelectorSelection["source"]): Promise<void> {
|
||||
if (!files.length) {
|
||||
return;
|
||||
}
|
||||
saveCapturedFilesFromInput(input, [file]);
|
||||
const attachType: ProductAttachmentType = source === "photo" ? "PHOTO" : "OTHER";
|
||||
isAttachmentUploading.value = true;
|
||||
try {
|
||||
const tempFile = (await AttachmentAPI.upload(await compressUploadImage(file)))[0];
|
||||
if (tempFile?.id) {
|
||||
attachmentItemList.value.push({ attachType: attachmentPickerTarget.value, mimeType: file.type, tempFileId: tempFile.id, url: AttachmentAPI.getTempReadURL(tempFile.id) });
|
||||
const compressedFiles = await compressUploadImageList(files);
|
||||
const tempFileList = await AttachmentAPI.upload(compressedFiles);
|
||||
for (const [index, tempFile] of tempFileList.entries()) {
|
||||
if (!tempFile.id) {
|
||||
continue;
|
||||
}
|
||||
attachmentItemList.value.push({
|
||||
attachType,
|
||||
mimeType: files[index]?.type,
|
||||
tempFileId: tempFile.id,
|
||||
url: AttachmentAPI.getTempReadURL(tempFile.id)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "产品附件上传失败"));
|
||||
} finally {
|
||||
isAttachmentUploading.value = false;
|
||||
input.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user