Skip to content

Commit 69fe4d1

Browse files
committed
refine imageCropper fetch
1 parent 0476f25 commit 69fe4d1

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

src/components/ImageCropper/index.vue

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<script>
106106
/* eslint-disable */
107107
import {effectRipple, data2blob} from './utils';
108+
import fetch from 'utils/fetch';
108109
import langBag from './lang';
109110
const mimes = {
110111
'jpg': 'image/jpeg',
@@ -659,7 +660,8 @@
659660
})
660661
}
661662
// 监听进度回调
662-
const uploadProgress = function(event) {
663+
function uploadProgress (event) {
664+
console.log(event)
663665
if (event.lengthComputable) {
664666
that.progress = 100 * Math.round(event.loaded) / event.total;
665667
}
@@ -669,43 +671,21 @@
669671
that.loading = 1;
670672
that.setStep(3);
671673
that.$emit('crop-success', createImgUrl, field, ki);
672-
new Promise(function(resolve, reject) {
673-
let client = new XMLHttpRequest();
674-
client.open('POST', url, true);
675-
client.onreadystatechange = function() {
676-
if (this.readyState !== 4) {
677-
return;
678-
}
679-
if (this.status === 200) {
680-
resolve(JSON.parse(this.responseText));
681-
} else {
682-
reject(this.status);
683-
}
684-
};
685-
client.upload.addEventListener("progress", uploadProgress, false); //监听进度
686-
// 设置header
687-
if (typeof headers == 'object' && headers) {
688-
Object.keys(headers).forEach((k) => {
689-
client.setRequestHeader(k, headers[k]);
690-
})
691-
}
692-
client.send(fmData);
693-
}).then(
694-
// 上传成功
695-
function(resData) {
696-
that.loading = 2;
697-
that.$emit('crop-upload-success', resData);
698-
},
699-
// 上传失败
700-
function(sts) {
701-
if (that.value) {
674+
fetch({
675+
url,
676+
method: 'post',
677+
data: fmData
678+
}).then(resData=>{
679+
that.loading = 2;
680+
that.$emit('crop-upload-success', resData.data);
681+
}).catch(err=>{
682+
if (that.value) {
702683
that.loading = 3;
703684
that.hasError = true;
704685
that.errorMsg = lang.fail;
705-
that.$emit('crop-upload-fail', sts, field, ki);
686+
that.$emit('crop-upload-fail', err, field, ki);
706687
}
707-
}
708-
);
688+
});
709689
}
710690
}
711691
}

0 commit comments

Comments
 (0)