permission.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
  3. */
  4. var isIos
  5. // #ifdef APP-PLUS
  6. isIos = (plus.os.name == "iOS")
  7. // #endif
  8. // 判断推送权限是否开启
  9. function judgeIosPermissionPush() {
  10. var result = false;
  11. var UIApplication = plus.ios.import("UIApplication");
  12. var app = UIApplication.sharedApplication();
  13. var enabledTypes = 0;
  14. if (app.currentUserNotificationSettings) {
  15. var settings = app.currentUserNotificationSettings();
  16. enabledTypes = settings.plusGetAttribute("types");
  17. console.log("enabledTypes1:" + enabledTypes);
  18. if (enabledTypes == 0) {
  19. console.log("推送权限没有开启");
  20. } else {
  21. result = true;
  22. console.log("已经开启推送功能!")
  23. }
  24. plus.ios.deleteObject(settings);
  25. } else {
  26. enabledTypes = app.enabledRemoteNotificationTypes();
  27. if (enabledTypes == 0) {
  28. console.log("推送权限没有开启!");
  29. } else {
  30. result = true;
  31. console.log("已经开启推送功能!")
  32. }
  33. console.log("enabledTypes2:" + enabledTypes);
  34. }
  35. plus.ios.deleteObject(app);
  36. plus.ios.deleteObject(UIApplication);
  37. return result;
  38. }
  39. // 判断定位权限是否开启
  40. function judgeIosPermissionLocation() {
  41. var result = false;
  42. var cllocationManger = plus.ios.import("CLLocationManager");
  43. var status = cllocationManger.authorizationStatus();
  44. result = (status != 2)
  45. console.log("定位权限开启:" + result);
  46. // 以下代码判断了手机设备的定位是否关闭,推荐另行使用方法 checkSystemEnableLocation
  47. /* var enable = cllocationManger.locationServicesEnabled();
  48. var status = cllocationManger.authorizationStatus();
  49. console.log("enable:" + enable);
  50. console.log("status:" + status);
  51. if (enable && status != 2) {
  52. result = true;
  53. console.log("手机定位服务已开启且已授予定位权限");
  54. } else {
  55. console.log("手机系统的定位没有打开或未给予定位权限");
  56. } */
  57. plus.ios.deleteObject(cllocationManger);
  58. return result;
  59. }
  60. // 判断麦克风权限是否开启
  61. function judgeIosPermissionRecord() {
  62. var result = false;
  63. var avaudiosession = plus.ios.import("AVAudioSession");
  64. var avaudio = avaudiosession.sharedInstance();
  65. var permissionStatus = avaudio.recordPermission();
  66. console.log("permissionStatus:" + permissionStatus);
  67. if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
  68. console.log("麦克风权限没有开启");
  69. } else {
  70. result = true;
  71. console.log("麦克风权限已经开启");
  72. }
  73. plus.ios.deleteObject(avaudiosession);
  74. return result;
  75. }
  76. // 判断相机权限是否开启
  77. function judgeIosPermissionCamera() {
  78. var result = false;
  79. var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
  80. var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
  81. console.log("authStatus:" + authStatus);
  82. if (authStatus == 3) {
  83. result = true;
  84. console.log("相机权限已经开启");
  85. } else {
  86. console.log("相机权限没有开启");
  87. }
  88. plus.ios.deleteObject(AVCaptureDevice);
  89. return result;
  90. }
  91. // 判断相册权限是否开启
  92. function judgeIosPermissionPhotoLibrary() {
  93. var result = false;
  94. var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
  95. var authStatus = PHPhotoLibrary.authorizationStatus();
  96. console.log("authStatus:" + authStatus);
  97. if (authStatus == 3) {
  98. result = true;
  99. console.log("相册权限已经开启");
  100. } else {
  101. console.log("相册权限没有开启");
  102. }
  103. plus.ios.deleteObject(PHPhotoLibrary);
  104. return result;
  105. }
  106. // 判断通讯录权限是否开启
  107. function judgeIosPermissionContact() {
  108. var result = false;
  109. var CNContactStore = plus.ios.import("CNContactStore");
  110. var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
  111. if (cnAuthStatus == 3) {
  112. result = true;
  113. console.log("通讯录权限已经开启");
  114. } else {
  115. console.log("通讯录权限没有开启");
  116. }
  117. plus.ios.deleteObject(CNContactStore);
  118. return result;
  119. }
  120. // 判断日历权限是否开启
  121. function judgeIosPermissionCalendar() {
  122. var result = false;
  123. var EKEventStore = plus.ios.import("EKEventStore");
  124. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
  125. if (ekAuthStatus == 3) {
  126. result = true;
  127. console.log("日历权限已经开启");
  128. } else {
  129. console.log("日历权限没有开启");
  130. }
  131. plus.ios.deleteObject(EKEventStore);
  132. return result;
  133. }
  134. // 判断备忘录权限是否开启
  135. function judgeIosPermissionMemo() {
  136. var result = false;
  137. var EKEventStore = plus.ios.import("EKEventStore");
  138. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
  139. if (ekAuthStatus == 3) {
  140. result = true;
  141. console.log("备忘录权限已经开启");
  142. } else {
  143. console.log("备忘录权限没有开启");
  144. }
  145. plus.ios.deleteObject(EKEventStore);
  146. return result;
  147. }
  148. // Android权限查询
  149. function requestAndroidPermission(permissionID) {
  150. return new Promise((resolve, reject) => {
  151. plus.android.requestPermissions(
  152. [permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  153. function(resultObj) {
  154. var result = 0;
  155. for (var i = 0; i < resultObj.granted.length; i++) {
  156. var grantedPermission = resultObj.granted[i];
  157. console.log('已获取的权限:' + grantedPermission);
  158. result = 1
  159. }
  160. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  161. var deniedPresentPermission = resultObj.deniedPresent[i];
  162. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  163. result = 0
  164. }
  165. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  166. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  167. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  168. result = -1
  169. }
  170. resolve(result);
  171. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  172. // if (result != 1) {
  173. // gotoAppPermissionSetting()
  174. // }
  175. },
  176. function(error) {
  177. console.log('申请权限错误:' + error.code + " = " + error.message);
  178. resolve({
  179. code: error.code,
  180. message: error.message
  181. });
  182. }
  183. );
  184. });
  185. }
  186. // 使用一个方法,根据参数判断权限
  187. function judgeIosPermission(permissionID) {
  188. if (permissionID == "location") {
  189. return judgeIosPermissionLocation()
  190. } else if (permissionID == "camera") {
  191. return judgeIosPermissionCamera()
  192. } else if (permissionID == "photoLibrary") {
  193. return judgeIosPermissionPhotoLibrary()
  194. } else if (permissionID == "record") {
  195. return judgeIosPermissionRecord()
  196. } else if (permissionID == "push") {
  197. return judgeIosPermissionPush()
  198. } else if (permissionID == "contact") {
  199. return judgeIosPermissionContact()
  200. } else if (permissionID == "calendar") {
  201. return judgeIosPermissionCalendar()
  202. } else if (permissionID == "memo") {
  203. return judgeIosPermissionMemo()
  204. }
  205. return false;
  206. }
  207. // 跳转到**应用**的权限页面
  208. function gotoAppPermissionSetting() {
  209. if (isIos) {
  210. var UIApplication = plus.ios.import("UIApplication");
  211. var application2 = UIApplication.sharedApplication();
  212. var NSURL2 = plus.ios.import("NSURL");
  213. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  214. var setting2 = NSURL2.URLWithString("app-settings:");
  215. application2.openURL(setting2);
  216. plus.ios.deleteObject(setting2);
  217. plus.ios.deleteObject(NSURL2);
  218. plus.ios.deleteObject(application2);
  219. } else {
  220. // console.log(plus.device.vendor);
  221. var Intent = plus.android.importClass("android.content.Intent");
  222. var Settings = plus.android.importClass("android.provider.Settings");
  223. var Uri = plus.android.importClass("android.net.Uri");
  224. var mainActivity = plus.android.runtimeMainActivity();
  225. var intent = new Intent();
  226. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  227. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  228. intent.setData(uri);
  229. mainActivity.startActivity(intent);
  230. }
  231. }
  232. // 检查系统的设备服务是否开启
  233. // var checkSystemEnableLocation = async function () {
  234. function checkSystemEnableLocation() {
  235. if (isIos) {
  236. var result = false;
  237. var cllocationManger = plus.ios.import("CLLocationManager");
  238. var result = cllocationManger.locationServicesEnabled();
  239. console.log("系统定位开启:" + result);
  240. plus.ios.deleteObject(cllocationManger);
  241. return result;
  242. } else {
  243. var context = plus.android.importClass("android.content.Context");
  244. var locationManager = plus.android.importClass("android.location.LocationManager");
  245. var main = plus.android.runtimeMainActivity();
  246. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  247. var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
  248. console.log("系统定位开启:" + result);
  249. return result
  250. }
  251. }
  252. module.exports = {
  253. judgeIosPermission: judgeIosPermission,
  254. requestAndroidPermission: requestAndroidPermission,
  255. checkSystemEnableLocation: checkSystemEnableLocation,
  256. gotoAppPermissionSetting: gotoAppPermissionSetting
  257. }