DCloud图片上传和压缩


define(['common', 'config'], function(com, con) {
/*
* 打开摄像头、打开图库 模块
*/
return {
//model对象
_model: {
result: true,
id: 0,
img: ""
},
//记录上传文件数
upTimer: {
count: 0,
imgs: []
},
//打开摄像头
open: function(obj, callback) {
my = this;
mui.plusReady(function() {
if($(obj).attr('data-camera-gallery') == "1") {
var a = [{
title: "拍照"
}, {
title: "从手机相册选择"
}];
plus.nativeUI.actionSheet({
//title: "修改用户头像",
cancel: "取消",
buttons: a
}, function(b) {
/*actionSheet 按钮点击事件*/
switch(b.index) {
case 0:
break;
case 1:
my.getImage(obj, callback);
break;
case 2:
my.galleryImg(obj, callback);
break;
default:
break;
}
});
} else {
my.getImage(obj, callback);
}
});
},
//拍照
getImage: function(obj, callback) {
var my = this;
var photoType = $(obj).attr("data-PhotoType") ? $(obj).attr("data-PhotoType") : "";
mui.plusReady(function() {
var data = {
result: false,
msg: '',
data: null
};
var c = plus.camera.getCamera();
c.captureImage(function(e) {
$(obj).css('opacity', '0.3').attr('disabled', true);
plus.io.resolveLocalFileSystemURL(e, function(entry) {
var s = entry.toLocalURL();
data.result = true;
data.data = s;
my.upload(obj, con.urlCONFIG.post_upload, s, callback, photoType);
}, function(e) {
data.data = "读取拍照文件错误:" + e.message;
callback(data);
});
}, function(error) {
data.msg = error.message;
//callback(data);
})
});
},
//打开相册
galleryImg: function(obj, callback) {
var my = this;
plus.gallery.pick(function(a) {
var photoType = $(obj).attr("data-PhotoType") ? $(obj).attr("data-PhotoType") : "";
var data = {
result: false,
msg: '',
data: null
};
$(obj).css('opacity', '0.3').attr('disabled', true);
plus.io.resolveLocalFileSystemURL(a, function(entry) {
var s = entry.toLocalURL();
data.result = true;
data.data = s;
my.upload(obj, com.urlCONFIG.post_upload, s, callback, photoType);
}, function(e) {
data.data = "读取拍照文件错误:" + e.message;
callback(data);
});
}, function(a) {}, {
filter: "image"
})
},
//上传图片到服务器
upload: function(obj, url, filename, callback, photoType) {
var my = this;
my.upTimer.count++;
var tips = com.msg(my.upTimer.count + '个文件正在上传', 999);
mui.plusReady(function() {

var task = plus.uploader.createUpload(url, {
method: "POST",
blocksize: 0,
priority: 100
},
function(t, status) {
$(obj).css('opacity', '1').removeAttr('disabled');
var json_data = t.responseText.replace('null(','').replace(');null','');
//console.log(json_data)
var data = JSON.parse(json_data);
if(data) {
// 上传完成
if(status == 200) {
my.upTimer.count--;
if(my.upTimer.count <= 0) {
com.msg('所有图片上传完毕', 1);
}
var model = _.clone(my._model);
model.id = new Date().getTime();
model.img = data.data;
my.upTimer.imgs.push(model);
callback(model);
} else {
callback(data);
}
} else {
com.msg('上传失败:' + data.msg, 0);
}
}
);
//图片压缩
my.resize(filename, function(zipSrc) {
task.addFile(zipSrc, {
key: "upload"
});
task.addData(zipSrc, zipSrc);
task.start();
});
});
},
//压缩(需要获取本地文件权限)
resize: function(src, callback) {
var filename = src.substring(src.lastIndexOf('/') + 1);
plus.zip.compressImage({
src: src,
dst: '_doc/' + filename,
overwrite: true,
width: '1000px', //这里指定了宽度,同样可以修改
format: 'jpg',
quality: 90 //图片质量不再修改,以免失真
},
function(e) {
callback(e.target);
},
function(err) {
com.alert('未知错误!', 0, function() {
mui.back();
})
})
}
};
})
\n
1 分享 关注
123321150@qq.com
legolas1989@qq.com

legolas1989@qq.com

dddd
0 赞 2019-01-08 18:52

要回复文章请先登录注册