permissionStore.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. defineStore
  3. } from 'pinia';
  4. export const usePermission = defineStore('permission', {
  5. state: () => {
  6. return {
  7. dialogView: null,
  8. permissionListener: null,
  9. list: [{
  10. name: "READ_EXTERNAL_STORAGE",
  11. title: "读照片及文件权限:",
  12. content: "用于更换、上传您的头像、图片功能,不授权该权限会影响app的正常使用",
  13. }]
  14. };
  15. },
  16. actions: {
  17. //监听权限申请
  18. async requstPermission(permissionID) {
  19. return new Promise((resolve, reject) => {
  20. try {
  21. // if (!uni.getSystemInfoSync().platform == 'android') return resolve(true)
  22. /**
  23. * @description plus.navigator.checkPermission 检查应用是否获取指定权限
  24. * 有些权限检测不到 就继续下面的代码,比如相册权限就可以直接检测,就很方便,授权情况下不需要再走下面代码了
  25. * checkPermission 返回参数
  26. * @params undetermined 未确定
  27. * @params authorized 授权
  28. */
  29. let checkPermission = plus.navigator.checkPermission(
  30. "android.permission." + permissionID
  31. );
  32. if (checkPermission == "authorized") return resolve(true);
  33. //判断是否自己在list里面配置了这个权限
  34. let index = this.list.findIndex((item) => item.name == permissionID);
  35. if (index == -1) throw new Error("这个权限没有配置");
  36. //唤起原生权限说明弹框
  37. this.requstPermissionDialog(index);
  38. //授权检测回调
  39. plus.android.requestPermissions(
  40. [
  41. "android.permission." + permissionID, //单个权限
  42. ],
  43. (resultObj) => {
  44. this.permissionListener.stop();
  45. console.log(resultObj, "resultObj");
  46. // 权限申请结果
  47. /**
  48. * @description resultObj.deniedAlways 永久拒绝授权
  49. * 多个权限返回结果可能是{"granted":["android.permission.CAMERA"],"deniedPresent":[],"deniedAlways":["android.permission.READ_EXTERNAL_STORAGE"]}
  50. * 这个情况就是我同时授权相册和相机,但是只允许了相机,没有授权相册
  51. * 这个时候 可以通过deniedAlways 查看哪个权限被永久拒绝了,然后自行在设置弹框内容
  52. * 所以可以自己判断细分一下,我下面的代码是先判断了是否有永久拒绝的权限,然后直接弹框提示用户去设置
  53. */
  54. if (resultObj.deniedAlways && resultObj.deniedAlways.length > 0) {
  55. uni.showModal({
  56. title: "提示",
  57. content: "操作权限已被拒绝,请手动前往设置",
  58. confirmText: "立即设置",
  59. success: (res) => {
  60. if (res.confirm) {
  61. this.gotoAppPermissionSetting();
  62. } else {
  63. resolve(false);
  64. }
  65. },
  66. });
  67. console.log("永久拒绝授权");
  68. } else if (
  69. resultObj.deniedPresent &&
  70. resultObj.deniedPresent.length > 0
  71. ) {
  72. resolve(false);
  73. console.log("拒绝授权");
  74. } else if (resultObj.granted && resultObj.granted.length > 0) {
  75. resolve(true);
  76. console.log("授权成功");
  77. }
  78. },
  79. (error) => {
  80. reject(false);
  81. console.log("申请权限错误:", error);
  82. }
  83. );
  84. } catch (err) {
  85. reject(false);
  86. console.log(err);
  87. }
  88. });
  89. },
  90. //监听弹框
  91. requstPermissionDialog(index) {
  92. try {
  93. if (!this.permissionListener)
  94. this.permissionListener = uni.createRequestPermissionListener();
  95. const dialogData = this.list[index];
  96. this.permissionListener.onConfirm((res) => {
  97. this.dialogStyle(dialogData, true);
  98. });
  99. this.permissionListener.onComplete(async (res) => {
  100. this.dialogStyle({}, false);
  101. });
  102. } catch (err) {
  103. console.log("监听弹框错误", err);
  104. }
  105. },
  106. //弹框样式
  107. dialogStyle({
  108. title = "",
  109. content = ""
  110. }, status) {
  111. try {
  112. if (!status) return this.dialogView.close();
  113. const systemInfo = uni.getSystemInfoSync();
  114. const statusBarHeight = systemInfo.statusBarHeight;
  115. const navigationBarHeight = systemInfo.platform === "android" ? 48 : 44;
  116. const totalHeight = statusBarHeight + navigationBarHeight;
  117. this.dialogView = new plus.nativeObj.View("per-modal", {
  118. top: "0px",
  119. left: "0px",
  120. width: "100%",
  121. backgroundColor: "#444",
  122. //opacity: .5;
  123. });
  124. this.dialogView.drawRect({
  125. color: "#fff",
  126. radius: "5px",
  127. }, {
  128. top: totalHeight + "px",
  129. left: "5%",
  130. width: "90%",
  131. height: "100px",
  132. });
  133. this.dialogView.drawText(
  134. title, {
  135. top: totalHeight + 5 + "px",
  136. left: "8%",
  137. height: "30px",
  138. }, {
  139. align: "left",
  140. color: "#000",
  141. }
  142. );
  143. this.dialogView.drawText(
  144. content, {
  145. top: totalHeight + 35 + "px",
  146. height: "60px",
  147. left: "8%",
  148. width: "84%",
  149. }, {
  150. whiteSpace: "normal",
  151. size: "14px",
  152. align: "left",
  153. color: "#656563",
  154. }
  155. );
  156. this.dialogView.show();
  157. } catch (e) {
  158. console.log(e, "权限说明弹框样式错误");
  159. }
  160. },
  161. //跳转到app权限设置页面
  162. gotoAppPermissionSetting() {
  163. if (!uni.getSystemInfoSync().platform == "android") {
  164. // ios
  165. /* var UIApplication = plus.ios.import("UIApplication");
  166. var application2 = UIApplication.sharedApplication();
  167. var NSURL2 = plus.ios.import("NSURL");
  168. var setting2 = NSURL2.URLWithString("app-settings:");
  169. application2.openURL(setting2);
  170. plus.ios.deleteObject(setting2);
  171. plus.ios.deleteObject(NSURL2);
  172. plus.ios.deleteObject(application2); */
  173. } else {
  174. // console.log(plus.device.vendor);
  175. var Intent = plus.android.importClass("android.content.Intent");
  176. var Settings = plus.android.importClass("android.provider.Settings");
  177. var Uri = plus.android.importClass("android.net.Uri");
  178. var mainActivity = plus.android.runtimeMainActivity();
  179. var intent = new Intent();
  180. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  181. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  182. intent.setData(uri);
  183. mainActivity.startActivity(intent);
  184. }
  185. },
  186. },
  187. });