permission.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. * 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
  3. * https://ext.dcloud.net.cn/plugin?id=594
  4. */
  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. //获取ios手机权限
  254. // 判断位置权限是否始终开启
  255. function judgeIosPermissionLocationAlways() {
  256. var result = false;
  257. var cllocationManger = plus.ios.import("CLLocationManager");
  258. var status = cllocationManger.authorizationStatus();
  259. var enable = cllocationManger.locationServicesEnabled();
  260. if (status == 3 && enable) {
  261. result = true;
  262. console.log("位置权限始终开启");
  263. } else {
  264. console.log("位置权限不是始终开启");
  265. }
  266. plus.ios.deleteObject(cllocationManger);
  267. return result;
  268. }
  269. // 判断是否开启精准位置权限
  270. function judgeIosPermissionLocationAccuracy() {
  271. var result = false;
  272. var cllocationManger = plus.ios.import("CLLocationManager");
  273. var status = cllocationManger.authorizationStatus();
  274. if (status == 3 || status == 4) {
  275. result = true;
  276. console.log("精准位置权限已开启");
  277. } else {
  278. console.log("精准位置权限未开启");
  279. }
  280. plus.ios.deleteObject(cllocationManger);
  281. return result;
  282. }
  283. //获取安卓手机权限
  284. function requestAndroidPermissions(type) {
  285. let permissionArr=[]
  286. if(type=='ios'){
  287. // const awalysLocation=judgeIosPermissionLocationAlways()
  288. // const preciseLocation=judgeIosPermissionLocationAccuracy()
  289. const awalysLocation=true
  290. const preciseLocation=true
  291. permissionArr=[
  292. {type: 'awalysLocation',state:awalysLocation,title: ('使用App期间'), desc: '允许访问位置信息'},
  293. {type: 'preciseLocation',state:preciseLocation,title: ('精准位置'), desc: '开启精准位置开关'},
  294. ]
  295. }else if(type=='android'){
  296. const locationPermission=checkLocationPermission()
  297. const optimizationStatus=checkBatteryOptimizationStatus()
  298. permissionArr=[
  299. {type: 'location',state:locationPermission,title: ('位置权限'), desc: '打开手机定位,并运行APP始终使用'},
  300. {type: 'battery',state:optimizationStatus,title: ('电池优化'), desc: '打开电池优化设置, 将弗兰克APP加入保护名单'},
  301. ]
  302. }
  303. return permissionArr
  304. }
  305. function checkLocationPermission() {
  306. var main = plus.android.runtimeMainActivity();
  307. var PackageManager = plus.android.importClass("android.content.pm.PackageManager");
  308. // 定义定位权限常量
  309. var FINE_PERMISSION = "android.permission.ACCESS_FINE_LOCATION";
  310. var COARSE_PERMISSION = "android.permission.ACCESS_COARSE_LOCATION";
  311. // 检查精确和粗略定位权限状态
  312. var hasFineLocation = main.checkSelfPermission(FINE_PERMISSION) === PackageManager.PERMISSION_GRANTED;
  313. var hasCoarseLocation = main.checkSelfPermission(COARSE_PERMISSION) === PackageManager.PERMISSION_GRANTED;
  314. if (hasFineLocation || hasCoarseLocation) {
  315. console.log("✅ 定位权限已授予");
  316. return true;
  317. } else {
  318. console.log("⚠️ 定位权限未授予");
  319. return false;
  320. }
  321. }
  322. function checkBatteryOptimizationStatus() {
  323. var main = plus.android.runtimeMainActivity();
  324. var Context = plus.android.importClass("android.content.Context");
  325. var PowerManager = plus.android.importClass("android.os.PowerManager");
  326. var powerManager = main.getSystemService(Context.POWER_SERVICE);
  327. var packageName = main.getPackageName();
  328. var isIgnoringBatteryOptimizations = powerManager.isIgnoringBatteryOptimizations(packageName);
  329. if (isIgnoringBatteryOptimizations) {
  330. console.log("✅ 应用已被白名单豁免,未受电池优化限制");
  331. return true;
  332. } else {
  333. console.log("⚠️ 应用受电池优化限制");
  334. return false;
  335. }
  336. }
  337. function openBatteryOptimization() {
  338. var Intent = plus.android.importClass("android.content.Intent");
  339. var Settings = plus.android.importClass("android.provider.Settings");
  340. var Uri = plus.android.importClass("android.net.Uri");
  341. var main = plus.android.runtimeMainActivity();
  342. var intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
  343. var uri = Uri.parse("package:" + main.getPackageName());
  344. intent.setData(uri);
  345. main.startActivity(intent);
  346. }
  347. function checkBackgroundRunningPermission() {
  348. var main = plus.android.runtimeMainActivity();
  349. var Context = plus.android.importClass("android.content.Context");
  350. var ActivityManager = plus.android.importClass("android.app.ActivityManager");
  351. var activityManager = main.getSystemService(Context.ACTIVITY_SERVICE);
  352. var runningAppProcesses = activityManager.getRunningAppProcesses();
  353. console.log(runningAppProcesses,'runningAppProcesses');
  354. var packageName = main.getPackageName();
  355. if('size' in runningAppProcesses){
  356. for (var i = 0; i < runningAppProcesses.size(); i++) {
  357. var processInfo = runningAppProcesses.get(i);
  358. if (processInfo.processName === packageName) {
  359. console.log("✅ 应用正在后台运行");
  360. return true;
  361. }
  362. }
  363. }else{
  364. console.log("⚠️ 应用可能未开启后台运行权限");
  365. return false;
  366. }
  367. }
  368. function checkAppLockStatus() {
  369. var main = plus.android.runtimeMainActivity();
  370. var Context = plus.android.importClass("android.content.Context");
  371. var AppOpsManager = plus.android.importClass("android.app.AppOpsManager");
  372. var appOps = main.getSystemService(Context.APP_OPS_SERVICE);
  373. var packageName = main.getPackageName();
  374. var uid = plus.android.importClass("android.os.Process").myUid();
  375. var mode = appOps.checkOpNoThrow("android:get_usage_stats", uid, packageName);
  376. console.log(AppOpsManager.OPSTR_GET_USAGE_STATS ,mode,AppOpsManager.MODE_ALLOWED);
  377. if (mode === AppOpsManager.MODE_ALLOWED) {
  378. console.log("✅ 应用未被应用锁限制");
  379. return true;
  380. } else {
  381. console.log("⚠️ 应用可能被应用锁限制");
  382. return false;
  383. }
  384. }
  385. function openAppLockSettings() {
  386. const brand = uni.getSystemInfoSync().brand.toLowerCase();
  387. console.log(brand);
  388. if (brand.includes("xiaomi")) {
  389. plus.runtime.openURL("miui://securitycenter",(res)=>{
  390. console.log(res,'test');
  391. }); // 小米 - 安全中心
  392. } else if (brand.includes("huawei")) {
  393. plus.runtime.openURL("huawei://settings/security/ApplicationLock"); // 华为 - 应用锁
  394. } else if (brand.includes("oppo")) {
  395. plus.runtime.openURL("oppo://securecenter/appLock"); // OPPO
  396. } else if (brand.includes("vivo")) {
  397. plus.runtime.openURL("vivosec://AppLock"); // VIVO
  398. } else {
  399. openGeneralSettings(); // 其他品牌通用方法
  400. }
  401. }
  402. function openGeneralSettings() {
  403. var Intent = plus.android.importClass("android.content.Intent");
  404. var Settings = plus.android.importClass("android.provider.Settings");
  405. var main = plus.android.runtimeMainActivity();
  406. var intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); // 安全设置
  407. main.startActivity(intent);
  408. }
  409. module.exports = {
  410. judgeIosPermission: judgeIosPermission,
  411. requestAndroidPermission: requestAndroidPermission,
  412. checkSystemEnableLocation: checkSystemEnableLocation,
  413. gotoAppPermissionSetting: gotoAppPermissionSetting,
  414. checkBatteryOptimizationStatus,
  415. checkAppLockStatus,
  416. openBatteryOptimization,
  417. checkBackgroundRunningPermission,
  418. requestAndroidPermissions,
  419. openAppLockSettings,//前往打开应用锁
  420. judgeIosPermissionLocationAlways,
  421. judgeIosPermissionLocationAccuracy
  422. }