li.shaoyi 4 hónapja
szülő
commit
e8c677e72d

+ 74 - 31
src/packages/mobile/components/base/gallery/index.vue

@@ -29,7 +29,7 @@
 import { computed, PropType } from 'vue'
 import { Image, Icon, Loading, showFailToast } from 'vant'
 import { UploadFile } from './types'
-import plus from '@/utils/h5plus'
+import h5plus from '@/utils/h5plus'
 
 const props = defineProps({
     modelValue: {
@@ -76,43 +76,86 @@ const uploadFiles = computed({
     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
 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)
                 })
-            }, (error: DOMException) => {
-                showFailToast(error.message)
+            }, () => {
+                console.log('取消选择')
+            }, {
+                filter: props.fileType
             })
-        }, () => {
-            console.log('取消选择')
-        }, {
-            filter: props.fileType
         })
+    }).catch((error) => {
+        showFailToast(error)
     })
 }
 

+ 2 - 1
src/packages/mobile/components/base/uploader/index.vue

@@ -1,6 +1,7 @@
+<!-- 待优化 -->
 <template>
     <div class="app-uploader">
-        <!-- <span class="app-uploader__button" @click="onClickUpload" v-if="!hasPermission"></span> -->
+        <span class="app-uploader__button" @click="onClickUpload" v-if="!hasPermission"></span>
         <Uploader ref="uploaderRef" v-model="fileList" :max-count="maxCount" :max-size="5 * 1024 * 1024"
             @oversize="onOversize" :after-read="onAfterRead" @delete="onDelete" reupload />
         <Notify v-model:show="showNotify" :duration="0" :title="$t('common.tips30')" :content="$t('common.tips31')" />