开心的白菜
开心的白菜
  • 发布:2024-09-19 15:49
  • 更新:2024-09-19 15:49
  • 阅读:209

uniapp app自动监听权限

分类:uni-app

自动监听 onLaunch 引入 listener方法即可

监听权限

// @ts-nocheck  

// #ifdef APP-PLUS  
import permissions from "./permissions";  
import {closeNotify, drawNotify} from "@/utils/notify";  
const permissionListener = uni.createRequestPermissionListener()  
// #endif  

/**  
 * 监听权限  
 */  
export function listener() {  
    /* 是否已经弹出设置框 */  
    let hasConfirm = false  

    // 监听申请系统权限  
    permissionListener.onRequest((data: string[]) => {  
    })  
    // 监听弹出系统权限授权框  
    permissionListener.onConfirm((data: string[]) => {  
        const permissionsObj = getPermissionsObj(data)  
        drawNotify(permissionsObj.title, permissionsObj.desc)  
    })  
    // 监听权限申请完成  
    permissionListener.onComplete((data: string[]) => {  
        closeNotify()  

        const parts = data[data.length - 1].split('.');  
        let permissionName = parts[parts.length - 1];  

        const Manifest = plus.android.importClass('android.Manifest')  
        const MainActivity = plus.android.runtimeMainActivity()  
        const permissionStatus = MainActivity.checkSelfPermission(Manifest.permission[permissionName])  

        // 彻底拒绝  
        if (permissionStatus === -1 && !hasConfirm) {  
            hasConfirm = true  
            const permissionsObj = getPermissionsObj(data)  

            plus.nativeUI.confirm(permissionsObj.desc, (e) => {  
                if (e.index === 0) uni.openAppAuthorizeSetting()  
                hasConfirm = false  
            }, {  
                title: permissionsObj.title,  
                "buttons": ["去设置", "取消"],  
            });  
        }  
    })  
}  

function getPermissionsObj(data: string[]): { title: string, desc: string } {  
    let title: string = ""  
    let desc: string = ""  
    data.forEach((permission) => {  
        permissions.forEach(item => {  
            if (item.permissions.indexOf(permission) !== -1) {  
                title = item.title  
                desc = item.desc  
            }  
        })  
    })  

    return {title, desc}  
}  

权限设置

export interface Permissions {  
    title: string,  
    desc: string,  
    permissions: string[]  
}  

const permissions: Permissions[] = [  
    {  
        title: "读取短信权限",  
        desc: "读取短信权限达到个性化目的",  
        permissions: [  
            "android.permission.SEND_SMS",  
            "android.permission.RECEIVE_SMS",  
            "android.permission.READ_SMS",  
            "android.permission.RECEIVE_WAP_PUSH",  
            "android.permission.RECEIVE_MMS",  
        ]  
    },  
    {  
        title: "读取存储卡,包括相册等权限",  
        desc: "读取存储卡,包括相册等权限达到个性化目的",  
        permissions: [  
            "android.permission.READ_EXTERNAL_STORAGE",  
            "android.permission.WRITE_EXTERNAL_STORAGE",  
        ]  
    },  
    {  
        title: "读取联系人权限",  
        desc: "读取联系人权限达到个性化目的",  
        permissions: [  
            "android.permission.READ_CONTACTS",  
            "android.permission.WRITE_CONTACTS",  
            "android.permission.GET_ACCOUNTS",  
        ]  
    },  
    {  
        title: "读取手机权限",  
        desc: "读取手机权限达到个性化目的",  
        permissions: [  
            "android.permission.READ_PHONE_STATE",  
            "android.permission.CALL_PHONE",  
            "android.permission.READ_CALL_LOG",  
            "android.permission.WRITE_CALL_LOG",  
            "android.permission.ADD_VOICEMAIL",  
            "android.permission.USE_SIP",  
            "android.permission.PROCESS_OUTGOING_CALLS",  
        ]  
    },  
    {  
        title: "读取日历权限",  
        desc: "读取日历权限达到个性化目的",  
        permissions: [  
            "android.permission.READ_CALENDAR",  
            "android.permission.WRITE_CALENDAR",  
        ]  
    },  
    {  
        title: "读取相机权限",  
        desc: "读取相机权限达到个性化目的",  
        permissions: [  
            "android.permission.CAMERA",  
        ]  
    },  
    {  
        title: "读取位置权限",  
        desc: "读取位置权限达到个性化目的",  
        permissions: [  
            "android.permission.ACCESS_FINE_LOCATION",  
            "android.permission.ACCESS_COARSE_LOCATION",  
        ]  
    },  
    {  
        title: "读取传感器权限",  
        desc: "读取传感器权限达到个性化目的",  
        permissions: [  
            "android.permission.BODY_SENSORS",  
        ]  
    },  
    {  
        title: "读取麦克风权限",  
        desc: "读取麦克风权限达到个性化目的",  
        permissions: [  
            "android.permission.RECORD_AUDIO",  
        ]  
    },  
]  

export default permissions  

顶部信息

let view: any  
let idKey: string = 'permissionNotify'  

/**  
 * 绘制消息  
 * @description 可根据描述字数动态变更高度  
 * @param {string} title 标题  
 * @param {string} desc 描述  
 */  
export function drawNotify(title: string, desc: string) {  
    // 关闭之前的 notify  
    // @ts-ignore  
    let idView = plus.nativeObj.View.getViewById(idKey)  
    if (idView) idView.close()  

    const {windowWidth, statusBarHeight} = uni.getSystemInfoSync()  

    const xDistance: number = 16 // 距离屏幕两侧的距离  
    const tDistance: number = 16 // 距离顶部的距离  
    const radius: number = 16 // 盒子圆角  

    const xPadding: number = 16 // 盒子paddingX  
    const YPadding: number = 16 // 盒子paddingY  
    const space: number = 12 // 描述距离标题的距离  

    const titleSize: number = 16 // 标题文字大小  
    const descSize: number = 14 // 描述文字大小  

    const width: number = windowWidth - xDistance * 2  
    // @ts-ignore  
    const top: number = statusBarHeight + tDistance  

    const lineNum = Math.ceil(desc.length / Math.floor((width - YPadding * 2) / descSize)) * 1.2  

    const height = YPadding * 2 + titleSize + space + descSize * lineNum  

    // @ts-ignore  
    let _view = new plus.nativeObj.View(  
        idKey,  
        {  
            width: `${width}`,  
            top: `${top}`,  
            left: `${xDistance}`,  
            height: `${height}`,  
        },  
        [  
            {  
                tag: 'rect',  
                id: 'rect',  
                rectStyles: {  
                    color: '#FFFFFF',  
                    radius: `${radius}`,  
                },  
            },  
            {  
                tag: 'font',  
                id: 'title',  
                text: title,  
                textStyles: {size: `${titleSize}`, align: 'left', weight: 'bold'},  
                position: {top: `${YPadding}`, left: `${xPadding}`, height: 'wrap_content'},  
            },  
            {  
                tag: 'font',  
                id: 'desc',  
                text: desc,  
                textStyles: {size: `${descSize}`, align: 'left', color: '#666666', whiteSpace: 'normal'},  
                position: {  
                    top: `${YPadding + titleSize + space}`,  
                    left: `${xPadding}`,  
                    height: 'wrap_content',  
                    width: `${width - YPadding * 2}`,  
                },  
            },  
        ]  
    )  

    _view.show()  

    view = _view  
}  

/**  
 * 关闭消息  
 */  
export function closeNotify() {  
    if (view) view.close()  
}  
0 关注 分享

要回复文章请先登录注册