index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!-- 待优化 -->
  2. <template>
  3. <div class="app-uploader">
  4. <span class="app-uploader__button" @click="onClickUpload" v-if="!hasPermission"></span>
  5. <Uploader ref="uploaderRef" v-model="fileList" :max-count="maxCount" :max-size="5 * 1024 * 1024"
  6. @oversize="onOversize" :after-read="onAfterRead" @delete="onDelete" reupload />
  7. <Notify v-model:show="showNotify" :duration="0" :title="$t('common.tips30')" :content="$t('common.tips31')" />
  8. </div>
  9. </template>
  10. <script lang="ts" setup>
  11. import { ref } from 'vue'
  12. import { showFailToast, Uploader, UploaderFileListItem } from 'vant'
  13. import { useGlobalStore, i18n } from '@/stores'
  14. import plus from '@/utils/h5plus'
  15. import service from '@/services'
  16. import axios from 'axios'
  17. import Notify from '@mobile/components/base/notify/index.vue'
  18. defineProps({
  19. maxCount: {
  20. type: Number,
  21. default: 1
  22. }
  23. })
  24. const { global: { t } } = i18n
  25. const emit = defineEmits(['success', 'delete'])
  26. const globalStore = useGlobalStore()
  27. const uploaderRef = ref()
  28. const showNotify = ref(false)
  29. const fileList = ref<UploaderFileListItem[]>([])
  30. const storagePermission = globalStore.getAndroidPermissions('READ_EXTERNAL_STORAGE')
  31. const hasPermission = ref(storagePermission === 1) // 是否已授权存储权限
  32. const onOversize = () => {
  33. showFailToast(t('common.tips29'))
  34. }
  35. const onClickUpload = () => {
  36. const status = globalStore.getAndroidPermissions('READ_EXTERNAL_STORAGE')
  37. switch (status) {
  38. case 0: {
  39. showNotify.value = true
  40. plus.requestPermissionReadExternalStorage({
  41. onSuccess: () => {
  42. showNotify.value = false
  43. globalStore.setAndroidPermissions('READ_EXTERNAL_STORAGE', 1)
  44. },
  45. onError: () => {
  46. showNotify.value = false
  47. globalStore.setAndroidPermissions('READ_EXTERNAL_STORAGE', -1)
  48. }
  49. })
  50. break
  51. }
  52. case 1: {
  53. hasPermission.value = true
  54. uploaderRef.value.chooseFile()
  55. break
  56. }
  57. default: {
  58. showFailToast(t('common.tips25'))
  59. }
  60. }
  61. }
  62. // eslint-disable-next-line
  63. const onAfterRead = (file: any) => {
  64. const data = new FormData()
  65. data.append('file', file.file)
  66. file.status = 'uploading'
  67. file.message = t('common.tips28')
  68. axios.post(service.getServiceConfig('uploadUrl'), data).then(res => {
  69. if (res.status == 200) {
  70. file.status = 'success'
  71. file.message = t('common.tips26')
  72. file.url = res.data[0]?.filePath
  73. const files = fileList.value.map((e) => e.url)
  74. emit('success', files.join(';'))
  75. } else {
  76. file.status = 'failed'
  77. file.message = t('common.tips27')
  78. }
  79. })
  80. }
  81. const onDelete = (file: UploaderFileListItem, detail: { name: string; index: number; }) => {
  82. emit('delete', detail.index)
  83. }
  84. </script>
  85. <style lang="less">
  86. @import './index.less';
  87. </style>