feat: recycle system
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
<template>
|
||||
<t-form class="form" label-width="6.5rem">
|
||||
<t-form-item label="回收物名称" required-mark>
|
||||
<t-input v-model.trim="form.name" align="right" borderless maxlength="128" placeholder="例如空瓶" />
|
||||
</t-form-item>
|
||||
<t-form-item label="计量单位" required-mark>
|
||||
<t-input
|
||||
v-model="form.unitName"
|
||||
align="right"
|
||||
borderless
|
||||
maxlength="32"
|
||||
placeholder="例如个、公斤"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label="允许小数" content-align="right">
|
||||
<t-switch v-model="form.allowDecimalQuantity" size="small" />
|
||||
</t-form-item>
|
||||
<t-form-item label="启用" content-align="right">
|
||||
<t-switch v-model="form.status" size="small" :custom-value="['ACTIVE', 'INACTIVE']" />
|
||||
</t-form-item>
|
||||
<t-form-item label="备注">
|
||||
<t-textarea
|
||||
v-model="form.remark"
|
||||
borderless
|
||||
autosize
|
||||
maxlength="500"
|
||||
placeholder="选填"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<attachment-selector
|
||||
v-model:items="attachments"
|
||||
title="图片及附件"
|
||||
:busy="uploading"
|
||||
:viewer-z-index="props.viewerZIndex"
|
||||
@select="upload($event.files)"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<span class="gray">回收物保存后不能删除,不再使用请停用。停用不影响已创建的兑换单。</span>
|
||||
</t-form-item>
|
||||
</t-form>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { AttachmentAPI } from "timi-web-api";
|
||||
import RecycleAPI from "@/api/RecycleAPI";
|
||||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||||
import type { PhotoItem } from "@/types/Photo";
|
||||
import type { RecycleStatus } from "@/types/Recycle";
|
||||
import { getAttachmentReadURL } from "@/utils/attachment";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { compressUploadImageList } from "@/utils/imageCompression";
|
||||
import { Message } from "@/utils/message";
|
||||
|
||||
defineOptions({ name: "RecycleItemForm" });
|
||||
const props = withDefaults(defineProps<{ itemId?: string; viewerZIndex?: number }>(), { itemId: "" });
|
||||
const emit = defineEmits<{ saved: [] }>();
|
||||
const form = reactive({
|
||||
name: "",
|
||||
unitName: "",
|
||||
allowDecimalQuantity: false,
|
||||
status: "ACTIVE" as RecycleStatus,
|
||||
remark: ""
|
||||
});
|
||||
const attachments = ref<PhotoItem[]>([]);
|
||||
const loading = ref(false);
|
||||
const ready = ref(false);
|
||||
const saving = ref(false);
|
||||
const uploading = ref(false);
|
||||
let version = 0;
|
||||
|
||||
watch(() => props.itemId, async value => {
|
||||
const current = ++version;
|
||||
Object.assign(form, { name: "", unitName: "", allowDecimalQuantity: false, status: "ACTIVE", remark: "" });
|
||||
attachments.value = [];
|
||||
ready.value = !value;
|
||||
if (!value) {
|
||||
loading.value = false;
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
try {
|
||||
const item = await RecycleAPI.itemDetail(value);
|
||||
if (current !== version) return;
|
||||
Object.assign(form, {
|
||||
name: item.name || "",
|
||||
unitName: item.unitName || "",
|
||||
allowDecimalQuantity: item.allowDecimalQuantity ?? false,
|
||||
status: item.status || "ACTIVE",
|
||||
remark: item.remark || ""
|
||||
});
|
||||
attachments.value = (item.attachmentList || []).filter(entry => entry.id).map(entry => ({
|
||||
attachment: entry,
|
||||
attachType: entry.attachType,
|
||||
mimeType: entry.mimeType,
|
||||
url: getAttachmentReadURL(entry.id || "")
|
||||
}));
|
||||
ready.value = true;
|
||||
} catch (error) {
|
||||
if (current === version) Message.error(getErrorMessage(error, "回收物加载失败"));
|
||||
} finally {
|
||||
if (current === version) loading.value = false;
|
||||
}
|
||||
}, { immediate: true });
|
||||
|
||||
async function upload(files: File[]): Promise<void> {
|
||||
if (!files.length || uploading.value) return;
|
||||
uploading.value = true;
|
||||
try {
|
||||
const result = await AttachmentAPI.upload(await compressUploadImageList(files));
|
||||
for (const [index, file] of result.entries()) {
|
||||
if (!file.id) continue;
|
||||
const source = files[index];
|
||||
attachments.value.push({
|
||||
attachment: { name: source?.name, mimeType: source?.type },
|
||||
attachType: source?.type.startsWith("image/") ? "PHOTO" : "OTHER",
|
||||
mimeType: source?.type,
|
||||
tempFileId: file.id,
|
||||
url: AttachmentAPI.getTempReadURL(file.id)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "附件上传失败"));
|
||||
} finally {
|
||||
uploading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function save(): Promise<void> {
|
||||
if (saving.value || uploading.value || !ready.value) return;
|
||||
if (!form.name.trim() || !form.unitName.trim()) {
|
||||
Message.warning("请填写回收物名称和计量单位");
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
await RecycleAPI.itemSave({
|
||||
id: props.itemId || undefined,
|
||||
name: form.name.trim(),
|
||||
unitName: form.unitName.trim(),
|
||||
allowDecimalQuantity: form.allowDecimalQuantity,
|
||||
status: form.status,
|
||||
remark: form.remark || null,
|
||||
attachmentList: attachments.value.flatMap(entry => entry.tempFileId
|
||||
? [{ ...entry.attachment, attachType: entry.attachType, tempFileId: entry.tempFileId }]
|
||||
: entry.attachment ? [entry.attachment] : [])
|
||||
});
|
||||
Message.success("回收物配置已保存");
|
||||
emit("saved");
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "回收物配置保存失败"));
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
defineExpose({ save, saving, loading, uploading, ready });
|
||||
</script>
|
||||
Reference in New Issue
Block a user