权限判断
parent
4c21140f1e
commit
7959bc28bd
|
|
@ -1,5 +1,10 @@
|
||||||
<script>
|
<script>
|
||||||
import { useUserStoreWithOut } from '@/store/modules/user'
|
import { useUserStoreWithOut } from '@/store/modules/user'
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
// 实现,路由拦截。当应用无访问摄像头/相册权限,引导跳到设置界面 https://ext.dcloud.net.cn/plugin?id=5095
|
||||||
|
import interceptorChooseImage from '@/uni_modules/json-interceptor-chooseImage/js_sdk/main.js';
|
||||||
|
interceptorChooseImage()
|
||||||
|
// #endif
|
||||||
export default {
|
export default {
|
||||||
onLaunch: function () {
|
onLaunch: function () {
|
||||||
const userStore = useUserStoreWithOut()
|
const userStore = useUserStoreWithOut()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
## 1.0.2(2021-05-20)
|
||||||
|
修复IOS提示不准确,无摄像头权限提示了无法访问相册
|
||||||
|
## 1.0.1(2021-05-20)
|
||||||
|
新增文档和示例代码
|
||||||
|
## 1.0.0(2021-05-20)
|
||||||
|
第一版本发布
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
export default function(){
|
||||||
|
//当应用无访问摄像头/相册权限,引导跳到设置界面
|
||||||
|
uni.addInterceptor('chooseImage', {
|
||||||
|
fail(e) { // 失败回调拦截 更多拦截器用法 [详情](https://uniapp.dcloud.io/api/interceptor?id=addinterceptor)
|
||||||
|
console.log(e);
|
||||||
|
if (uni.getSystemInfoSync().platform == "android" && e.errMsg == 'chooseImage:fail No Permission') {
|
||||||
|
if (e.code === 11) {
|
||||||
|
uni.showModal({
|
||||||
|
title: "无法访问摄像头",
|
||||||
|
content: "当前无摄像头访问权限,建议前往设置",
|
||||||
|
confirmText: "前往设置",
|
||||||
|
success(e) {
|
||||||
|
if (e.confirm) {
|
||||||
|
gotoAppPermissionSetting()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
uni.showModal({
|
||||||
|
title: "无法访问相册",
|
||||||
|
content: "当前无系统相册访问权限,建议前往设置",
|
||||||
|
confirmText: "前往设置",
|
||||||
|
success(e) {
|
||||||
|
if (e.confirm) {
|
||||||
|
gotoAppPermissionSetting()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (e.code === 2&&e.errMsg == "chooseImage:fail No filming permission") {
|
||||||
|
console.log('e.errMsg === 2 ios无法拍照权限 ');
|
||||||
|
// 注:e.code === 8 ios无从相册选择图片的权限 api已内置无需自己用拦截器实现
|
||||||
|
uni.showModal({
|
||||||
|
title: "无法访问摄像头",
|
||||||
|
content: "当前无摄像头访问权限,建议前往设置",
|
||||||
|
confirmText: "前往设置",
|
||||||
|
success(e) {
|
||||||
|
if (e.confirm) {
|
||||||
|
gotoAppPermissionSetting()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
//跳转到**应用**的权限页面 参考来源:https://ext.dcloud.net.cn/plugin?id=594
|
||||||
|
function gotoAppPermissionSetting() {
|
||||||
|
if (uni.getSystemInfoSync().platform == "ios") {
|
||||||
|
var UIApplication = plus.ios.import("UIApplication");
|
||||||
|
var application2 = UIApplication.sharedApplication();
|
||||||
|
var NSURL2 = plus.ios.import("NSURL");
|
||||||
|
var setting2 = NSURL2.URLWithString("app-settings:");
|
||||||
|
application2.openURL(setting2);
|
||||||
|
plus.ios.deleteObject(setting2);
|
||||||
|
plus.ios.deleteObject(NSURL2);
|
||||||
|
plus.ios.deleteObject(application2);
|
||||||
|
} else {
|
||||||
|
var Intent = plus.android.importClass("android.content.Intent");
|
||||||
|
var Settings = plus.android.importClass("android.provider.Settings");
|
||||||
|
var Uri = plus.android.importClass("android.net.Uri");
|
||||||
|
var mainActivity = plus.android.runtimeMainActivity();
|
||||||
|
var intent = new Intent();
|
||||||
|
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||||
|
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
|
||||||
|
intent.setData(uri);
|
||||||
|
mainActivity.startActivity(intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
{
|
||||||
|
"id": "json-interceptor-chooseImage",
|
||||||
|
"displayName": "拦截器应用示例 — 图片选择",
|
||||||
|
"version": "1.0.2",
|
||||||
|
"description": "当选择图片遇到权限问题时引导用户快捷打开系统设置",
|
||||||
|
"keywords": [
|
||||||
|
"interceptor,拦截器,相册权限"
|
||||||
|
],
|
||||||
|
"repository": "",
|
||||||
|
"engines": {
|
||||||
|
"HBuilderX": "^3.1.0"
|
||||||
|
},
|
||||||
|
"dcloudext": {
|
||||||
|
"category": [
|
||||||
|
"JS SDK",
|
||||||
|
"通用 SDK"
|
||||||
|
],
|
||||||
|
"sale": {
|
||||||
|
"regular": {
|
||||||
|
"price": "0.00"
|
||||||
|
},
|
||||||
|
"sourcecode": {
|
||||||
|
"price": "0.00"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"contact": {
|
||||||
|
"qq": ""
|
||||||
|
},
|
||||||
|
"declaration": {
|
||||||
|
"ads": "无",
|
||||||
|
"data": "无",
|
||||||
|
"permissions": "无"
|
||||||
|
},
|
||||||
|
"npmurl": ""
|
||||||
|
},
|
||||||
|
"uni_modules": {
|
||||||
|
"dependencies": [],
|
||||||
|
"encrypt": [],
|
||||||
|
"platforms": {
|
||||||
|
"cloud": {
|
||||||
|
"tcb": "y",
|
||||||
|
"aliyun": "y"
|
||||||
|
},
|
||||||
|
"client": {
|
||||||
|
"App": {
|
||||||
|
"app-vue": "y",
|
||||||
|
"app-nvue": "y"
|
||||||
|
},
|
||||||
|
"H5-mobile": {
|
||||||
|
"Safari": "n",
|
||||||
|
"Android Browser": "n",
|
||||||
|
"微信浏览器(Android)": "n",
|
||||||
|
"QQ浏览器(Android)": "n"
|
||||||
|
},
|
||||||
|
"H5-pc": {
|
||||||
|
"Chrome": "n",
|
||||||
|
"IE": "n",
|
||||||
|
"Edge": "n",
|
||||||
|
"Firefox": "n",
|
||||||
|
"Safari": "n"
|
||||||
|
},
|
||||||
|
"小程序": {
|
||||||
|
"微信": "n",
|
||||||
|
"阿里": "n",
|
||||||
|
"百度": "n",
|
||||||
|
"字节跳动": "n",
|
||||||
|
"QQ": "n"
|
||||||
|
},
|
||||||
|
"快应用": {
|
||||||
|
"华为": "n",
|
||||||
|
"联盟": "n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
拦截器顾名思义,是在框架方法执行的各个环节(包含:拦截前触发、成功回调拦截、失败回调拦截、完成回调拦截)
|
||||||
|
插入逻辑,篡改参数或终止运行。
|
||||||
|
#### 优势
|
||||||
|
- 这种方式相当于改写了api的内部逻辑,相比语法糖他更加直观,不影响ide的代码提示。
|
||||||
|
- 将常规固定的逻辑封装到框架的api内,且支持个性化设计。如你可以在本插件路径:`/uni_modules/json-interceptor-chooseImage/js_sdk/main.js`修改弹出框元素,绘制更漂亮的样式和文字说明。
|
||||||
|
|
||||||
|
#### 使用示例,App.vue页代码如下:
|
||||||
|
```
|
||||||
|
<script>
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
import interceptorChooseImage from '@/uni_modules/json-interceptor-chooseImage/js_sdk/main.js';
|
||||||
|
// #endif
|
||||||
|
export default {
|
||||||
|
onLaunch: function() {
|
||||||
|
console.log('App Launch')
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
interceptorChooseImage()
|
||||||
|
// #endif
|
||||||
|
},
|
||||||
|
onShow: function() {
|
||||||
|
console.log('App Show')
|
||||||
|
},
|
||||||
|
onHide: function() {
|
||||||
|
console.log('App Hide')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
> 跳转到**应用**的权限页面 参考来源:[https://ext.dcloud.net.cn/plugin?id=594](https://ext.dcloud.net.cn/plugin?id=594) 感谢作者@DCloud_heavensoft
|
||||||
Loading…
Reference in New Issue