uniapp动态权限修改之摄像头和存储权限

插件:permission.js
uniapp官网授权地址:https://ext.dcloud.net.cn/plugin?id=594#detail

import permision from '../../lib/permission.js';

modify_content: '' //权限内容

//选择相机或者相册更换头像
		sculpture(e) {
			this.$refs.popup_head.close();
			if (e == 1) {
				this.modify_content = '摄像头权限(拍摄头像)';
				//相机
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.requestAndroidPermission('android.permission.CAMERA',e);
						break;
					case 'ios':
						this.judgeIosPermissionCamera();
						break;
				}
			} else {
				this.modify_content = '存储权限(读取相册头像)';
				//相册
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE',e);
						break;
					case 'ios':
						this.judgeIosPermissionPhotoLibrary();
						break;
				}
			}
		},
		// 授权提示
		modify() {
			uni.showModal({
				title: '####需要下列权限才可以正常使用',
				content: this.modify_content,
				confirmText: '前往开启',
				success: function(res) {
					if (res.confirm) {
						permision.gotoAppPermissionSetting(); //动态修改权限
					}
				}
			});
		},
		// 安卓相机/相册
		async requestAndroidPermission(permissionID,e) {
			let resp = await permision.requestAndroidPermission(permissionID,e);
			if (resp == -1) {
				this.modify();
			} else if (resp == 1) {
				if(e==1){
					this.chooseImage1();
				}else{
					this.chooseImage2();
				}
			}
		},
		// ios相机
		async judgeIosPermissionCamera() {
			let resp = await permision.judgeIosPermissionCamera();
			if (!resp) {
				this.modify();
			} else {
				this.chooseImage1();
			}
		},
		// ios相册
		async judgeIosPermissionPhotoLibrary() {
			let resp = await permision.judgeIosPermissionPhotoLibrary();
			if (!resp) {
				this.modify();
			} else {
				this.chooseImage2();
			}
		},
		//选择图片
		chooseImage1() {
			var cmr = plus.camera.getCamera();
			var cmr = plus.camera.getCamera();
			var res = cmr.supportedImageResolutions[0];
			var fmt = cmr.supportedImageFormats[0];
			console.log('Resolution: ' + res + ', Format: ' + fmt);
			cmr.captureImage(
				(path)=> {
					console.log('Capture image success: ' + path);
					this.photo = path;
					//文件压缩
					this.compress();
				},
				function(error) {
					console.log('Capture image failed: ' + error.message);
				},
				{ resolution: res, format: fmt }
			);
		},
		//选择相册
		chooseImage2(){
			plus.gallery.pick(
				(path)=> {
					console.log(path)
					this.photo = path;
					//文件压缩
					this.compress();
				},
				function(e) {
					console.log('取消选择图片');
				},
				{ filter: 'image' }
			);
		},
		//文件压缩
		compress() {
			let photo = this.photo;
			console.log(photo, '相片路径');
			//通过URL参数获取文件对象
			plus.io.resolveLocalFileSystemURL(
				photo,
				entry => {
					//可通过entry对象操作图片
					entry.file(file => {
						//当文件大于30kb进行压缩
						if (file.size > 30 * 1024) {
							plus.zip.compressImage(
								{
									src: photo,
									dst: photo
										.replace('.png', '2222.png')
										.replace('.PNG', '2222.PNG')
										.replace('.jpg', '2222.jpg')
										.replace('.JPG', '2222.JPG'),
									width: '200px',
									height: '200px',
									quality: 10,
									overwrite: true
								},
								event => {
									let newPhoto = photo
										.replace('.png', '2222.png')
										.replace('.PNG', '2222.PNG')
										.replace('.jpg', '2222.jpg')
										.replace('.JPG', '2222.JPG');
									console.log(newPhoto, '新相片');
									this.getQiniuToken(newPhoto);
								},
								function(error) {
									uni.showModal({
										content: '图片太大,需要重新选择图片',
										showCancel: false
									});
								}
							);
						} else {
							console.log(newPhoto, '新相片');
							this.getQiniuToken(photo);
						}
					});
				},
				e => {
					//返回错误信息
					console.log(e.message);
				}
			);
		},
		//获取token
		async getQiniuToken(file) {
			const res = await api.getQiniuToken();
			let token = res.data;
			let key = Math.round(new Date() / 1000) + this.userInfo.phone;
			//上传文件至七牛
			uni.uploadFile({
				url: 'https://up-z2.qiniup.com',
				filePath: file,
				name: 'file',
				formData: {
					key,
					token
				},
				success: res => {
					if (res.statusCode == 200) {
						this.userInfo.headIcon = key;
						//更新用户信息
						this.updateUserInfo(this.userInfo);
					}
				},
				error: res => {
					console.log(res);
				}
			});
		},
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值