插件: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);
}
});
},