洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.4 KiB

4 months ago
  1. /// null = 未请求,1 = 已允许,0 = 拒绝|受限, 2 = 系统未开启
  2. var isIOS
  3. function location() {
  4. var result = 0;
  5. var cllocationManger = plus.ios.import("CLLocationManager");
  6. var enable = cllocationManger.locationServicesEnabled();
  7. var status = cllocationManger.authorizationStatus();
  8. if (!enable) {
  9. result = 2;
  10. } else if (status === 0) {
  11. result = null;
  12. } else if (status === 3 || status === 4) {
  13. result = 1;
  14. } else {
  15. result = 0;
  16. }
  17. plus.ios.deleteObject(cllocationManger);
  18. return result;
  19. }
  20. function requestIOS(permissionID) {
  21. return new Promise((resolve, reject) => {
  22. switch (permissionID) {
  23. case "location":
  24. resolve(location());
  25. break;
  26. default:
  27. resolve(0);
  28. break;
  29. }
  30. });
  31. }
  32. function requestAndroid(permissionID) {
  33. return new Promise((resolve, reject) => {
  34. plus.android.requestPermissions(
  35. [permissionID],
  36. function(resultObj) {
  37. var result = 0;
  38. for (var i = 0; i < resultObj.granted.length; i++) {
  39. var grantedPermission = resultObj.granted[i];
  40. console.log('已获取的权限:');
  41. result = 1
  42. }
  43. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  44. var deniedPresentPermission = resultObj.deniedPresent[i];
  45. console.log('拒绝本次申请的权限:');
  46. result = 0
  47. }
  48. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  49. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  50. console.log('永久拒绝申请的权限:');
  51. result = -1
  52. }
  53. resolve(result);
  54. },
  55. function(error) {
  56. resolve({
  57. code: error.code,
  58. message: error.message
  59. });
  60. }
  61. );
  62. });
  63. }
  64. function gotoAppPermissionSetting() {
  65. if (permission.isIOS) {
  66. var UIApplication = plus.ios.import("UIApplication");
  67. var application2 = UIApplication.sharedApplication();
  68. var NSURL2 = plus.ios.import("NSURL");
  69. var setting2 = NSURL2.URLWithString("app-settings:");
  70. application2.openURL(setting2);
  71. plus.ios.deleteObject(setting2);
  72. plus.ios.deleteObject(NSURL2);
  73. plus.ios.deleteObject(application2);
  74. } else {
  75. var Intent = plus.android.importClass("android.content.Intent");
  76. var Settings = plus.android.importClass("android.provider.Settings");
  77. var Uri = plus.android.importClass("android.net.Uri");
  78. var mainActivity = plus.android.runtimeMainActivity();
  79. var intent = new Intent();
  80. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  81. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  82. intent.setData(uri);
  83. mainActivity.startActivity(intent);
  84. }
  85. }
  86. const permission = {
  87. get isIOS(){
  88. return typeof isIOS === 'boolean' ? isIOS : (isIOS = uni.getSystemInfoSync().platform === 'ios')
  89. },
  90. requestIOS: requestIOS,
  91. requestAndroid: requestAndroid,
  92. gotoAppSetting: gotoAppPermissionSetting
  93. }
  94. export default permission