当前位置:   article > 正文

uniapp 解决华为上架被拒问题,APP在申请敏感权限时,应同步说明权限申请的使用目的_app敏感权限申请说明

app敏感权限申请说明

1、store/modules/permission.js

  1. // app权限申请处理
  2. const state = {
  3. // 处理应用程序权限请求
  4. CAMERA: false,
  5. WRITE_EXTERNAL_STORAGE: false,
  6. ACCESS_FINE_LOCATION: false,
  7. CALL_PHONE: false,
  8. isIos: uni.getSystemInfoSync().platform == 'ios',
  9. mapping: {
  10. 'CAMERA': {
  11. title: '摄像头权限说明',
  12. content: '摄像头权限将用于拍摄照片和视频。这样,您可以在应用程序中记录瞬间、分享内容或进行其他相关操作。',
  13. methods: 'SET_CAMERA'
  14. },
  15. 'WRITE_EXTERNAL_STORAGE': {
  16. title: '存储空间/照片权限申请说明',
  17. content: '便于您使用该功能上传您的照片/图片/视频及用于更换头像、发布商品/分享、下载、与客服沟通等场景中读取和写入相册和文件内容。',
  18. methods: 'SET_WRITE_EXTERNAL_STORAGE'
  19. },
  20. 'ACCESS_FINE_LOCATION': {
  21. title: '地理位置权限申请说明',
  22. content: '****应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。',
  23. methods: 'SET_ACCESS_FINE_LOCATION'
  24. },
  25. 'CALL_PHONE': {
  26. title: '拨打/管理电话权限申请说明',
  27. content: '便于您使用该功能联系买家、骑手或者客服、业务经理与联系等场景下使用',
  28. methods: 'SET_CALL_PHONE'
  29. }
  30. }
  31. }
  32. const mutations = {
  33. // 管理权限告知目的
  34. SET_CAMERA(state, val) {
  35. state.CAMERA = val
  36. },
  37. SET_WRITE_EXTERNAL_STORAGE(state, val) {
  38. state.WRITE_EXTERNAL_STORAGE = val
  39. },
  40. SET_CALL_PHONE(state, val) {
  41. state.CALL_PHONE = val
  42. },
  43. SET_ACCESS_FINE_LOCATION(state, val) {
  44. state.ACCESS_FINE_LOCATION = val
  45. }
  46. }
  47. const actions = {
  48. // 权限获取
  49. async requestPermissions({ state, dispatch, commit }, permissionID) {
  50. try {
  51. if (!state[permissionID] && !state.isIos) {
  52. var viewObj = await dispatch('nativeObjView', permissionID)
  53. viewObj.show()
  54. }
  55. console.log('android.permission.' + permissionID, '当前手机权限')
  56. return new Promise(async (resolve, reject) => {
  57. // ios不需要这个
  58. if (state.isIos) {
  59. resolve(1)
  60. return
  61. }
  62. // Android权限查询
  63. function requestAndroidPermission(permissionID_) {
  64. return new Promise((resolve, reject) => {
  65. plus.android.requestPermissions(
  66. [permissionID_], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  67. function(resultObj) {
  68. var result = 0
  69. for (var i = 0; i < resultObj.granted.length; i++) {
  70. // var grantedPermission = resultObj.granted[i];
  71. // console.log('已获取的权限:' + grantedPermission);
  72. result = 1
  73. }
  74. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  75. // var deniedPresentPermission = resultObj.deniedPresent[i];
  76. // console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  77. result = 0
  78. }
  79. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  80. // var deniedAlwaysPermission = resultObj.deniedAlways[i];
  81. // console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  82. result = -1
  83. }
  84. resolve(result)
  85. },
  86. function(error) {
  87. console.log('申请权限错误:' + error.code + ' = ' + error
  88. .message)
  89. resolve({
  90. code: error.code,
  91. message: error.message
  92. })
  93. }
  94. )
  95. })
  96. }
  97. const result = await requestAndroidPermission('android.permission.' + permissionID)
  98. if (result === 1) {
  99. // '已获得授权'
  100. commit(state.mapping[permissionID].methods, true)
  101. } else if (result === 0) {
  102. // '未获得授权'
  103. commit(state.mapping[permissionID].methods, false)
  104. } else {
  105. commit(state.mapping[permissionID].methods, true)
  106. uni.showModal({
  107. title: '提示',
  108. content: '操作权限已被拒绝,请手动前往设置',
  109. confirmText: '立即设置',
  110. success: (res) => {
  111. if (res.confirm) {
  112. dispatch('gotoAppPermissionSetting')
  113. }
  114. }
  115. })
  116. }
  117. if (viewObj) viewObj.close()
  118. resolve(result)
  119. })
  120. } catch (error) {
  121. console.log(error)
  122. reject(error)
  123. }
  124. },
  125. // 提示框
  126. nativeObjView({ state }, permissionID) {
  127. const systemInfo = uni.getSystemInfoSync()
  128. const statusBarHeight = systemInfo.statusBarHeight
  129. const navigationBarHeight = systemInfo.platform === 'android' ? 48 : 44 // Set the navigation bar height based on the platform
  130. const totalHeight = statusBarHeight + navigationBarHeight
  131. let view = new plus.nativeObj.View('per-modal', {
  132. top: '0px',
  133. left: '0px',
  134. width: '100%',
  135. backgroundColor: 'rgba(0,0,0,0.5)'
  136. })
  137. view.drawRect({
  138. color: '#fff',
  139. radius: '5px'
  140. }, {
  141. top: totalHeight + 'px',
  142. left: '5%',
  143. width: '90%',
  144. height: '100px'
  145. })
  146. view.drawText(state.mapping[permissionID].title, {
  147. top: totalHeight + 5 + 'px',
  148. left: '8%',
  149. height: '30px'
  150. }, {
  151. align: 'left',
  152. color: '#000'
  153. }, {
  154. onClick: function(e) {
  155. console.log(e)
  156. }
  157. })
  158. view.drawText(state.mapping[permissionID].content, {
  159. top: totalHeight + 35 + 'px',
  160. height: '60px',
  161. left: '8%',
  162. width: '84%'
  163. }, {
  164. whiteSpace: 'normal',
  165. size: '14px',
  166. align: 'left',
  167. color: '#656563'
  168. })
  169. function show() {
  170. view = plus.nativeObj.View.getViewById('per-modal')
  171. view.show()
  172. view = null // 展示的时候也得清空,不然影响下次的关闭,不知道为啥
  173. }
  174. function close() {
  175. view = plus.nativeObj.View.getViewById('per-modal')
  176. view.close()
  177. view = null
  178. }
  179. return { show, close }
  180. },
  181. // 跳转到**应用**的权限页面
  182. gotoAppPermissionSetting({ state }) {
  183. if (state.isIos) {
  184. var UIApplication = plus.ios.import('UIApplication')
  185. var application2 = UIApplication.sharedApplication()
  186. var NSURL2 = plus.ios.import('NSURL')
  187. var setting2 = NSURL2.URLWithString('app-settings:')
  188. application2.openURL(setting2)
  189. plus.ios.deleteObject(setting2)
  190. plus.ios.deleteObject(NSURL2)
  191. plus.ios.deleteObject(application2)
  192. } else {
  193. var Intent = plus.android.importClass('android.content.Intent')
  194. var Settings = plus.android.importClass('android.provider.Settings')
  195. var Uri = plus.android.importClass('android.net.Uri')
  196. var mainActivity = plus.android.runtimeMainActivity()
  197. var intent = new Intent()
  198. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
  199. var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
  200. intent.setData(uri)
  201. mainActivity.startActivity(intent)
  202. }
  203. }
  204. }
  205. const getters = {}
  206. export default {
  207. state,
  208. getters,
  209. mutations,
  210. actions
  211. }

 2、store/index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import permission from './modules/permission.js'
  4. const namespaced = true
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. modules: {
  8. permission: {
  9. namespaced,
  10. ...permission
  11. }
  12. }
  13. })
  14. export default store

3、使用

  1. clickFun() {
  2. // #ifdef APP-PLUS
  3. const result = await uni.$store.dispatch('permission/requestPermissions', 'WRITE_EXTERNAL_STORAGE')
  4. if (result !== 1) return
  5. // #endif
  6. // 权限拿到后,下面的逻辑根据你们的业务逻辑来写 ...
  7. uni.showToast({
  8. title:'权限获取成功'
  9. })
  10. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/648315
推荐阅读
相关标签
  

闽ICP备14008679号