当前位置:   article > 正文

uniapp打包成app 华为上架申请权限添加使用说明_uniapp权限申请说明

uniapp权限申请说明

因为华为上架需要添加申请权限时的使用说明,否则就无法通过审核,就封装了申请权限时弹出权限说明窗口,利用了本地存储如果权限已经通过就不弹权限说明窗口直接调用回调,如果没有通过或没有就弹出申请权限弹窗。

1.页面组件

  1. <template>
  2. <view>
  3. <view v-if="showTips" class="fixed bgFF radio30 absolute tips fs28 color02">
  4. <view class="lh50">
  5. {{actionPermission.title}}使用说明:
  6. </view>
  7. <view class="lh50">
  8. {{actionPermission.tips}}
  9. </view>
  10. </view>
  11. <u-popup :show="show" @close="close" mode="center" round="30rpx">
  12. <view class="padding50 bgFF content text-center radio20 fs32 color">
  13. <view class="wh100">
  14. <view class="lh50 marginB40">
  15. 您已禁止{{actionPermission.title}},如需要使用请设置!
  16. </view>
  17. <view @click="handelAllow" class="fw600 paddingT40 borderT1">
  18. 去设置
  19. </view>
  20. </view>
  21. </view>
  22. </u-popup>
  23. </view>
  24. </template>
  25. <script>
  26. import {
  27. tipsConfig
  28. } from './index.js'
  29. import permision from './permission.js'
  30. export default {
  31. data() {
  32. return {
  33. show: false,
  34. key: 'CAMERA',
  35. // isSetting: false,
  36. showTips: false
  37. }
  38. },
  39. computed: {
  40. actionPermission() {
  41. return tipsConfig[this.key]
  42. }
  43. },
  44. methods: {
  45. async showView(key = 'CAMERA', callback) {
  46. // #ifdef APP-PLUS
  47. // 获取系统信息
  48. const systemInfo = uni.getSystemInfoSync();
  49. // 判断是否为ios
  50. const isIos = systemInfo?.platform === 'ios';
  51. if (isIos) {
  52. return callback && callback()
  53. }
  54. this.key = key
  55. // 如果返回1 则是允许权限 直接调用回调就行 如果是0则询问 -1则永久拒绝
  56. const permission = uni.getStorageSync('permission') || {}
  57. const keyStatus = permission[this.actionPermission?.key] || null
  58. if (keyStatus == 1) {
  59. return callback && callback()
  60. }
  61. // else if (keyStatus == -1) {
  62. // this.show = true
  63. // return
  64. // }
  65. const result = await this.requestAndroidPermission()
  66. permission[this.actionPermission?.key] = result
  67. uni.setStorageSync('permission', permission)
  68. if (result == 1) {
  69. callback && callback()
  70. } else {
  71. this.show = true
  72. }
  73. //#endif
  74. // #ifndef APP-PLUS
  75. callback && callback()
  76. //#endif
  77. },
  78. close() {
  79. this.show = false
  80. this.showTips = false
  81. },
  82. /**
  83. * 禁止
  84. */
  85. handelProhibited(status) {
  86. this.close()
  87. },
  88. /**
  89. * 允许
  90. */
  91. handelAllow() {
  92. this.show = false
  93. permision.gotoAppPermissionSetting()
  94. },
  95. async requestAndroidPermission() {
  96. this.showTips = true
  97. return await permision.requestAndroidPermission(this.actionPermission.permission, () => {
  98. this.showTips = false
  99. })
  100. },
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .tips-box {
  106. left: 0;
  107. top: 0;
  108. width: 100vw;
  109. height: 100vh;
  110. }
  111. .tips {
  112. width: 80vw;
  113. padding: 30rpx 70rpx;
  114. left: 50%;
  115. top: 15vh;
  116. transform: translateX(-50%);
  117. z-index: 10076;
  118. }
  119. /deep/ .u-popup__content {
  120. background-color: transparent;
  121. }
  122. .content {
  123. width: 80vw;
  124. }
  125. .color {
  126. color: #4994F6;
  127. }
  128. .action::before {
  129. content: '';
  130. width: 4rpx;
  131. height: 100%;
  132. position: absolute;
  133. left: 50%;
  134. top: 0;
  135. transform: translateX(-50%);
  136. background-color: #D9D9D9;
  137. }
  138. </style>

2.权限相关配置说明

  1. export const tipsConfig = {
  2. LOCATION: {
  3. title: '位置权限',
  4. tips: '用于获取当前位置信息、考勤打卡等场景',
  5. describe: '获取位置信息',
  6. key: 'LOCATION',
  7. permission:'android.permission.ACCESS_FINE_LOCATION'
  8. },
  9. CAMERA: {
  10. title: '相机/拍摄权限',
  11. tips: '用于拍照、录制视频等场景',
  12. describe: '拍摄照片和录制视频',
  13. key: 'CAMERA',
  14. permission:'android.permission.CAMERA'
  15. },
  16. CAMERA_STORAGE: {
  17. title: '相机/拍摄/存储空间权限',
  18. tips: '用于拍照、上传照片等场景',
  19. describe: '拍摄照片、上传照片',
  20. key: 'STORAGE',
  21. permission:'android.permission.CAMERA'
  22. },
  23. CAMERA_VIDEO_STORAGE: {
  24. title: '相机/拍摄/存储空间权限',
  25. tips: '用于拍摄视频、上传视频等场景',
  26. describe: '拍摄视频、上传视频',
  27. key: 'STORAGE',
  28. permission:'android.permission.CAMERA'
  29. },
  30. STORAGE: {
  31. title: '存储空间/照片权限',
  32. tips: '用于上传文件、上传照片和视频等场景',
  33. describe: '上传文件和照片、视频',
  34. key: 'STORAGE',
  35. permission:'android.permission.READ_EXTERNAL_STORAGE'
  36. },
  37. RECORD_AUDIO: {
  38. title: '麦克风权限',
  39. tips: '用于发送语音等场景',
  40. describe: '录制音频',
  41. key: 'RECORD_AUDIO',
  42. permission:'android.permission.RECORD_AUDIO'
  43. },
  44. CALL_PHONE: {
  45. title: '拨打电话权限',
  46. tips: '用于拨打电话等场景',
  47. describe: '拨打电话',
  48. key: 'CALL_PHONE',
  49. permission:'android.permission.CALL_PHONE'
  50. }
  51. }
  52. export const clearPermissionStorageKey = (key,err,callback) => {
  53. if(err?.errMsg?.includes('No Permission')){
  54. const permission = uni.getStorageSync('permission') || {}
  55. if(permission[key] == 1){
  56. delete permission[key]
  57. uni.setStorageSync('permission',permission)
  58. callback && callback()
  59. }
  60. }
  61. }

3.查询安卓权限是否授权代码(网上抄的)

  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,callback) {
  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. callback && callback()
  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. callback && callback()
  180. resolve({
  181. code: error.code,
  182. message: error.message
  183. });
  184. }
  185. );
  186. });
  187. }
  188. // 使用一个方法,根据参数判断权限
  189. function judgeIosPermission(permissionID) {
  190. if (permissionID == "location") {
  191. return judgeIosPermissionLocation()
  192. } else if (permissionID == "camera") {
  193. return judgeIosPermissionCamera()
  194. } else if (permissionID == "photoLibrary") {
  195. return judgeIosPermissionPhotoLibrary()
  196. } else if (permissionID == "record") {
  197. return judgeIosPermissionRecord()
  198. } else if (permissionID == "push") {
  199. return judgeIosPermissionPush()
  200. } else if (permissionID == "contact") {
  201. return judgeIosPermissionContact()
  202. } else if (permissionID == "calendar") {
  203. return judgeIosPermissionCalendar()
  204. } else if (permissionID == "memo") {
  205. return judgeIosPermissionMemo()
  206. }
  207. return false;
  208. }
  209. // 跳转到**应用**的权限页面
  210. function gotoAppPermissionSetting() {
  211. if (isIos) {
  212. var UIApplication = plus.ios.import("UIApplication");
  213. var application2 = UIApplication.sharedApplication();
  214. var NSURL2 = plus.ios.import("NSURL");
  215. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  216. var setting2 = NSURL2.URLWithString("app-settings:");
  217. application2.openURL(setting2);
  218. plus.ios.deleteObject(setting2);
  219. plus.ios.deleteObject(NSURL2);
  220. plus.ios.deleteObject(application2);
  221. } else {
  222. // console.log(plus.device.vendor);
  223. var Intent = plus.android.importClass("android.content.Intent");
  224. var Settings = plus.android.importClass("android.provider.Settings");
  225. var Uri = plus.android.importClass("android.net.Uri");
  226. var mainActivity = plus.android.runtimeMainActivity();
  227. var intent = new Intent();
  228. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  229. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  230. intent.setData(uri);
  231. mainActivity.startActivity(intent);
  232. }
  233. }
  234. // 检查系统的设备服务是否开启
  235. // var checkSystemEnableLocation = async function () {
  236. function checkSystemEnableLocation() {
  237. if (isIos) {
  238. var result = false;
  239. var cllocationManger = plus.ios.import("CLLocationManager");
  240. var result = cllocationManger.locationServicesEnabled();
  241. console.log("系统定位开启:" + result);
  242. plus.ios.deleteObject(cllocationManger);
  243. return result;
  244. } else {
  245. var context = plus.android.importClass("android.content.Context");
  246. var locationManager = plus.android.importClass("android.location.LocationManager");
  247. var main = plus.android.runtimeMainActivity();
  248. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  249. var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
  250. console.log("系统定位开启:" + result);
  251. return result
  252. }
  253. }
  254. module.exports = {
  255. judgeIosPermission: judgeIosPermission,
  256. requestAndroidPermission: requestAndroidPermission,
  257. checkSystemEnableLocation: checkSystemEnableLocation,
  258. gotoAppPermissionSetting: gotoAppPermissionSetting
  259. }

4.使用方法:
1.页面引入组件,申请权限前调用组件showView方法 传入对应的权限值和回调函数
2.如果用户拒绝或取消就调用 clearPermissionStorageKey 方法删除本地是否已经授权的权限值

  1. 1.引入组件、注册组件
  2. import Authorize from '@/components/authorize/index.vue'
  3. <Authorize ref="authorize" />
  4. import {
  5. clearPermissionStorageKey
  6. } from '@/components/authorize/index.js'
  7. 2.方法调用
  8. handelAuthorize() {
  9. this.$refs?.authorize?.showView('CAMERA_VIDEO_STORAGE', this.handelVideo)
  10. },
  11. 3.回调函数
  12. // 视频
  13. handelVideo() {
  14. uni.chooseVideo({
  15. sourceType: ['camera', 'album'],
  16. success: async (res) => {
  17. },
  18. fail: (err) => {
  19. clearPermissionStorageKey('CAMERA_VIDEO_STORAGE', err, () => {
  20. this.handelVideo()
  21. })
  22. }
  23. });
  24. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/776004
推荐阅读
相关标签
  

闽ICP备14008679号