permission.js 11 KB

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