123 lines
3.2 KiB
JavaScript
123 lines
3.2 KiB
JavaScript
import locationService from "./locationService";
|
||
/**
|
||
* 申请权限(App端专用)
|
||
* @param {string} permission - 要申请的权限
|
||
* @param {string} explain - 权限使用说明
|
||
* @returns {Promise<boolean>} - 返回是否授权成功
|
||
*/
|
||
function requestAppPermission(permission, explain) {
|
||
return new Promise((resolve) => {
|
||
// #ifdef APP-PLUS
|
||
if (plus.os.name === 'Android') {
|
||
// Android 平台
|
||
plus.android.requestPermissions(
|
||
[permission],
|
||
function(result) {
|
||
if(result.deniedPresent.includes(permission)) {
|
||
// 0:已授权,-1:未授权
|
||
resolve('deniedPresent');
|
||
}else if(result.deniedAlways.includes(permission)) {
|
||
// 0:已授权,-1:未授权
|
||
resolve('deniedAlways');
|
||
}else {
|
||
resolve(true);
|
||
}
|
||
},
|
||
explain || '需要此权限以提供完整服务'
|
||
);
|
||
} else {
|
||
console.log('iOS',plus.ios.requestPermissions)
|
||
// iOS 平台
|
||
plus.ios.requestPermissions(
|
||
// [permission],
|
||
['NSLocationAlwaysUsageDescription'],
|
||
function() {
|
||
console.log('iOS 没有直接的回调结果,需要检查状态')
|
||
// iOS 没有直接的回调结果,需要检查状态
|
||
checkPermissionStatus(permission).then(resolve);
|
||
},
|
||
explain || '需要此权限以提供完整服务'
|
||
);
|
||
}
|
||
// #endif
|
||
|
||
// #ifndef APP-PLUS
|
||
// 非App端直接返回true,由各平台自己的逻辑处理
|
||
resolve(true);
|
||
// #endif
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 检查权限状态(iOS专用)
|
||
*/
|
||
function checkPermissionStatus(permission) {
|
||
return new Promise((resolve) => {
|
||
if (plus.os.name === 'Android') {
|
||
// Android可以直接用requestPermissions的结果
|
||
resolve(true);
|
||
return;
|
||
}
|
||
console.log('[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]')
|
||
// iOS需要特殊处理
|
||
const authStatus = plus.ios.invoke('CLLocationManager', 'authorizationStatus');
|
||
// 0:未决定, 1:受限, 2:拒绝, 3:允许
|
||
resolve(authStatus === 3);
|
||
});
|
||
}
|
||
|
||
|
||
/**
|
||
* 统一权限请求方法
|
||
* @param {string} permission - 权限标识
|
||
* @param {string} explain - 说明文字
|
||
*/
|
||
export async function requestPermission(permission, explain) {
|
||
// #ifdef APP-PLUS
|
||
// App端处理
|
||
const type = await requestAppPermission(permission, explain);
|
||
console.log('type========',type)
|
||
if (type == 'deniedPresent') {
|
||
// uni.showModal({
|
||
// title: '权限申请',
|
||
// content: explain || '需要此权限以提供完整服务',
|
||
// confirmText: '去设置',
|
||
// success(res) {
|
||
// if (res.confirm) {
|
||
// // plus.runtime.openSettings();
|
||
// locationService.openAppSettings()
|
||
// }
|
||
// }
|
||
// });
|
||
uni.setStorageSync('locationDate',new Date());
|
||
return false;
|
||
}else if (type == 'deniedAlways') {
|
||
uni.setStorageSync('locationDate',new Date());
|
||
return false;
|
||
}else {
|
||
return true;
|
||
}
|
||
// return granted;
|
||
// #endif
|
||
|
||
// #ifdef MP-WEIXIN
|
||
// 微信小程序处理
|
||
return new Promise((resolve) => {
|
||
uni.authorize({
|
||
scope: permission,
|
||
success() { resolve(true); },
|
||
fail() { resolve(false); }
|
||
});
|
||
});
|
||
// #endif
|
||
|
||
// #ifdef H5
|
||
// H5处理(部分权限可用)
|
||
return Promise.resolve(true);
|
||
// #endif
|
||
}
|
||
|
||
|
||
export default {
|
||
requestPermission
|
||
} |