风中凌乱的L
风中凌乱的L
  • 发布:2023-10-31 18:04
  • 更新:2024-07-01 17:22
  • 阅读:4389

华为审核申请权限告知目的

分类:HBuilderX

华为app上架审核,申请权限的时候只有弹框,没有告知目的,被审核驳回了。。。
然后看官方,发现plus.android.requestPermissions只有点击才有回调啊,然后看到一个大佬代码设置了一个初始值,缝缝补补,整出来一个将就使用的完整代码。合不合适看最后压缩包视频就知道啦

//这是vuex单独一个模块的完整代码  
const state = {  
    WRITE_EXTERNAL_STORAGE: false,  
    READ_EXTERNAL_STORAGE: false,  
    CALL_PHONE: false,  
    isIos: plus.os.name == "iOS",  
    mapping: {  
        'WRITE_EXTERNAL_STORAGE': {  
            title: "存储空间/照片权限说明",  
            content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、发布评论/分享、下载、与客服沟通等场景中读取和写入相册和文件内容。",  
            methods: 'SET_WRITE_EXTERNAL_STORAGE'  
        },  
        'READ_EXTERNAL_STORAGE': {  
            title: "存储空间/照片权限说明",  
            content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、发布评论/分享、下载、与客服沟通等场景中读取和写入相册和文件内容。",  
            methods: 'SET_READ_EXTERNAL_STORAGE'  
        },  
        'CALL_PHONE': {  
            title: "拨打/管理电话权限说明",  
            content: "便于您使用该功能联系商家或者商家与您联系等场景",  
            methods: 'SET_CALL_PHONE'  
        }  
    }  
}  
const mutations = {  
    SET_WRITE_EXTERNAL_STORAGE(state, val) {  
        state.WRITE_EXTERNAL_STORAGE = val  
    },  
    SET_CALL_PHONE(state, val) {  
        state.CALL_PHONE = val  
    },  
    SET_READ_EXTERNAL_STORAGE(state, val) {  
        state.READ_EXTERNAL_STORAGE = val  
    }  
}  
const actions = {  
    //权限获取  
    async requestPermissions({  
        state,  
        dispatch,  
        commit  
    }, permissionID) {  
        try {  
            if (!state[permissionID]) {  
                var viewObj = await dispatch('nativeObjView', permissionID);  
                viewObj.show();  
            }  
            console.log('android.permission.' + permissionID, '当前手机权限');  
            return new Promise(async (resolve, reject) => {  
                // Android权限查询  
                function requestAndroidPermission(permissionID_) {  
                    return new Promise((resolve, reject) => {  
                        plus.android.requestPermissions(  
                            [  
                                permissionID_  
                            ], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装  
                            function (resultObj) {  
                                var result = 0;  
                                for (var i = 0; i < resultObj.granted.length; i++) {  
                                    var grantedPermission = resultObj.granted[i];  
                                    console.log('已获取的权限:' + grantedPermission);  
                                    result = 1  
                                }  
                                for (var i = 0; i < resultObj.deniedPresent  
                                    .length; i++) {  
                                    var deniedPresentPermission = resultObj  
                                        .deniedPresent[  
                                        i];  
                                    console.log('拒绝本次申请的权限:' + deniedPresentPermission);  
                                    result = 0  
                                }  
                                for (var i = 0; i < resultObj.deniedAlways  
                                    .length; i++) {  
                                    var deniedAlwaysPermission = resultObj.deniedAlways[  
                                        i];  
                                    console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);  
                                    result = -1  
                                }  
                                resolve(result);  
                            },  
                            function (error) {  
                                console.log('申请权限错误:' + error.code + " = " + error  
                                    .message);  
                                resolve({  
                                    code: error.code,  
                                    message: error.message  
                                });  
                            }  
                        );  
                    });  
                }  

                const result = await requestAndroidPermission(  
                    'android.permission.' + permissionID  
                );  
                if (result === 1) {  
                    //'已获得授权'  
                    commit(state.mapping[permissionID].methods, true)  
                } else if (result === 0) {  
                    //'未获得授权'  
                    commit(state.mapping[permissionID].methods, false)  
                } else {  
                    commit(state.mapping[permissionID].methods, true)  
                    uni.showModal({  
                        title: '提示',  
                        content: '操作权限已被拒绝,请手动前往设置',  
                        confirmText: "立即设置",  
                        success: (res) => {  
                            if (res.confirm) {  
                                dispatch('gotoAppPermissionSetting')  
                            }  
                        }  
                    })  
                }  
                if (viewObj) viewObj.close()  
                resolve(result);  
            });  
        } catch (error) {  
            console.log(error);  
            reject(error);  
        }  
    },  
    //提示框  
    nativeObjView({  
        state  
    }, permissionID) {  
        const systemInfo = uni.getSystemInfoSync();  
        const statusBarHeight = systemInfo.statusBarHeight;  
        const navigationBarHeight = systemInfo.platform === 'android' ? 48 :  
            44; // Set the navigation bar height based on the platform  
        const totalHeight = statusBarHeight + navigationBarHeight;  
        let view = new plus.nativeObj.View('per-modal', {  
            top: '0px',  
            left: '0px',  
            width: '100%',  
            backgroundColor: '#444',  
            //opacity: '.9'   
        })  
        view.drawRect({  
            color: '#fff',  
            radius: '5px'  
        }, {  
            top: totalHeight + 'px',  
            left: '5%',  
            width: '90%',  
            height: "100px",  
        })  
        view.drawText(state.mapping[permissionID].title, {  
            top: totalHeight + 5 + 'px',  
            left: "8%",  
            height: "30px"  
        }, {  
            align: "left",  
            color: "#000",  
        }, {  
            onClick: function (e) {  
                console.log(e);  
            }  
        })  
        view.drawText(state.mapping[permissionID].content, {  
            top: totalHeight + 35 + 'px',  
            height: "60px",  
            left: "8%",  
            width: "84%"  
        }, {  
            whiteSpace: 'normal',  
            size: "14px",  
            align: "left",  
            color: "#656563"  
        })  

        function show() {  
            view = plus.nativeObj.View.getViewById('per-modal');  
            view.show()  
            view = null  //这里要为null,不然隐藏不了,不知道为啥  
        }  

        function close() {  
            view = plus.nativeObj.View.getViewById('per-modal');  
            view.close();  
            view = null  
        }  
        return {  
            show,  
            close  
        }  
    },  

    // 跳转到**应用**的权限页面  
    gotoAppPermissionSetting({  
        state  
    }) {  
        if (state.isIos) {  
            var UIApplication = plus.ios.import("UIApplication");  
            var application2 = UIApplication.sharedApplication();  
            var NSURL2 = plus.ios.import("NSURL");  
            // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");       
            var setting2 = NSURL2.URLWithString("app-settings:");  
            application2.openURL(setting2);  

            plus.ios.deleteObject(setting2);  
            plus.ios.deleteObject(NSURL2);  
            plus.ios.deleteObject(application2);  
        } else {  
            // console.log(plus.device.vendor);  
            var Intent = plus.android.importClass("android.content.Intent");  
            var Settings = plus.android.importClass("android.provider.Settings");  
            var Uri = plus.android.importClass("android.net.Uri");  
            var mainActivity = plus.android.runtimeMainActivity();  
            var intent = new Intent();  
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  
            var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);  
            intent.setData(uri);  
            mainActivity.startActivity(intent);  
        }  
    }  
}  
export default {  
    namespaced: true,  
    state,  
    mutations,  
    actions  
};  

