加载中...

uniapp 权限申请告知目的,华为上架审核

uniapp 权限申请告知目的,华为上架审核

效果图

New Image

权限告知

第一步vuex里面定义一个模块,专门管理这个的

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

另外:看到有人使用 这个检测权限,我没试过,有兴趣的可以自己试一下

plus.navigator.checkPermission(''android.permission.CAMERA'') 

第二步直接引用,在原来基础上加就好了

  1. <template>
  2. <view>
  3. <button type="default" @click="save">读取相册</button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. }
  11. },
  12. methods: {
  13. async save() {
  14. /* #ifdef APP-PLUS */
  15. let result = await this.$store.dispatch("app/requestPermissions",
  16. ''WRITE_EXTERNAL_STORAGE'')
  17. if (result !== 1) return
  18. /* #endif */
  19. uni.saveImageToPhotosAlbum({
  20. filePath: "",
  21. success: function(res) {
  22. uni.showToast({
  23. title: ''保存成功'',
  24. icon:"success"
  25. });
  26. },
  27. fail: function(res) {
  28. uni.showToast({
  29. title: ''保存失败'',
  30. icon:"fail"
  31. });
  32. },
  33. })
  34. }
  35. }
  36. }
  37. </script>
New Image

最后说明:借鉴了好几个大佬的代码,自己整合了一下。还有就是因为plus.android.requestPermissions只有点击才有回调,如果权限弹框出来了,跳转设置页面的时候,再回来,自己要在show里面在判断一下。我反正没搞,我觉得可以混过审核了