|
function getPicture(sourceType) {
if (sourceType == 1) { // 拍照
api.getPicture({
sourceType: 'camera',
encodingType: 'jpg',
mediaValue: 'pic',
allowEdit: false,
destinationType: 'url',
quality: 80,
targetWidth: 800,
targetHeight: 800,
saveToPhotoAlbum: false
}, function(ret, err) {
if (ret) {
if (!utils.isNullOrEmpty(ret.data))
{
uploadAndSaveImage(ret.data);
}
}
});
} else if (sourceType == 2) { // 从图片库中选择
console.log("sourceType:" + sourceType);
api.getPicture({
sourceType: 'library',
encodingType: 'jpg',
mediaValue: 'pic',
destinationType: 'url',
quality: 80,
targetWidth: 800,
targetHeight: 800
}, function(ret, err) {
console.log(JSON.stringify(ret));
if (ret) {
if (!utils.isNullOrEmpty(ret.data))
{
uploadAndSaveImage(ret.data);
}
}
});
}
}
function uploadAndSaveImage(imgUrl) {
console.log("upload");
toast.loading({
title: "正在上传"
});
api.ajax({
url: api.getPrefs({
sync: true,
key: 'serverUrl'
}) + '/mental-api/attach/upload',
headers: {
"token": api.getPrefs({
sync: true,
key: 'token'
})
},
data: {
files: {
"file": imgUrl
}
},
method: "post",
dataType: 'json',
timeout: 120,
async: true,
}, function(ret, err) {
toast.hide();
console.log(JSON.stringify(ret));
if (ret) {
if (ret.code == 0) {
$("#images").css("display","none");
$("#fmImages").css("display","");
if(utils.isNullOrEmpty(ret.uploadFiles[0].fileUrl) && ret.uploadFiles[0].fileUrl.substring(0, 4) == "http")
{
$("#fmImages").attr("src", imagePath + ret.uploadFiles[0].fileUrl);
$("#fmImages").attr("data_url", ret.uploadFiles[0].fileUrl);
}
else{
$("#fmImages").attr("src", ret.uploadFiles[0].fileUrl);
$("#fmImages").attr("data_url", ret.uploadFiles[0].fileUrl);
}
if (($.trim($("#fmTitle").val()) != "") && (audioContent != "") && ($("#category").val() != "") && ($("#fmImages").attr("src") != "../../image/noImg.png"))
{
$("#pubishFm").css("color", "#21c29e");
}
else {
$("#pubishFm").css("color", "#666666");
}
} else {
toast.fail({title:ret.msg});
}
} else {
toast.fail({title:err.msg});
}
})
}
function appendImage() {
api.actionSheet({
title: '',
cancelTitle: '取消',
buttons: ['拍照', '从手机相册选择']
}, function(ret, err) {
if (ret) {
console.log(JSON.stringify(ret));
getPicture(ret.buttonIndex);
}
});
} |
|