2025-10-30 16:33:33 +08:00
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
|
import type { UploadRawFile } from 'element-plus';
|
|
|
|
|
|
|
|
|
|
|
|
import type { Reply } from './types';
|
|
|
|
|
|
|
|
|
|
|
|
import { computed, reactive, ref } from 'vue';
|
|
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
import { IconifyIcon } from '@vben/icons';
|
2025-10-30 16:33:33 +08:00
|
|
|
|
import { useAccessStore } from '@vben/stores';
|
|
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
import {
|
|
|
|
|
|
ElButton,
|
|
|
|
|
|
ElCol,
|
|
|
|
|
|
ElDialog,
|
|
|
|
|
|
ElMessage,
|
|
|
|
|
|
ElRow,
|
|
|
|
|
|
ElUpload,
|
|
|
|
|
|
} from 'element-plus';
|
2025-10-30 16:33:33 +08:00
|
|
|
|
|
2025-11-03 14:04:00 +08:00
|
|
|
|
import { UploadType, useBeforeUpload } from '#/utils/useUpload';
|
2025-11-20 10:34:21 +08:00
|
|
|
|
import MaterialSelect from '#/views/mp/components/wx-material-select/wx-material-select.vue';
|
2025-10-30 16:33:33 +08:00
|
|
|
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
|
modelValue: Reply;
|
|
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
|
(e: 'update:modelValue', v: Reply): void;
|
|
|
|
|
|
}>();
|
|
|
|
|
|
|
2025-11-21 18:19:42 +08:00
|
|
|
|
const accessStore = useAccessStore();
|
2025-10-30 16:33:33 +08:00
|
|
|
|
const UPLOAD_URL = `${import.meta.env.VITE_BASE_URL}/admin-api/mp/material/upload-temporary`;
|
2025-11-21 18:19:42 +08:00
|
|
|
|
const HEADERS = { Authorization: `Bearer ${accessStore.accessToken}` };
|
2025-10-30 16:33:33 +08:00
|
|
|
|
const reply = computed<Reply>({
|
|
|
|
|
|
get: () => props.modelValue,
|
|
|
|
|
|
set: (val) => emit('update:modelValue', val),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const showDialog = ref(false);
|
|
|
|
|
|
const fileList = ref([]);
|
|
|
|
|
|
const uploadData = reactive({
|
|
|
|
|
|
accountId: reply.value.accountId,
|
|
|
|
|
|
introduction: '',
|
2025-11-20 21:09:02 +08:00
|
|
|
|
title: '',
|
|
|
|
|
|
type: 'image',
|
2025-10-30 16:33:33 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
/** 图片上传前校验 */
|
|
|
|
|
|
function beforeImageUpload(rawFile: UploadRawFile) {
|
|
|
|
|
|
return useBeforeUpload(UploadType.Image, 2)(rawFile);
|
|
|
|
|
|
}
|
2025-10-30 16:33:33 +08:00
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
/** 上传成功 */
|
|
|
|
|
|
function onUploadSuccess(res: any) {
|
2025-10-30 16:33:33 +08:00
|
|
|
|
if (res.code !== 0) {
|
2025-11-21 18:19:42 +08:00
|
|
|
|
ElMessage.error(`上传出错:${res.msg}`);
|
2025-10-30 16:33:33 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空上传时的各种数据
|
|
|
|
|
|
fileList.value = [];
|
|
|
|
|
|
uploadData.title = '';
|
|
|
|
|
|
uploadData.introduction = '';
|
|
|
|
|
|
|
|
|
|
|
|
// 上传好的文件,本质是个素材,所以可以进行选中
|
|
|
|
|
|
selectMaterial(res.data);
|
2025-11-03 11:18:45 +08:00
|
|
|
|
}
|
2025-10-30 16:33:33 +08:00
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
/** 删除图片 */
|
|
|
|
|
|
function onDelete() {
|
2025-10-30 16:33:33 +08:00
|
|
|
|
reply.value.mediaId = null;
|
|
|
|
|
|
reply.value.url = null;
|
|
|
|
|
|
reply.value.name = null;
|
2025-11-03 11:18:45 +08:00
|
|
|
|
}
|
2025-10-30 16:33:33 +08:00
|
|
|
|
|
2025-11-03 11:18:45 +08:00
|
|
|
|
/** 选择素材 */
|
|
|
|
|
|
function selectMaterial(item: any) {
|
2025-10-30 16:33:33 +08:00
|
|
|
|
showDialog.value = false;
|
|
|
|
|
|
|
|
|
|
|
|
// reply.value.type = 'image'
|
|
|
|
|
|
reply.value.mediaId = item.mediaId;
|
|
|
|
|
|
reply.value.url = item.url;
|
|
|
|
|
|
reply.value.name = item.name;
|
2025-11-03 11:18:45 +08:00
|
|
|
|
}
|
2025-10-30 16:33:33 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<!-- 情况一:已经选择好素材、或者上传好图片 -->
|
2025-11-07 11:33:32 +08:00
|
|
|
|
<div
|
|
|
|
|
|
class="mx-auto mb-[10px] w-[280px] border border-[#eaeaea] p-[10px]"
|
|
|
|
|
|
v-if="reply.url"
|
|
|
|
|
|
>
|
|
|
|
|
|
<img class="w-full" :src="reply.url" />
|
|
|
|
|
|
<p
|
|
|
|
|
|
class="overflow-hidden text-ellipsis whitespace-nowrap text-center text-xs"
|
|
|
|
|
|
v-if="reply.name"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ reply.name }}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<ElRow class="pt-[10px] text-center" justify="center">
|
2025-11-03 11:18:45 +08:00
|
|
|
|
<ElButton type="danger" circle @click="onDelete">
|
|
|
|
|
|
<IconifyIcon icon="ep:delete" />
|
|
|
|
|
|
</ElButton>
|
|
|
|
|
|
</ElRow>
|
2025-10-30 16:33:33 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<!-- 情况二:未做完上述操作 -->
|
2025-11-07 11:33:32 +08:00
|
|
|
|
<ElRow v-else class="text-center" align="middle">
|
2025-10-30 16:33:33 +08:00
|
|
|
|
<!-- 选择素材 -->
|
2025-11-07 11:33:32 +08:00
|
|
|
|
<ElCol
|
|
|
|
|
|
:span="12"
|
|
|
|
|
|
class="h-[160px] w-[49.5%] border border-[rgb(234,234,234)] py-[50px]"
|
|
|
|
|
|
>
|
2025-11-03 11:18:45 +08:00
|
|
|
|
<ElButton type="success" @click="showDialog = true">
|
|
|
|
|
|
素材库选择 <IconifyIcon icon="ep:circle-check" />
|
|
|
|
|
|
</ElButton>
|
|
|
|
|
|
<ElDialog
|
2025-10-30 16:33:33 +08:00
|
|
|
|
title="选择图片"
|
|
|
|
|
|
v-model="showDialog"
|
|
|
|
|
|
width="90%"
|
|
|
|
|
|
append-to-body
|
|
|
|
|
|
destroy-on-close
|
|
|
|
|
|
>
|
2025-11-13 18:35:10 +08:00
|
|
|
|
<MaterialSelect
|
2025-10-30 16:33:33 +08:00
|
|
|
|
type="image"
|
|
|
|
|
|
:account-id="reply.accountId"
|
|
|
|
|
|
@select-material="selectMaterial"
|
|
|
|
|
|
/>
|
2025-11-03 11:18:45 +08:00
|
|
|
|
</ElDialog>
|
|
|
|
|
|
</ElCol>
|
2025-10-30 16:33:33 +08:00
|
|
|
|
<!-- 文件上传 -->
|
2025-11-07 11:33:32 +08:00
|
|
|
|
<ElCol
|
|
|
|
|
|
:span="12"
|
|
|
|
|
|
class="float-right h-[160px] w-[49.5%] border border-[rgb(234,234,234)] py-[50px]"
|
|
|
|
|
|
>
|
2025-11-21 18:19:42 +08:00
|
|
|
|
{{ uploadData }}
|
2025-11-03 11:18:45 +08:00
|
|
|
|
<ElUpload
|
2025-10-30 16:33:33 +08:00
|
|
|
|
:action="UPLOAD_URL"
|
|
|
|
|
|
:headers="HEADERS"
|
|
|
|
|
|
multiple
|
|
|
|
|
|
:limit="1"
|
|
|
|
|
|
:file-list="fileList"
|
|
|
|
|
|
:data="uploadData"
|
|
|
|
|
|
:before-upload="beforeImageUpload"
|
|
|
|
|
|
:on-success="onUploadSuccess"
|
|
|
|
|
|
>
|
2025-11-03 11:18:45 +08:00
|
|
|
|
<ElButton type="primary">上传图片</ElButton>
|
2025-10-30 16:33:33 +08:00
|
|
|
|
<template #tip>
|
|
|
|
|
|
<span>
|
2025-11-07 11:33:32 +08:00
|
|
|
|
<div class="text-center leading-[18px]">
|
2025-10-30 16:33:33 +08:00
|
|
|
|
支持 bmp/png/jpeg/jpg/gif 格式,大小不超过 2M
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</template>
|
2025-11-03 11:18:45 +08:00
|
|
|
|
</ElUpload>
|
|
|
|
|
|
</ElCol>
|
|
|
|
|
|
</ElRow>
|
2025-10-30 16:33:33 +08:00
|
|
|
|
</div>
|
2025-11-21 18:19:42 +08:00
|
|
|
|
</template>
|