/**
* Created by 熊竹 on 2017/3/15.
*/
Vue.component('crop-upload', {
template: '
' +
'
' +
'
' +
'
![]()
' +
'
' +
'
' +
'
' +
'
![]()
' +
'
' +
'取消' +
'确认' +
'
' +
'
' +
'
',
props: {
src: {
type: String,
default: ''
},
ratioX: {
type: Number,
default: 1
},
ratioY: {
type: Number,
default: 1
}
},
data: function () {
return {
showCropper: false,
raw: '',
croperHeight: 0,
croperWidth: 0
}
},
computed: {
height: function () {
return 180 + 'px';
},
width: function () {
return 180 * this.ratioX / this.ratioY + 'px';
},
cropperStyle: function () {
return {
height: this.croperHeight,
width: this.croperWidth,
maxWidth: '100px'
}
}
},
methods: {
fileChange: function (res) {
var reader = new FileReader();
reader.onload = function () {
var image = new Image();
image.onload = function () {
this.croperWidth = image.width;
this.croperHeight = image.height;
this.raw = image.src;
this.showCropper = true;
$('#cropper').Jcrop({
aspectRatio: 1,
allowSelect: false,
bgOpacity: 0.3,
setSelect: [0, 0, 100, 100],
onChange: function (res) {
console.log(res)
}.bind(this)
});
}.bind(this);
image.src = reader.result;
}.bind(this);
reader.readAsDataURL(this.$refs.input.files[0]);
},
chooseFile: function () {
this.$refs.input.click();
},
upload: function () {
var formData = new FormData();
// 建立一个upload表单项,值为上传的文件
formData.append('file', this.$refs.input.files[0]);
var xhr = new XMLHttpRequest();
xhr.open('POST', '../assets/uploadFile');
// 定义上传完成后的回调函数
xhr.onload = function () {
if (xhr.status === 200) {
var res = JSON.parse(xhr.response)
if (res.success) {
this.$emit('uploaded', res.data[0])
}
}
}.bind(this);
xhr.send(formData);
},
cancel: function () {
this.showCropper = false;
},
confirm: function () {
}
}
});