|
|
@@ -4,6 +4,9 @@
|
|
|
* @date 2019.8.1
|
|
|
*/
|
|
|
import configdata from './config'
|
|
|
+// #ifdef APP-PLUS
|
|
|
+import permision from "../js_sdk/wa-permission/permission.js"
|
|
|
+// #endif
|
|
|
module.exports = {
|
|
|
//微信的appId
|
|
|
getWxAppid() {
|
|
|
@@ -466,69 +469,104 @@ module.exports = {
|
|
|
},
|
|
|
//APP获取手机权限鉴权
|
|
|
// APP获取手机权限鉴权(优化版,兼容全权限+多系统)
|
|
|
- checkPermission(permission, tip) {
|
|
|
- return new Promise((resolve) => {
|
|
|
- const permisionObj = uni.getAppAuthorizeSetting && uni.getAppAuthorizeSetting();
|
|
|
- if (!permisionObj || !permisionObj.getSetting) {
|
|
|
- console.log('当前环境不支持 getAppAuthorizeSetting');
|
|
|
- resolve(true); // 默认放行
|
|
|
- return;
|
|
|
+
|
|
|
+ async checkPermission(permission, tip, that) {
|
|
|
+ // 权限映射配置
|
|
|
+ let permisionID = '';
|
|
|
+ let permisionIosID = '';
|
|
|
+
|
|
|
+ if (permission == 'location') {
|
|
|
+ permisionID = 'android.permission.ACCESS_FINE_LOCATION';
|
|
|
+ permisionIosID = 'location';
|
|
|
+ }
|
|
|
+ if (permission == 'camera') {
|
|
|
+ permisionID = 'android.permission.CAMERA,android.permission.READ_EXTERNAL_STORAGE';
|
|
|
+ permisionIosID = 'camera,photoLibrary';
|
|
|
+ }
|
|
|
+ if (permission == 'microphone') {
|
|
|
+ permisionID = 'android.permission.RECORD_AUDIO';
|
|
|
+ permisionIosID = 'record';
|
|
|
}
|
|
|
|
|
|
- const system = uni.getSystemInfoSync().platform;
|
|
|
- let realScope = permission;
|
|
|
- const scopeMap = {
|
|
|
- 'microphone': 'microphone',
|
|
|
- 'camera': 'camera',
|
|
|
- 'location': 'scope.userLocation',
|
|
|
- 'albumRead': system === 'ios' ? 'photos' : 'storage',
|
|
|
- 'albumWrite': system === 'ios' ? 'writePhotosAlbum' : 'storage',
|
|
|
- };
|
|
|
- if (permission === 'album') realScope = scopeMap.albumRead;
|
|
|
- else if (permission === 'albumWrite') realScope = scopeMap.albumWrite;
|
|
|
- else if (scopeMap[permission]) realScope = scopeMap[permission];
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ // #ifdef H5
|
|
|
+ resolve(true);
|
|
|
+ return;
|
|
|
+ // #endif
|
|
|
|
|
|
- console.log('准备检测权限:', realScope);
|
|
|
+ // 特殊处理:相机权限在非iOS平台显示自定义弹窗
|
|
|
+ if (permisionID == 'android.permission.CAMERA,android.permission.READ_EXTERNAL_STORAGE' && uni.getSystemInfoSync().platform != 'ios') {
|
|
|
+ that.$refs.permission.open();
|
|
|
+ }
|
|
|
|
|
|
- permisionObj.getSetting({
|
|
|
- success: (res) => {
|
|
|
- console.log('权限状态', res.authSetting);
|
|
|
- const authStatus = res.authSetting[realScope];
|
|
|
- if (authStatus === true) {
|
|
|
- resolve(true);
|
|
|
- } else if (authStatus === false) {
|
|
|
- uni.showModal({
|
|
|
- title: '权限不足',
|
|
|
- content: `${tip},请前往设置开启权限`,
|
|
|
- confirmText: '去设置',
|
|
|
- cancelText: '取消',
|
|
|
- success: (modalRes) => {
|
|
|
- if (modalRes.confirm) {
|
|
|
- permisionObj.openAppAuthorizeSetting({
|
|
|
- success: () => resolve(false),
|
|
|
- fail: () => resolve(false)
|
|
|
- });
|
|
|
- } else {
|
|
|
+ // Android 平台处理
|
|
|
+ if (uni.getSystemInfoSync().platform === 'android') {
|
|
|
+ if (permisionID == '') {
|
|
|
+ resolve(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await permision.requestAndroidPermission(permisionID);
|
|
|
+ console.log(result);
|
|
|
+
|
|
|
+ if (result == 1) {
|
|
|
+ console.log("已获得授权");
|
|
|
+ that.$refs.permission?.close();
|
|
|
+ resolve(true);
|
|
|
+ } else if (result == 0) {
|
|
|
+ console.log("未获得授权");
|
|
|
+ that.$refs.permission?.open();
|
|
|
+ resolve(false);
|
|
|
+ } else {
|
|
|
+ console.log("被永久拒绝");
|
|
|
+ if (uni.getSystemInfoSync().platform != 'ios') {
|
|
|
+ that.$refs.permission?.open();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 显示确认对话框
|
|
|
+ appConfirm("权限被永久拒绝," + tip, function(res) {
|
|
|
+ if (res) {
|
|
|
+ uni.openAppAuthorizeSetting();
|
|
|
+ }
|
|
|
+ resolve(false);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error("权限请求失败:", error);
|
|
|
resolve(false);
|
|
|
- }
|
|
|
}
|
|
|
- });
|
|
|
- } else {
|
|
|
- permisionObj.authorize({
|
|
|
- scope: realScope,
|
|
|
- success: () => resolve(true),
|
|
|
- fail: (err) => {
|
|
|
- console.log('权限授权失败', err);
|
|
|
- resolve(false);
|
|
|
+ }
|
|
|
+ // iOS 平台处理
|
|
|
+ else {
|
|
|
+ if (permisionIosID == '') {
|
|
|
+ resolve(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const iosPermissions = permisionIosID.split(',');
|
|
|
+ let allGranted = true;
|
|
|
+
|
|
|
+ for (let i = 0; i < iosPermissions.length; i++) {
|
|
|
+ const currentPermission = iosPermissions[i];
|
|
|
+ const result = permision.judgeIosPermission(currentPermission);
|
|
|
+ console.log(`iOS权限 ${currentPermission}:`, result);
|
|
|
+
|
|
|
+ if (!result) {
|
|
|
+ allGranted = false;
|
|
|
+ console.log(`未获得 ${currentPermission} 授权`);
|
|
|
+
|
|
|
+ // 特殊处理相机和相册权限
|
|
|
+ if (currentPermission == 'camera' || currentPermission == 'photoLibrary') {
|
|
|
+ that.$refs.camera?.open();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.log(`已获得 ${currentPermission} 授权`);
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
+
|
|
|
+ resolve(allGranted);
|
|
|
}
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- console.log('getSetting失败', err);
|
|
|
- resolve(false);
|
|
|
- }
|
|
|
});
|
|
|
- });
|
|
|
}
|
|
|
};
|