我是定义vuex里面的一个模块上面完整代码。
然后在原来的基础上加上这个判断就好啦。
<script>
export default {
data() {
return {
}
},
methods: {
async save() {
let result = await this.$store.dispatch("app/requestPermissions",
'WRITE_EXTERNAL_STORAGE')
if (result !== 1) return
uni.saveImageToPhotosAlbum({
filePath: "https://shop.baihuitimes.com/uploads/def/20230913/3fcaeccc6290e1b8367c00ea0aa600e4.png",
success: function(res) {
uni.showToast({
title: '保存成功',
icon:"success"
});
},
fail: function(res) {
uni.showToast({
title: '保存失败',
icon:"fail"
});
},
})
}
}
}
</script>

最后说明,如果用户没有点击授权的操作,将会无法及时拿到权限回调,可以自己在show里面再改改。上架审核人员前期点点点影响不到的。

4 关注 分享
1***@qq.com 1***@qq.com 8***@qq.com DCloud_heavensoft

要回复文章请先登录注册

ztb

ztb

博主你好 我的hbx版本是4.15的 在运行之前我新打了自定义基座 调试的时候提示了 uni.createRequestPermissionListener is not a function 请问一下 这个是什么原因呢
2024-07-01 17:22
风中凌乱的L

风中凌乱的L (作者)

回复 2***@qq.com :
看看这个 https://blog.csdn.net/m0_59203969/article/details/138119107
2024-04-23 12:54
风中凌乱的L

风中凌乱的L (作者)

2024.4.23更新pinia集中管理,使用官方 uni.createRequestPermissionListener() 更稳定简介 https://blog.csdn.net/m0_59203969/article/details/138119107
2024-04-23 12:52
2***@qq.com

2***@qq.com

按照作者的方法,为什么在点击第一次的时候没有上方的文字说明,点击第二次就有了
2024-03-19 10:48
DCloud_heavensoft

DCloud_heavensoft

4.0新增监听: Android平台 新增 监听权限申请事件,适用于应用商店要求申请权限时弹出用途说明 [详情](https://uniapp.dcloud.net.cn/api/system/create-request-permission-listener)
2024-03-07 17:28
石倩霞

石倩霞

回复 h***@163.com :
大佬最后怎么处理的呢,请教一下,已经驳回2次了
2024-03-07 16:58
a***@p8os.com

a***@p8os.com

回复 h***@163.com :
请问一下是怎么处理的
2024-02-28 16:46
h***@163.com

h***@163.com

回复 h***@163.com :
解决了。
2024-01-17 16:46
h***@163.com

h***@163.com

我有疑问,如果用uni.chooseimage,调用相机和相册在弹出actionsheet(这个actionsheet是uni.chooseimage自带的)并选择相机还是相册后,那么这个权限怎么弄呢。直接在chooseImage前面加这个权限,没法知道用户点的是相机还是相册
2024-01-17 15:54
5***@qq.com

5***@qq.com

回复 l***@163.com :
看看这个教程可以的: https://blog.csdn.net/weixin_45458358/article/details/135151702?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170390622816800188573784%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=170390622816800188573784&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-135151702-null-null.142^v99^pc_search_result_base7&utm_term=%E8%A7%A3%E5%86%B3%E5%8D%8E%E4%B8%BA%E7%AD%89%E4%B8%8A%E6%9E%B6%E8%A2%AB%E6%8B%92%E9%97%AE%E9%A2%98%2C%E8%BD%AF%E4%BB%B6%E5%9C%A8%E8%BF%90%E8%A1%8C%E6%97%B6&spm=1018.2226.3001.4187
2023-12-30 11:19