zhangwencui
2 天以前 a2002ba0e8aa2a0e4eee61b5205748d8f6e454fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script lang="ts" setup>
import type { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface';
 
import { computed, ref } from 'vue';
 
import { $t } from '@vben/locales';
 
import { Button, Upload } from 'ant-design-vue';
 
import { useUpload } from '#/components/upload/use-upload';
 
defineOptions({ name: 'TinymceImageUpload' });
 
const props = defineProps({
  disabled: {
    default: false,
    type: Boolean,
  },
  fullscreen: {
    default: false,
    type: Boolean,
  }, // 图片上传,是否放到全屏的位置
});
 
const emit = defineEmits(['uploading', 'done', 'error']);
 
const uploading = ref(false);
 
const getButtonProps = computed(() => {
  const { disabled } = props;
  return {
    disabled,
  };
});
 
async function customRequest(info: UploadRequestOption<any>) {
  // 1. emit 上传中
  const file = info.file as File;
  const name = file?.name;
  if (!uploading.value) {
    emit('uploading', name);
    uploading.value = true;
  }
 
  // 2. 执行上传
  const { httpRequest } = useUpload();
  try {
    const url = await httpRequest(file);
    emit('done', name, url);
  } catch {
    emit('error', name);
  } finally {
    uploading.value = false;
  }
}
</script>
<template>
  <div :class="[{ fullscreen }]" class="tinymce-image-upload">
    <Upload
      :show-upload-list="false"
      accept=".jpg,.jpeg,.gif,.png,.webp"
      multiple
      :custom-request="customRequest"
    >
      <Button type="primary" v-bind="{ ...getButtonProps }">
        {{ $t('ui.upload.imgUpload') }}
      </Button>
    </Upload>
  </div>
</template>
 
<style lang="scss" scoped>
.tinymce-image-upload {
  position: absolute;
  top: 4px;
  right: 10px;
  z-index: 20;
 
  &.fullscreen {
    position: fixed;
    z-index: 10000;
  }
}
</style>