這段代碼有許多不足之處 可優(yōu)化的地方有許多 親各位大佬指教
畢竟對于mui理解有限 我就不對下面代碼進(jìn)行講解
function choosePhoto() {
if (mui.os.plus) {
var buttonTit = [{
title: "拍照"
}, {
title: "從手機(jī)相冊選擇"
}];
plus.nativeUI.actionSheet({
title: "上傳圖片",
cancel: "取消",
buttons: buttonTit
}, function(b) { /*actionSheet 按鈕點(diǎn)擊事件*/
switch (b.index) {
case 0:
break;
case 1:
getImage(); /*拍照*/
break;
case 2:
galleryImg(); /*打開相冊*/
break;
default:
break;
}
})
}
};
// 拍照獲取圖片
function getImage() {
//調(diào)用相機(jī)
plus.camera.getCamera().captureImage(function(e) {
//獲取file對象entry
plus.io.resolveLocalFileSystemURL(e, function(entry) {
var path = entry.toLocalURL();
entry.file(function(file) {
vm.paths.push(path)
$('.getPhoto').val(file.name)
vm.fileName = file.name
//new plus.io.FileReader()方法
var reader = new plus.io.FileReader();
reader.onloadend = function(e) {
//e獲取到對象 e.target.result是圖片base64
upload(e.target.result, file.name)
}
reader.readAsDataURL(file);
})
}, function(e) {
mui.toast("讀取拍照文件錯誤:" + e.message);
});
});
}
//從相冊中選擇圖片
function galleryImg() {
// 從相冊中選擇圖片
plus.gallery.pick(function(path) {
plus.io.resolveLocalFileSystemURL(path, function(entry) {
entry.file(function(file) {
var paths = entry.toLocalURL();
vm.fileName = file.name
vm.paths.push(paths)
$('.getPhoto').val(file.name)
var reader = new plus.io.FileReader();
reader.onloadend = function(e) {
upload(e.target.result, file.name)
}
reader.readAsDataURL(file);
})
}, function(e) {
plus.nativeUI.toast("讀取拍照文件錯誤:" + e.message);
});
});
function upload(base64, fileName) {
var wt = plus.nativeUI.showWaiting();
mui.ajax({
type: "POST",
url: zfyurl + "/app/api/order/uploadFile",
data: {
base64: base64, //base64數(shù)據(jù)
fileName: fileName
},
dataType: "json",
success: function(response) {
plus.nativeUI.closeWaiting();
if (response.code == 'M000000') {
vm.enclosure = response.info
mui.toast("上傳成功");
} else {
mui.toast("上傳失敗")
}
},
error: function(err) {
plus.nativeUI.closeWaiting();
console.log(JSON.stringify(err))
}