570 lines
13 KiB
Vue
570 lines
13 KiB
Vue
<template>
|
|
<div v-bind="$attrs" class="photo-scroller">
|
|
<div class="t-form__label" :class="{ required }">
|
|
<slot name="title">
|
|
<span v-text="title" />
|
|
</slot>
|
|
</div>
|
|
<div class="photo-shell" :class="{ 'without-add': !showAdd }">
|
|
<div class="items">
|
|
<div v-if="!displayPhotoList.length" class="empty" v-text="emptyText" />
|
|
<div
|
|
v-for="(item, index) in displayPhotoList"
|
|
:key="`${item.url}-${item.attachType}-${index}`"
|
|
class="item"
|
|
>
|
|
<t-button variant="text" class="preview-trigger" @click="openPhotoViewer(index)">
|
|
<t-image
|
|
class="image"
|
|
:src="item.url"
|
|
:alt="getAttachTypeText(item.attachType)"
|
|
fit="cover"
|
|
/>
|
|
</t-button>
|
|
<div
|
|
v-if="showTypeLabel"
|
|
class="type-label white glass-black clip-text"
|
|
v-text="getAttachTypeText(item.attachType)"
|
|
/>
|
|
<t-button
|
|
v-if="showRemove && hasRemovablePhotos"
|
|
theme="danger"
|
|
class="remove"
|
|
shape="circle"
|
|
size="extra-small"
|
|
@click="removePhoto(index)"
|
|
>
|
|
<t-icon name="close" size="1rem" />
|
|
</t-button>
|
|
</div>
|
|
</div>
|
|
<div v-if="showAdd" class="add-trigger">
|
|
<t-button
|
|
v-if="addActionList.length === 1"
|
|
theme="primary"
|
|
variant="dashed"
|
|
class="add"
|
|
:loading="isBusy"
|
|
:disabled="isBusy"
|
|
@click="handleAddAction(addActionList[0])"
|
|
>
|
|
<span v-text="addActionList[0].text" />
|
|
</t-button>
|
|
<t-popover
|
|
v-else
|
|
v-model:visible="addPopoverVisible"
|
|
placement="left"
|
|
theme="light"
|
|
>
|
|
<t-button
|
|
class="add"
|
|
theme="primary"
|
|
variant="dashed"
|
|
:loading="isBusy"
|
|
:disabled="isBusy"
|
|
>
|
|
<span v-text="addText" />
|
|
</t-button>
|
|
<template #content>
|
|
<div class="add-actions">
|
|
<t-button
|
|
v-for="(action, index) in addActionList"
|
|
:key="`${action.emit}-${index}`"
|
|
variant="text"
|
|
:theme="action.theme"
|
|
class="add-action"
|
|
:disabled="isBusy"
|
|
@click="handleAddAction(action)"
|
|
>
|
|
<template #icon v-if="action.icon">
|
|
<t-icon :name="action.icon" />
|
|
</template>
|
|
<span v-text="action.text" />
|
|
</t-button>
|
|
</div>
|
|
</template>
|
|
</t-popover>
|
|
</div>
|
|
</div>
|
|
<input
|
|
v-if="props.upload"
|
|
ref="fileInput"
|
|
class="file-input"
|
|
type="file"
|
|
accept="image/*"
|
|
multiple
|
|
@change="handleFileChange"
|
|
/>
|
|
</div>
|
|
<t-image-viewer
|
|
v-model:visible="photoViewerVisible"
|
|
:images="photoViewerImages"
|
|
show-index
|
|
@close="photoActionVisible = false"
|
|
@index-change="handlePhotoViewerIndexChange"
|
|
>
|
|
<template #delete-btn>
|
|
<div v-if="showViewerActions" class="viewer-action" @click.stop>
|
|
<t-popover
|
|
v-model:visible="photoActionVisible"
|
|
placement="bottom"
|
|
theme="light"
|
|
:show-arrow="false"
|
|
>
|
|
<t-button class="white" variant="text">
|
|
<t-icon name="ellipsis" />
|
|
</t-button>
|
|
<template #content>
|
|
<div class="actions" @click.stop>
|
|
<div class="title">修改类型</div>
|
|
<div class="type">
|
|
<t-button
|
|
class="button"
|
|
v-for="item in attachTypeOptions"
|
|
:key="item.value"
|
|
:theme="item.value === currentPhotoItem?.attachType ? 'primary' : 'default'"
|
|
variant="text"
|
|
size="extra-small"
|
|
:disabled="isSubmitting"
|
|
@click="changePhotoType(item.value)"
|
|
>
|
|
<span v-text="item.text" />
|
|
</t-button>
|
|
</div>
|
|
<t-divider />
|
|
<t-button
|
|
class="button delete-action"
|
|
theme="danger"
|
|
size="extra-small"
|
|
variant="text"
|
|
:disabled="isSubmitting"
|
|
@click="removeCurrentPhoto"
|
|
>
|
|
<template #icon>
|
|
<t-icon name="delete" />
|
|
</template>
|
|
删除
|
|
</t-button>
|
|
</div>
|
|
</template>
|
|
</t-popover>
|
|
</div>
|
|
</template>
|
|
</t-image-viewer>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import type { ButtonProps } from "tdesign-mobile-vue";
|
|
import type { PhotoItem } from "@/types/Photo";
|
|
import { Message } from "@/utils/message";
|
|
|
|
defineOptions({
|
|
inheritAttrs: false
|
|
});
|
|
|
|
interface Props {
|
|
addActions?: readonly AddAction[];
|
|
addText?: string;
|
|
editMode?: boolean;
|
|
emptyText?: string;
|
|
fallbackImage?: string;
|
|
loading?: boolean;
|
|
modelValue: string[];
|
|
photoItems?: readonly PhotoItem[];
|
|
required?: boolean;
|
|
upload?: (fileList: File[]) => Promise<PhotoItem[]>;
|
|
showViewerActions?: boolean;
|
|
showAdd?: boolean;
|
|
showRemove?: boolean;
|
|
showTypeLabel?: boolean;
|
|
submit?: (photoItems: PhotoItem[]) => Promise<void>;
|
|
title?: string;
|
|
}
|
|
|
|
type AddAction = {
|
|
icon?: string;
|
|
emit: string;
|
|
text: string;
|
|
theme?: ButtonProps["theme"];
|
|
};
|
|
|
|
type ViewerPhotoItem = PhotoItem & {
|
|
sourceIndex: number;
|
|
};
|
|
|
|
const ATTACH_TYPE_TEXT_MAP: Record<string, string> = {
|
|
ADDITIONAL: "附加照片",
|
|
PHOTO: "照片",
|
|
POINT_CARD: "积分卡",
|
|
OTHER: "其他文件"
|
|
};
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
addActions: () => [],
|
|
title: "照片列表",
|
|
addText: "拍照",
|
|
editMode: false,
|
|
emptyText: "暂无照片",
|
|
loading: false,
|
|
required: false,
|
|
showAdd: true,
|
|
showRemove: true,
|
|
showTypeLabel: true,
|
|
showViewerActions: true
|
|
});
|
|
|
|
type Emits = {
|
|
(event: string): void;
|
|
(event: "update:modelValue", value: string[]): void;
|
|
(event: "update:photoItems", value: PhotoItem[]): void;
|
|
};
|
|
|
|
const emit = defineEmits<Emits>();
|
|
|
|
const photoViewerVisible = ref(false);
|
|
const photoViewerImages = ref<string[]>([]);
|
|
const photoViewerItems = ref<ViewerPhotoItem[]>([]);
|
|
const photoViewerIndex = ref(0);
|
|
const photoActionVisible = ref(false);
|
|
const isSubmitting = ref(false);
|
|
const isUploading = ref(false);
|
|
const addPopoverVisible = ref(false);
|
|
const fileInput = ref<HTMLInputElement>();
|
|
const addActionList = computed<readonly AddAction[]>(() => props.addActions.length ? props.addActions : [{
|
|
emit: "add",
|
|
text: props.addText,
|
|
theme: "default"
|
|
}]);
|
|
const isBusy = computed(() => props.loading || isSubmitting.value || isUploading.value);
|
|
const displayPhotoList = computed<PhotoItem[]>(() => {
|
|
if (props.photoItems?.length) {
|
|
return props.photoItems.map(normalizePhotoItem);
|
|
}
|
|
if (props.modelValue.length) {
|
|
return props.modelValue.map(url => ({ url, attachType: "PHOTO" }));
|
|
}
|
|
|
|
return props.fallbackImage ? [{ url: props.fallbackImage, attachType: "PHOTO" }] : [];
|
|
});
|
|
const hasRemovablePhotos = computed(() => !!props.photoItems?.length || !!props.modelValue.length);
|
|
const attachTypeOptions = [
|
|
{ text: "照片", value: "PHOTO" },
|
|
{ text: "积分卡", value: "POINT_CARD" },
|
|
{ text: "其他文件", value: "OTHER" }
|
|
] as const;
|
|
const currentPhotoItem = computed(() => photoViewerItems.value[photoViewerIndex.value]);
|
|
|
|
function getAttachTypeText(attachType?: string): string {
|
|
return ATTACH_TYPE_TEXT_MAP[attachType || "PHOTO"] || "其他文件";
|
|
}
|
|
|
|
function handleAddAction(action?: AddAction): void {
|
|
if (!action) {
|
|
return;
|
|
}
|
|
addPopoverVisible.value = false;
|
|
if (action.emit === "add" && props.upload) {
|
|
fileInput.value?.click();
|
|
return;
|
|
}
|
|
emit(action.emit);
|
|
}
|
|
|
|
async function handleFileChange(event: Event): Promise<void> {
|
|
const input = event.target as HTMLInputElement;
|
|
const fileList = Array.from(input.files || []);
|
|
if (!fileList.length || !props.upload) {
|
|
return;
|
|
}
|
|
|
|
isUploading.value = true;
|
|
try {
|
|
const uploadedPhotoItems = await props.upload(fileList);
|
|
if (!uploadedPhotoItems.length) {
|
|
return;
|
|
}
|
|
const nextPhotoItems = [...displayPhotoList.value, ...uploadedPhotoItems];
|
|
emit("update:photoItems", nextPhotoItems);
|
|
emit("update:modelValue", nextPhotoItems.map(item => item.url));
|
|
} catch {
|
|
Message.error("照片上传失败");
|
|
} finally {
|
|
isUploading.value = false;
|
|
input.value = "";
|
|
}
|
|
}
|
|
|
|
async function removePhoto(index: number): Promise<void> {
|
|
if (index < 0 || displayPhotoList.value.length <= index) {
|
|
return;
|
|
}
|
|
|
|
await applyPhotoItems(displayPhotoList.value.filter((_, itemIndex) => itemIndex !== index));
|
|
}
|
|
|
|
function openPhotoViewer(index: number): void {
|
|
const viewerItems = displayPhotoList.value.map((item, sourceIndex) => ({ item, sourceIndex }));
|
|
const rotatedItems = [...viewerItems.slice(index), ...viewerItems.slice(0, index)];
|
|
photoViewerItems.value = rotatedItems.map(({ item, sourceIndex }) => ({ ...item, sourceIndex }));
|
|
photoViewerImages.value = photoViewerItems.value.map(item => item.previewUrl || item.url);
|
|
photoViewerIndex.value = 0;
|
|
photoActionVisible.value = false;
|
|
photoViewerVisible.value = true;
|
|
}
|
|
|
|
function handlePhotoViewerIndexChange(index: number): void {
|
|
photoViewerIndex.value = index;
|
|
photoActionVisible.value = false;
|
|
}
|
|
|
|
async function changePhotoType(attachType: string): Promise<void> {
|
|
const currentItem = currentPhotoItem.value;
|
|
if (!currentItem || currentItem.attachType === attachType) {
|
|
photoActionVisible.value = false;
|
|
return;
|
|
}
|
|
|
|
const nextPhotoItems = displayPhotoList.value.map((item, index) => index === currentItem.sourceIndex ? {
|
|
...item,
|
|
attachType
|
|
} : item);
|
|
await applyPhotoItems(nextPhotoItems);
|
|
}
|
|
|
|
async function removeCurrentPhoto(): Promise<void> {
|
|
const currentItem = currentPhotoItem.value;
|
|
if (!currentItem) {
|
|
return;
|
|
}
|
|
|
|
await applyPhotoItems(displayPhotoList.value.filter((_, index) => index !== currentItem.sourceIndex));
|
|
}
|
|
|
|
async function applyPhotoItems(nextPhotoItems: PhotoItem[]): Promise<void> {
|
|
if (isSubmitting.value) {
|
|
return;
|
|
}
|
|
|
|
if (!props.editMode && !props.submit) {
|
|
Message.warning("展示模式未配置照片保存方法");
|
|
return;
|
|
}
|
|
|
|
if (!props.editMode) {
|
|
isSubmitting.value = true;
|
|
try {
|
|
await props.submit!(nextPhotoItems);
|
|
} catch {
|
|
Message.error("照片修改失败");
|
|
return;
|
|
} finally {
|
|
isSubmitting.value = false;
|
|
}
|
|
}
|
|
|
|
emit("update:photoItems", nextPhotoItems);
|
|
emit("update:modelValue", nextPhotoItems.map(item => item.url));
|
|
photoActionVisible.value = false;
|
|
photoViewerVisible.value = false;
|
|
}
|
|
|
|
function normalizePhotoItem(item: PhotoItem): PhotoItem {
|
|
return {
|
|
...item,
|
|
attachType: item.attachType || item.attachment?.attachType || "PHOTO"
|
|
};
|
|
}
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.photo-scroller {
|
|
gap: .375rem;
|
|
width: 100%;
|
|
display: grid;
|
|
|
|
.file-input {
|
|
display: none;
|
|
}
|
|
|
|
.photo-shell {
|
|
display: grid;
|
|
gap: .75rem;
|
|
grid-template-columns: minmax(0, 1fr) 5.5rem;
|
|
align-items: start;
|
|
|
|
&.without-add {
|
|
grid-template-columns: minmax(0, 1fr);
|
|
}
|
|
|
|
.items {
|
|
gap: .75rem;
|
|
width: 100%;
|
|
height: 100%;
|
|
min-width: 0;
|
|
display: flex;
|
|
overflow-x: auto;
|
|
overflow-y: hidden;
|
|
-webkit-overflow-scrolling: touch;
|
|
scrollbar-width: thin;
|
|
|
|
.empty {
|
|
width: 100%;
|
|
color: #52607a;
|
|
display: flex;
|
|
padding: 1rem 0;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f8fbff;
|
|
border-radius: 1rem;
|
|
}
|
|
|
|
.item {
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
flex: 0 0 5.5rem;
|
|
position: relative;
|
|
overflow: hidden;
|
|
border-radius: 1rem;
|
|
|
|
.preview-trigger {
|
|
padding: 0;
|
|
border: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
display: block;
|
|
cursor: pointer;
|
|
background: transparent;
|
|
|
|
.image {
|
|
height: 100%;
|
|
}
|
|
}
|
|
|
|
.type-label {
|
|
width: 100%;
|
|
bottom: 0;
|
|
padding: .125rem .5rem;
|
|
position: absolute;
|
|
overflow: hidden;
|
|
font-size: .875rem;
|
|
text-align: center;
|
|
line-height: 1.2;
|
|
border-radius: 0 0 1rem 1rem;
|
|
pointer-events: none;
|
|
}
|
|
|
|
.remove {
|
|
top: .375rem;
|
|
right: .375rem;
|
|
position: absolute;
|
|
|
|
:deep(.t-button__content) {
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.t-form__label {
|
|
&.required::before {
|
|
color: var(--td-error-color, #e34d59);
|
|
content: "*";
|
|
margin-right: .25rem;
|
|
}
|
|
}
|
|
|
|
.add-trigger {
|
|
--td-popover-padding: 0 .5rem;
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
|
|
:deep(.t-popover__content) {
|
|
border-radius: .875rem;
|
|
}
|
|
|
|
.add {
|
|
--td-button-border-radius: .875rem;
|
|
|
|
width: 5.5rem;
|
|
height: 5.5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.add-actions {
|
|
gap: .25rem;
|
|
display: grid;
|
|
padding: .5rem 0;
|
|
|
|
.add-action {
|
|
--td-button-medium-height: 32px;
|
|
--td-button-medium-padding-horizontal: 10px;
|
|
|
|
gap: .5rem;
|
|
width: 100%;
|
|
position: relative;
|
|
font-weight: unset;
|
|
justify-content: start;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.viewer-action {
|
|
color: #fff;
|
|
margin-left: -1rem;
|
|
|
|
.actions {
|
|
display: flex;
|
|
min-width: 8rem;
|
|
flex-direction: column;
|
|
|
|
.title {
|
|
padding: 0 .75rem;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.type {
|
|
display: flex;
|
|
justify-content: start;
|
|
flex-direction: column;
|
|
|
|
.button {
|
|
text-indent: 1.5em;
|
|
}
|
|
}
|
|
|
|
.button {
|
|
justify-content: start;
|
|
}
|
|
}
|
|
}
|
|
|
|
:global(html.theme-dark) {
|
|
.photo-scroller {
|
|
.field-title,
|
|
.photo-empty {
|
|
color: #94a3b8;
|
|
}
|
|
|
|
.photo-empty {
|
|
color: #e2e8f0;
|
|
background: rgba(15, 23, 42, .72);
|
|
border-color: rgba(148, 163, 184, .16);
|
|
}
|
|
|
|
.add {
|
|
background: rgba(37, 99, 235, .16);
|
|
border-color: rgba(96, 165, 250, .28);
|
|
|
|
span {
|
|
color: #93c5fd;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</style>
|