oss.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <el-upload ref="upload" :action="action" :auto-upload="auto" v-bind="$attrs" :data="data" :before-upload="initOss" :on-success="handleSuccess">
  3. <template v-for="(index, name) in $slots" v-slot:[name]>
  4. <slot :name="name"></slot>
  5. </template>
  6. </el-upload>
  7. </template>
  8. <script setup lang="ts">
  9. import http from "/admin/support/http"
  10. import {ref} from "vue";
  11. import Message from "/admin/support/message";
  12. const props = defineProps({
  13. auto: {
  14. type: Boolean,
  15. default: true,
  16. },
  17. modelValue: {
  18. type: Boolean,
  19. default: false,
  20. require: true,
  21. },
  22. })
  23. const action = ref('')
  24. const data = ref({
  25. OSSAccessKeyId: '',
  26. policy: '',
  27. Signature: '',
  28. key: '',
  29. host: '',
  30. dir: '',
  31. expire: '',
  32. success_action_status: 200
  33. })
  34. const emits = defineEmits(['update:modelValue'])
  35. const initOss = async (file) => {
  36. if (file.size > 10 * 1024 * 1024) {
  37. Message.error('最大支持 10MB 文件')
  38. return
  39. }
  40. await http.get('upload/oss').then(r => {
  41. const {accessKeyId, bucket, dir, expire, host, policy, signature, url} = r.data.data
  42. action.value = host
  43. data.value.OSSAccessKeyId = accessKeyId
  44. data.value.policy = policy
  45. data.value.Signature = signature
  46. data.value.key = dir + file.name
  47. data.value.host = host
  48. data.value.dir = dir
  49. data.value.expire = expire
  50. })
  51. }
  52. const handleSuccess = (r) => {
  53. emits('update:modelValue', action.value + data.value.key)
  54. }
  55. </script>