ImgUpload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div :class="[prefixCls, { fullscreen }]">
  3. <Upload
  4. name="file"
  5. multiple
  6. @change="handleChange"
  7. :action="uploadUrl"
  8. :showUploadList="false"
  9. :data="getBizData()"
  10. :headers="getheader()"
  11. accept=".jpg,.jpeg,.gif,.png,.webp"
  12. >
  13. <a-button type="primary" v-bind="{ ...getButtonProps }">
  14. {{ t('component.upload.imgUpload') }}
  15. </a-button>
  16. </Upload>
  17. </div>
  18. </template>
  19. <script lang="ts">
  20. import { defineComponent, computed } from 'vue';
  21. import { Upload } from 'ant-design-vue';
  22. import { useDesign } from '/@/hooks/web/useDesign';
  23. import { useGlobSetting } from '/@/hooks/setting';
  24. import { useI18n } from '/@/hooks/web/useI18n';
  25. import { getToken } from '/@/utils/auth';
  26. import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
  27. export default defineComponent({
  28. name: 'TinymceImageUpload',
  29. components: { Upload },
  30. props: {
  31. fullscreen: {
  32. type: Boolean,
  33. },
  34. disabled: {
  35. type: Boolean,
  36. default: false,
  37. },
  38. },
  39. emits: ['uploading', 'done', 'error'],
  40. setup(props, { emit }) {
  41. let uploading = false;
  42. //update-begin-author:taoyan date:2022-5-13 for: 富文本上传图片不支持
  43. function getheader() {
  44. return { 'X-Access-Token': getToken() };
  45. }
  46. function getBizData() {
  47. return {
  48. biz: 'jeditor',
  49. jeditor: '1',
  50. };
  51. }
  52. const { domainUrl } = useGlobSetting();
  53. const uploadUrl = domainUrl + '/sys/common/upload';
  54. //update-end-author:taoyan date:2022-5-13 for: 富文本上传图片不支持
  55. const { t } = useI18n();
  56. const { prefixCls } = useDesign('tinymce-img-upload');
  57. const getButtonProps = computed(() => {
  58. const { disabled } = props;
  59. return {
  60. disabled,
  61. };
  62. });
  63. function handleChange(info: Recordable) {
  64. const file = info.file;
  65. const status = file?.status;
  66. //const url = file?.response?.url;
  67. const name = file?.name;
  68. if (status === 'uploading') {
  69. if (!uploading) {
  70. emit('uploading', name);
  71. uploading = true;
  72. }
  73. } else if (status === 'done') {
  74. //update-begin-author:taoyan date:2022-5-13 for: 富文本上传图片不支持
  75. let realUrl = getFileAccessHttpUrl(file.response.message);
  76. emit('done', name, realUrl);
  77. //update-end-author:taoyan date:2022-5-13 for: 富文本上传图片不支持
  78. uploading = false;
  79. } else if (status === 'error') {
  80. emit('error');
  81. uploading = false;
  82. }
  83. }
  84. return {
  85. prefixCls,
  86. handleChange,
  87. uploadUrl,
  88. getheader,
  89. getBizData,
  90. t,
  91. getButtonProps,
  92. };
  93. },
  94. });
  95. </script>
  96. <style lang="less" scoped>
  97. @prefix-cls: ~'@{namespace}-tinymce-img-upload';
  98. .@{prefix-cls} {
  99. position: absolute;
  100. top: 4px;
  101. right: 10px;
  102. z-index: 20;
  103. &.fullscreen {
  104. position: fixed;
  105. z-index: 10000;
  106. }
  107. }
  108. </style>