办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<script lang="ts" setup>
import { computed } from 'vue';
 
import { useAppConfig } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';
 
import { message, Upload } from 'ant-design-vue';
 
defineOptions({ name: 'TinymceImageUpload' });
 
const props = defineProps({
  disabled: {
    default: false,
    type: Boolean,
  },
  fullscreen: {
    type: Boolean,
  },
});
 
const emit = defineEmits(['uploading', 'done', 'error']);
 
let uploading = false;
 
const { apiURL, clientId } = useAppConfig(
  import.meta.env,
  import.meta.env.PROD,
);
const accessStore = useAccessStore();
const uploadUrl = `${apiURL}/resource/oss/upload`;
// 使用upload组件只能这样上传
const headers = {
  Authorization: `Bearer ${accessStore.accessToken}`,
  clientId,
};
 
const getButtonProps = computed(() => {
  const { disabled } = props;
  return {
    disabled,
  };
});
 
function handleChange(info: Record<string, any>) {
  const file = info.file;
  const status = file?.status;
  // const url = file?.response?.data.url;
  const name = file?.name;
 
  switch (status) {
    case 'done': {
      // http 200会走到这里  需要再次判断
      const { response } = file;
      const { code, data, msg = '服务器错误' } = response;
      if (code === 200) {
        const { url } = data;
        emit('done', name, url);
      } else {
        message.error(msg);
      }
      // emit('done', name, url);
      uploading = false;
 
      break;
    }
    case 'error': {
      emit('error');
      uploading = false;
 
      break;
    }
    case 'uploading': {
      if (!uploading) {
        emit('uploading', name);
        uploading = true;
      }
 
      break;
    }
    // No default
  }
}
</script>
<template>
  <div :class="[{ fullscreen }]" class="tinymce-image-upload">
    <Upload
      :action="uploadUrl"
      :headers="headers"
      :show-upload-list="false"
      accept=".jpg,.jpeg,.gif,.png,.webp"
      multiple
      name="file"
      @change="handleChange"
    >
      <!-- 这里要改成i18n -->
      <a-button type="primary" v-bind="{ ...getButtonProps }">
        图片上传
      </a-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>