|
@@ -29,7 +29,7 @@
|
|
|
import { computed, PropType } from 'vue'
|
|
import { computed, PropType } from 'vue'
|
|
|
import { Image, Icon, Loading, showFailToast } from 'vant'
|
|
import { Image, Icon, Loading, showFailToast } from 'vant'
|
|
|
import { UploadFile } from './types'
|
|
import { UploadFile } from './types'
|
|
|
-import plus from '@/utils/h5plus'
|
|
|
|
|
|
|
+import h5plus from '@/utils/h5plus'
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
|
modelValue: {
|
|
modelValue: {
|
|
@@ -76,43 +76,86 @@ const uploadFiles = computed({
|
|
|
set: (val) => emit('update:modelValue', val)
|
|
set: (val) => emit('update:modelValue', val)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+// https://www.html5plus.org/doc/zh_cn/navigator.html#plus.navigator.checkPermission
|
|
|
|
|
+const checkImagesPermission = () => {
|
|
|
|
|
+ return new Promise<void>((resolve, reject) => {
|
|
|
|
|
+ if (h5plus.getSystemInfo('os') === 'Android') {
|
|
|
|
|
+ h5plus.onPlusReady((plus) => {
|
|
|
|
|
+ const [majorVersion] = plus.os.version.split('.') // 获取系统主版本号
|
|
|
|
|
+ const permissions: string[] = []
|
|
|
|
|
+
|
|
|
|
|
+ switch (true) {
|
|
|
|
|
+ case (+majorVersion <= 12):
|
|
|
|
|
+ permissions.push('android.permission.READ_EXTERNAL_STORAGE')
|
|
|
|
|
+ break
|
|
|
|
|
+ case (+majorVersion === 13):
|
|
|
|
|
+ permissions.push('android.permission.READ_MEDIA_IMAGES', 'android.permission.READ_MEDIA_VIDEO')
|
|
|
|
|
+ break
|
|
|
|
|
+ default:
|
|
|
|
|
+ permissions.push('android.permission.READ_MEDIA_VISUAL_USER_SELECTED')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const result = permissions.map((permission) => plus.navigator.checkPermission(permission))
|
|
|
|
|
+ if (result.some((value) => value === 'authorized')) {
|
|
|
|
|
+ resolve()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ h5plus.requestPermission({
|
|
|
|
|
+ permissions,
|
|
|
|
|
+ errorMessage: '相册权限未授权',
|
|
|
|
|
+ refuseMessage: '访问相册被拒绝',
|
|
|
|
|
+ onSuccess: () => resolve(),
|
|
|
|
|
+ onError: (error) => reject(error)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ } else {
|
|
|
|
|
+ resolve()
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// https://www.html5plus.org/doc/zh_cn/gallery.html#plus.gallery.pick
|
|
// https://www.html5plus.org/doc/zh_cn/gallery.html#plus.gallery.pick
|
|
|
const chooseFile = (index = -1) => {
|
|
const chooseFile = (index = -1) => {
|
|
|
- plus.onPlusReady((plus) => {
|
|
|
|
|
- plus.gallery.pick((path: string) => {
|
|
|
|
|
- plus.io.resolveLocalFileSystemURL(path, (entry: FileSystemFileEntry) => {
|
|
|
|
|
- entry.file((file) => {
|
|
|
|
|
- const reader = new plus.io.FileReader()
|
|
|
|
|
- reader.onloadend = (e: any) => {
|
|
|
|
|
- const fileBlob = base64ToFile(e.target.result, entry.name)
|
|
|
|
|
-
|
|
|
|
|
- const uploadFile: UploadFile = {
|
|
|
|
|
- fileName: entry.name,
|
|
|
|
|
- filePath: path,
|
|
|
|
|
- file: fileBlob
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (index > -1) {
|
|
|
|
|
- uploadFiles.value[index] = uploadFile
|
|
|
|
|
- } else {
|
|
|
|
|
- uploadFiles.value.push(uploadFile)
|
|
|
|
|
|
|
+ checkImagesPermission().then(() => {
|
|
|
|
|
+ h5plus.onPlusReady((plus) => {
|
|
|
|
|
+ plus.gallery.pick((path: string) => {
|
|
|
|
|
+ plus.io.resolveLocalFileSystemURL(path, (entry: FileSystemFileEntry) => {
|
|
|
|
|
+ entry.file((file) => {
|
|
|
|
|
+ const reader = new plus.io.FileReader()
|
|
|
|
|
+ reader.onloadend = (e: any) => {
|
|
|
|
|
+ const fileBlob = base64ToFile(e.target.result, entry.name)
|
|
|
|
|
+
|
|
|
|
|
+ const uploadFile: UploadFile = {
|
|
|
|
|
+ fileName: entry.name,
|
|
|
|
|
+ filePath: path,
|
|
|
|
|
+ file: fileBlob
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (index > -1) {
|
|
|
|
|
+ uploadFiles.value[index] = uploadFile
|
|
|
|
|
+ } else {
|
|
|
|
|
+ uploadFiles.value.push(uploadFile)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const currentIndex = index > -1 ? index : uploadFiles.value.length - 1
|
|
|
|
|
+ uploadToServer(currentIndex)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- const currentIndex = index > -1 ? index : uploadFiles.value.length - 1
|
|
|
|
|
- uploadToServer(currentIndex)
|
|
|
|
|
- }
|
|
|
|
|
- reader.readAsDataURL(file)
|
|
|
|
|
- }, (error) => {
|
|
|
|
|
|
|
+ reader.readAsDataURL(file)
|
|
|
|
|
+ }, (error) => {
|
|
|
|
|
+ showFailToast(error.message)
|
|
|
|
|
+ })
|
|
|
|
|
+ }, (error: DOMException) => {
|
|
|
showFailToast(error.message)
|
|
showFailToast(error.message)
|
|
|
})
|
|
})
|
|
|
- }, (error: DOMException) => {
|
|
|
|
|
- showFailToast(error.message)
|
|
|
|
|
|
|
+ }, () => {
|
|
|
|
|
+ console.log('取消选择')
|
|
|
|
|
+ }, {
|
|
|
|
|
+ filter: props.fileType
|
|
|
})
|
|
})
|
|
|
- }, () => {
|
|
|
|
|
- console.log('取消选择')
|
|
|
|
|
- }, {
|
|
|
|
|
- filter: props.fileType
|
|
|
|
|
})
|
|
})
|
|
|
|
|
+ }).catch((error) => {
|
|
|
|
|
+ showFailToast(error)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|