app.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
  3. * @description 安卓相关权限,需要先在manifest.json中勾选
  4. * @param {String} scopeValue - 权限种类
  5. *
  6. */
  7. // 只写了当前项目用到的权限,其他用到的自己加下
  8. const PERMISSION_MAP = {
  9. 'userLocation': {
  10. tip: '地理位置',
  11. androidPermissionID: ['android.permission.ACCESS_FINE_LOCATION'],
  12. },
  13. 'writePhotosAlbum': {
  14. tip: '保存到相册',
  15. androidPermissionID: ['android.permission.WRITE_EXTERNAL_STORAGE'],
  16. },
  17. 'camera': {
  18. tip: '摄像头',
  19. androidPermissionID: ['android.permission.CAMERA'],
  20. }
  21. }
  22. // 判断平台
  23. let isIos = (plus.os.name == "iOS")
  24. export default class AppAuth {
  25. constructor(scopeValue) {
  26. this.scopeValue = scopeValue
  27. }
  28. //权限检测
  29. async checkAuth() {
  30. const that = this
  31. let result = false
  32. switch (this.scopeValue) {
  33. case 'userLocation': //定位权限
  34. result = isIos ? that.iosLocationAuth : await that.androidAuth()
  35. break;
  36. case 'writePhotosAlbum': //相册权限
  37. result = isIos ? that.iosPhotoAuth : await that.androidAuth()
  38. break;
  39. case 'camera': //摄像头权限
  40. result = isIos ? that.iosCameraAuth : await that.androidAuth()
  41. break;
  42. default:
  43. result = false
  44. break;
  45. }
  46. return Promise.resolve(result)
  47. }
  48. // Android权限查询
  49. androidAuth() {
  50. const that = this;
  51. let permissionID = PERMISSION_MAP[this.scopeValue].androidPermissionID;
  52. return new Promise((resolve, reject) => {
  53. plus.android.requestPermissions(
  54. [...permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  55. function(resultObj) {
  56. let result = 0;
  57. for (var i = 0; i < resultObj.granted.length; i++) {
  58. var grantedPermission = resultObj.granted[i];
  59. console.log('已获取的权限:' + grantedPermission);
  60. result = 1
  61. }
  62. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  63. var deniedPresentPermission = resultObj.deniedPresent[i];
  64. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  65. result = 0
  66. }
  67. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  68. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  69. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  70. result = -1
  71. }
  72. resolve(result);
  73. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  74. if (result !== 1) {
  75. that.gotoAppSetting()
  76. }
  77. },
  78. function(error) {
  79. console.log('申请权限错误:' + error.code + " = " + error.message);
  80. resolve({
  81. code: error.code,
  82. message: error.message
  83. });
  84. }
  85. );
  86. });
  87. }
  88. // 跳转到**应用**的权限页面
  89. gotoAppSetting() {
  90. uni.showModal({
  91. title: '设置权限',
  92. content: `当前功能需要${PERMISSION_MAP[this.scopeValue].tip}权限,是否去设置?`,
  93. confirmText: '设置',
  94. success: res => {
  95. if (res.confirm) {
  96. if (isIos) {
  97. let UIApplication = plus.ios.import("UIApplication");
  98. let application2 = UIApplication.sharedApplication();
  99. let NSURL2 = plus.ios.import("NSURL");
  100. let setting2 = NSURL2.URLWithString("app-settings:");
  101. application2.openURL(setting2);
  102. plus.ios.deleteObject(setting2);
  103. plus.ios.deleteObject(NSURL2);
  104. plus.ios.deleteObject(application2);
  105. } else {
  106. let Intent = plus.android.importClass("android.content.Intent");
  107. let Settings = plus.android.importClass("android.provider.Settings");
  108. let Uri = plus.android.importClass("android.net.Uri");
  109. let mainActivity = plus.android.runtimeMainActivity();
  110. let intent = new Intent();
  111. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  112. let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  113. intent.setData(uri);
  114. mainActivity.startActivity(intent);
  115. }
  116. }
  117. },
  118. fail: err => {
  119. console.log(`%cuni.showModal失败:`, 'color:green;background:yellow');
  120. }
  121. })
  122. }
  123. // 定位是否开启
  124. iosLocationAuth() {
  125. let result = 0;
  126. let cllocationManger = plus.ios.import("CLLocationManager");
  127. let enable = cllocationManger.locationServicesEnabled();
  128. let status = cllocationManger.authorizationStatus();
  129. if (!enable) {
  130. result = 2;
  131. } else if (status === 0) {
  132. result = null;
  133. } else if (status === 3 || status === 4) {
  134. result = 1;
  135. } else {
  136. result = 0;
  137. }
  138. plus.ios.deleteObject(cllocationManger);
  139. return result;
  140. }
  141. // 判断推送权限是否开启
  142. iosPushAuth() {
  143. let result = false;
  144. let UIApplication = plus.ios.import("UIApplication");
  145. let app = UIApplication.sharedApplication();
  146. let enabledTypes = 0;
  147. if (app.currentUserNotificationSettings) {
  148. let settings = app.currentUserNotificationSettings();
  149. enabledTypes = settings.plusGetAttribute("types");
  150. console.log("enabledTypes1:" + enabledTypes);
  151. if (enabledTypes == 0) {
  152. console.log("推送权限没有开启");
  153. } else {
  154. result = true;
  155. console.log("已经开启推送功能!")
  156. }
  157. plus.ios.deleteObject(settings);
  158. } else {
  159. enabledTypes = app.enabledRemoteNotificationTypes();
  160. if (enabledTypes == 0) {
  161. console.log("推送权限没有开启!");
  162. } else {
  163. result = true;
  164. console.log("已经开启推送功能!")
  165. }
  166. console.log("enabledTypes2:" + enabledTypes);
  167. }
  168. plus.ios.deleteObject(app);
  169. plus.ios.deleteObject(UIApplication);
  170. return result;
  171. }
  172. // 判断麦克风权限是否开启
  173. iosRecordAuth() {
  174. let result = false;
  175. let avaudiosession = plus.ios.import("AVAudioSession");
  176. let avaudio = avaudiosession.sharedInstance();
  177. let permissionStatus = avaudio.recordPermission();
  178. console.log("permissionStatus:" + permissionStatus);
  179. if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
  180. console.log("麦克风权限没有开启");
  181. } else {
  182. result = true;
  183. console.log("麦克风权限已经开启");
  184. }
  185. plus.ios.deleteObject(avaudiosession);
  186. return result;
  187. }
  188. // 判断相机权限是否开启
  189. iosCameraAuth() {
  190. let result = false;
  191. let AVCaptureDevice = plus.ios.import("AVCaptureDevice");
  192. let authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
  193. console.log("authStatus:" + authStatus);
  194. if (authStatus == 3) {
  195. result = true;
  196. console.log("相机权限已经开启");
  197. } else {
  198. console.log("相机权限没有开启");
  199. }
  200. plus.ios.deleteObject(AVCaptureDevice);
  201. return result;
  202. }
  203. // 判断相册权限是否开启
  204. iosPhotoAuth() {
  205. let result = false;
  206. let PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
  207. let authStatus = PHPhotoLibrary.authorizationStatus();
  208. console.log("authStatus:" + authStatus);
  209. if (authStatus == 3) {
  210. result = true;
  211. console.log("相册权限已经开启");
  212. } else {
  213. console.log("相册权限没有开启");
  214. }
  215. plus.ios.deleteObject(PHPhotoLibrary);
  216. return result;
  217. }
  218. // 判断通讯录权限是否开启
  219. iosContactAuth() {
  220. let result = false;
  221. let CNContactStore = plus.ios.import("CNContactStore");
  222. let cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
  223. if (cnAuthStatus == 3) {
  224. result = true;
  225. console.log("通讯录权限已经开启");
  226. } else {
  227. console.log("通讯录权限没有开启");
  228. }
  229. plus.ios.deleteObject(CNContactStore);
  230. return result;
  231. }
  232. // 判断日历权限是否开启
  233. iosCalendarAuth() {
  234. let result = false;
  235. let EKEventStore = plus.ios.import("EKEventStore");
  236. let ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
  237. if (ekAuthStatus == 3) {
  238. result = true;
  239. console.log("日历权限已经开启");
  240. } else {
  241. console.log("日历权限没有开启");
  242. }
  243. plus.ios.deleteObject(EKEventStore);
  244. return result;
  245. }
  246. // 判断备忘录权限是否开启
  247. iosMemoAuth() {
  248. let result = false;
  249. let EKEventStore = plus.ios.import("EKEventStore");
  250. let ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
  251. if (ekAuthStatus == 3) {
  252. result = true;
  253. console.log("备忘录权限已经开启");
  254. } else {
  255. console.log("备忘录权限没有开启");
  256. }
  257. plus.ios.deleteObject(EKEventStore);
  258. return result;
  259. }
  260. }