Huanyuyuehui/utils/prompt.js

300 lines
9.0 KiB
JavaScript

export default {
// 处理应用程序权限请求
// #ifdef APP-PLUS
isIos: plus.os.name == "iOS",
os:uni.getSystemInfoSync().osName,
// #endif
mapping: {
'CAMERA': {
title: "对摄像头/存储空间/照片权限申请说明",
content: "摄像头权限将用于拍摄照片和视频,照片/图片用于更换头像场景中读取和写入相册和文件内容",
methods: 'SET_CAMERA'
},
'WRITE_EXTERNAL_STORAGE': {
title: "对存储空间/照片权限申请说明",
content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像场景中读取和写入相册和文件内容。",
methods: 'SET_WRITE_EXTERNAL_STORAGE'
},
'ACCESS_FINE_LOCATION': {
title: "铸源商城对地理位置权限申请说明",
content: "铸源商城应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。",
methods: 'SET_ACCESS_FINE_LOCATION'
},
'CALL_PHONE': {
title: "铸源商城拨打/管理电话权限申请说明",
content: "便于您使用该功能获取手机验证码场景下使用",
methods: 'SET_CALL_PHONE'
}
},
// ios权限是否开启
iosPermissionCheck(permissionID) {
// ios权限列表
let iosPermissions = {
camera: 'iosCamera', // 摄像头
location: 'iosLocation', // 位置
storage: 'iosAlbum', // 相册
}
return new Promise((resolve, reject) => {
let res = iosPermissions[permissionID]();
resolve(res);
});
},
// ios定位权限
iosLocation() {
let result = 0,
cLLocationManager = plus.ios.importClass("CLLocationManager"),
authStatus = cLLocationManager.authorizationStatus(),
enable = cLLocationManager.locationServicesEnabled();
if (enable && authStatus != 2) {
result = 1;
} else {
result = 0;
}
plus.ios.deleteObject(cLLocationManager);
return result;
},
// 安卓权限是否开启
androidPermission(permissionID){
// android权限列表
let androidPermissions = {
camera: "android.permission.CAMERA", // 摄像头权限
location: "android.permission.ACCESS_FINE_LOCATION", // 位置权限
storage: "android.permission.WRITE_EXTERNAL_STORAGE" // 存储写入权限
}
console.log(androidPermissions[permissionID]);
return new Promise((resolve, reject) => {
let result = 0
let compat = plus.android.importClass('androidx.core.content.ContextCompat')
let context = plus.android.runtimeMainActivity()
let results = compat.checkSelfPermission(context, androidPermissions[permissionID])
if (results != -1) {
console.log(results);
result = 1;
} else {
result = 0;
}
resolve(result)
})
},
permission(val){
let permissionCheck = {
android: this.androidPermission,
ios: this.iosPermissionCheck,
}
return new Promise(async(resolve, reject)=>{
let res = await permissionCheck[this.os](val)
resolve(res)
})
},
// 检查系统的设备服务是否开启
checkSystemEnableLocation() {
if (this.isIos) {
var result = false;
var cllocationManger = plus.ios.import("CLLocationManager");
var result = cllocationManger.locationServicesEnabled();
console.log("系统定位开启:" + result);
plus.ios.deleteObject(cllocationManger);
return result;
} else {
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var main = plus.android.runtimeMainActivity();
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
console.log("系统定位开启:" + result);
return result
}
},
// 获取权限
async requestPermissions(permissionID) {
let results;
if(!this.isIos){
results = permissionID.every((item)=>{
let compat = plus.android.importClass('androidx.core.content.ContextCompat')
let context = plus.android.runtimeMainActivity()
let results = compat.checkSelfPermission(context, 'android.permission.' + item)
return results!=0
})
}
// console.log(permissionID);
// console.log(results);
if (results && !this.isIos) {
var viewObj = await this.nativeObjView(permissionID);
viewObj.show();
}
// console.log('android.permission.' + permissionID, '当前手机权限');
return new Promise(async (resolve, reject) => {
//苹果
if (this.isIos) {
resolve(1);
return
}
// Android权限查询
function requestAndroidPermission(permissionID_) {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(permissionID_,
function(resultObj) {
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
result = -1
}
resolve(result);
},
function(error) {
console.log('申请权限错误:' + error.code + " = " + error
.message);
resolve({
code: error.code,
message: error.message
});
}
);
});
}
let permissionList = permissionID.map((item)=>{
return 'android.permission.' + item
})
const result = await requestAndroidPermission(permissionList);
console.log(result);
if (viewObj) viewObj.close()
if (result === 1) {
//'已获得授权'
} else if (result === 0) {
//'未获得授权'
} else {
uni.showModal({
title: '提示',
content: '操作权限已被拒绝,请手动前往设置',
confirmText: "立即设置",
success: (res) => {
if (res.confirm) {
this.gotoAppPermissionSetting()
}
}
})
}
resolve(result);
});
},
//提示框
nativeObjView(permissionID) {
return new Promise((resolve, reject) => {
const systemInfo = uni.getSystemInfoSync();
const statusBarHeight = systemInfo.statusBarHeight;
// const navigationBarHeight = systemInfo.platform === 'android' ? 48 :
// 44;
// const totalHeight = statusBarHeight + navigationBarHeight;
const totalHeight = statusBarHeight;
let view = new plus.nativeObj.View('per-modal', {
top: '0px',
left: '0px',
width: '100%',
backgroundColor: '#444',
//opacity: .5;
})
view.drawRect({
color: '#fff',
radius: '8px'
}, {
top: totalHeight + 'px',
left: '5%',
width: '90%',
height: "100px",
})
view.drawText(this.mapping[permissionID[0]].title, {
top: totalHeight + 5 + 'px',
left: "8%",
height: "30px"
}, {
align: "left",
color: "#000",
}, {
onClick: function(e) {
console.log(e);
}
})
view.drawText(this.mapping[permissionID[0]].content, {
top: totalHeight + 35 + 'px',
height: "60px",
left: "8%",
width: "84%"
}, {
whiteSpace: 'normal',
size: "14px",
align: "left",
color: "#656563"
})
function show() {
view = plus.nativeObj.View.getViewById('per-modal');
view.show()
view = null //展示的时候也得清空,不然影响下次的关闭
}
function close() {
view = plus.nativeObj.View.getViewById('per-modal');
view.close();
view = null
}
resolve({
show,
close
})
})
},
// 打开系统服务
gotoLocationServer(){
if (this.isIos) {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}else{
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings');
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
var main = plus.android.runtimeMainActivity();
main.startActivity(intent);
}
},
// 跳转到**应用**的权限页面
gotoAppPermissionSetting() {
if (this.isIos) {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
} else {
// console.log(plus.device.vendor);
var Intent = plus.android.importClass("android.content.Intent");
var Settings = plus.android.importClass("android.provider.Settings");
var Uri = plus.android.importClass("android.net.Uri");
var mainActivity = plus.android.runtimeMainActivity();
var intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
mainActivity.startActivity(intent);
}
}
}