|
api.js代码如下:
/**
* 判断APP是否持有该权限
* @param array one_per - 权限数组['camera','location']
*/
u.hasPermission = function(one_per) {
var perms = new Array();
if(one_per){
perms.push(one_per);
}
var rets = api.hasPermission({
list:perms
});
if(!one_per){
apialert('判断结果:' + JSON.stringify(rets));
return;
}
return rets;
}
/**
* 获取权限
* @param array one_per - 权限数组['camera','location']
* @param function callback - 回调函数
*/
u.reqPermission = function(one_per, callback) {
var perms = new Array();
if(one_per){
perms.push(one_per);
}
api.requestPermission({
list: perms,
code: 100001
}, function(ret, err) {
if(callback){
callback(ret);
return;
}
//获取处理结果
var list = ret.list;
for (var i in list) {
//只有有一项权限不足,就返回
if (list[i].granted == false) {
api.toast({
msg: '权限不足,无法进行下一步操作',
duration: 2000,
location: 'bottom'
});
return false;
}
}
});
}
/**
* 判断是否持有数组中的权限,无权限获取对应的权限
* @param array perm - 权限数组['camera','location']
* @param function callback - 回调函数
*/
u.confirmPer = function(perm, callback) {
var has = $api.hasPermission(perm);
console.log("has==>"+JSON.stringify(has));
if(!has || !has[0] || !has[0].granted){
api.confirm({
title: '提醒',
msg: '没有获得 ' + perm + " 权限\n是否前往设置?",
buttons: ['去设置', '取消']
}, function(ret, err) {
if(1 == ret.buttonIndex){
reqPermission(perm);
}
});
return false;
}
return true;
}
//权限用到的方法结束
//调用方法如下:
var falg = $api.confirmPer('camera'); |
|