自己封装的一个实现选择图片弹框国际化的方法,返回的格式与 uni.chooseImage 相同,因此可以把 uni.chooseImage 替换掉,在main.js挂到了Vue原型上:
/** 封装国际化选择图片
* @param {Object} self 组件中的 this
* @param {Object} params 配置参数
*/
Vue.prototype.selectImage = (self, params = {}) => {
let count = params.count || 9; // 数量
let sizeType = params.sizeType || ['original', 'compressed']; // 原图和压缩图
let sourceType = params.sourceType || ['camera', 'album']; // 相册和相机
let success = params.success || function success(){}; // 成功回调
let fail = params.fail || function fail(){}; // 失败回调
let option = {
cancel: self.$t('prompt.btn2'), // i18n取消文字
buttons: [{
title: self.$t('prompt.imgBtn1') // i18n相机文字
},
{
title: self.$t('prompt.imgBtn2') // i18n相册文字
}
]
};
if(!sourceType.includes('camera')) delete option.buttons[0];
if(!sourceType.includes('album')) delete option.buttons[1];
plus.nativeUI.actionSheet(option,
async (tap) => {
if(sourceType.length === 2) {
if( tap.index === 1) {
// 拍照
const cmr = plus.camera.getCamera();
cmr.captureImage((path) => {
plus.io.getFileInfo({
filePath: path,
success: res => {
let result = {
errMsg: "chooseImage:ok",
tempFilePaths: [path],
tempFiles: [{
path: path,
size: res.size
}]
};
success(result);
},
fail: err => fail(err)
});
}, (err) => {fail(err)});
} else if (tap.index === 2) {
// 从相册选择
plus.gallery.pick(
(res) => {
let filesList = res.files;
let tempFiles = [];
for (let path of filesList) {
plus.io.getFileInfo({
filePath: path,
success: r => {
tempFiles.push({
path: path,
size: r.size
});
if(tempFiles.length === filesList.length) {
success({
errMsg: "chooseImage:ok",
tempFilePaths: filesList,
tempFiles
})
}
},
fail: err => fail(err)
});
}
}, (err) => {fail(err)},{
filter: "image",
multiple: true,
maximum: count,
system: false,
});
}
} else if (sourceType.length === 1) {
if(sourceType.includes('camera')) {
// 拍照
const cmr = plus.camera.getCamera();
cmr.captureImage((path) => {
plus.io.getFileInfo({
filePath: path,
success: res => {
let result = {
errMsg: "chooseImage:ok",
tempFilePaths: [path],
tempFiles: [{
path: path,
size: res.size
}]
};
success(result);
},
fail: err => fail(err)
});
}, (err) => {fail(err)});
} else {
// 从相册选择
plus.gallery.pick(
(res) => {
let filesList = res.files;
let tempFiles = [];
for (let path of filesList) {
plus.io.getFileInfo({
filePath: path,
success: r => {
tempFiles.push({
path: path,
size: r.size
});
if(tempFiles.length === filesList.length) {
success({
errMsg: "chooseImage:ok",
tempFilePaths: filesList,
tempFiles
})
}
},
fail: err => fail(err)
});
}
}, (err) => {fail(err)},{
filter: "image",
multiple: true,
maximum: count,
system: false,
});
}
}
});
}
3 个评论
要回复文章请先登录或注册
1***@qq.com
s***@163.com
1***@qq.com