123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <template>
- <a-upload :show-file-list="true" :custom-request="handelUpload == null ? customRequest : handelUpload"
- :list-type="listType" :file-list="fileList" :limit="minx" @before-remove="beforeRemove" :show-remove-button="showRemoveButton" :show-cancel-button="false"
- :image-preview="imagePreview" :accept="accept" @before-upload="beforeUpload" />
- </template>
- <script setup>
- import { toRef, ref, watch, watchEffect, onMounted } from 'vue';
- import { useSystemStore } from '@/store/modules/systemStore';
- import { Modal } from "@arco-design/web-vue";
- import { useI18n } from 'vue-i18n';
- import { Message } from "@arco-design/web-vue";
- const systemStore = useSystemStore();
- const { t } = useI18n();
- const customRequest = async (option) => {
- const { file } = option.fileItem;
- const fileName = `thumbnail_${file.name}`;
- const key = `test/${fileName}`;
-
- const client = await systemStore.getSTSClient();
- const resClient = await client.putObject({
- key: key,
- body: file
- });
-
- if (resClient.statusCode === 200) {
- const uploadedFileUrl = resClient.url;
- fileList.value.push({ url: uploadedFileUrl, name: fileName, status: resClient.statusCode, uid: '0' });
-
- if (minx.value === 1) {
- emit('update:modelValue', uploadedFileUrl);
- } else {
- emit('update:modelValue', fileList.value.map(res => res.url).join(','));
- }
- }
- };
- const props = defineProps({
- modelValue: {
- type: [String, Array],
- default: "",
- },
- minx: {
- type: Number,
- default: 1
- },
- listType: {
- type: String,
- default: 'picture-card'
- },
- imagePreview: {
- type: Boolean,
- default: true
- },
- accept: {
- type: String,
- default: ''
- },
- handelUpload: {
- type: Function,
- default: null
- },
- showRemoveButton:{
- type: Boolean,
- default: true
- },
- beforeUpload: {
- type: Boolean,
- default: false,
- },
- });
- const minx = toRef(props, 'minx');
- const modelValue = toRef(props, 'modelValue');
- const listType = toRef(props, 'listType');
- const imagePreview = toRef(props, 'imagePreview');
- const handelUpload = toRef(props, 'handelUpload');
- const emit = defineEmits(["update:modelValue"]);
- const fileList = ref([]);
- const beforeRemove = (file) => {
- return new Promise((resolve, reject) => {
- Modal.confirm({
- title: $t("customer.confirmRemoveUpload"),
- onOk: () => {
-
- fileList.value = fileList.value.filter((item) => item.url !== file.url);
- if (minx.value === 1) {
- emit(
- "update:modelValue",
- fileList.value.length > 0 ? fileList.value[0].url : ""
- );
- } else {
- emit(
- "update:modelValue",
- fileList.value.map((res) => res.url).join(",")
- );
- }
- resolve(true);
- },
- onCancel: () => reject("cancel"),
- });
- });
- };
- watchEffect(() => {
- if (modelValue.value) {
- if (Array.isArray(modelValue.value)) {
-
- fileList.value = modelValue.value.map(item => ({ url: item, status: item.status, name: item.name }));
- } else if (typeof modelValue.value === 'string') {
-
- const urlList = modelValue.value.split(',').map(url => ({ url: url.trim() }));
- fileList.value = urlList;
- }
- } else {
- fileList.value = [];
- }
- })
- onMounted(() => {
- })
- const beforeUpload = (rawFile) => {
- if(!props.beforeUpload) return true
- const isAllowType =
- rawFile.type === "image/png" ||
- rawFile.type === "image/jpg" ||
- rawFile.type === "image/jpeg";
- if (!isAllowType) {
- Message.error("图片仅支持jpg或png");
- return false;
- } else if (rawFile.size / 1024 / 1024 > 2) {
- Message.error("图片不能大于2M");
- return false;
- }
- return true;
- };
- </script>
- <style scoped>
- </style>
|