guoyu/Test/yuyin/uni_modules/xwq-speech-to-text/utssdk/app-android/getPermission.uts
2025-12-03 18:58:36 +08:00

43 lines
1.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { UTSAndroid } from "io.dcloud.uts";
type Perssion={
isPass:boolean,
failList?:Array<string>,
successList?:Array<string>
}
//请求系统权限 isPass为true代表全部权限通过successList为通过的权限 failList为失败的权限
function getSystemPermission(perssionList:string[],callback:(val:Perssion)=>void):void{
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!,perssionList,function(allRight:boolean,grantedList:string[]){
if(allRight){
// 用户同意了全部权限
callback({isPass:true,successList:perssionList,failList:[]} as Perssion)
}else{
// 用户仅同意了某些权限
let failList:string[]=(perssionList as string[]).filter((name:string,index:number):boolean=>!grantedList.includes(name));
callback({isPass:false,successList:grantedList,failList} as Perssion)
}
},function(doNotAskAgain:boolean,grantedList:string[]){
// 不再询问的权限
console.log('doNotAskAgain===',doNotAskAgain)
if(doNotAskAgain){
callback({isPass:false,failList:grantedList,successList:[]} as Perssion)
}else{
if(grantedList.length===perssionList.length){
callback({isPass:false,failList:grantedList,successList:[]} as Perssion)
}
}
})
};
//初始化需要的权限
export function initPerssion(val:string[]):Promise<Perssion>{
return new Promise((resolve,reject)=>{
//判断是否有权限
let perssion=val;
getSystemPermission(perssion,(res)=>{
resolve(res);
})
})
}