UpdatePhoto.vue
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<div class="updatePhoto_container">
<NafmHeader :title="$t('title.photo')" :showBack="true" navClass="nafmii-head-bg3" @onBack="$emit('close')">
<template slot="right">
<span class="finish" @click="handleSave">{{$t('button.save')}}</span>
</template>
</NafmHeader>
<img :src="image" ref="image">
</div>
</template>
<script>
import 'cropperjs/dist/cropper.css';
import Cropper from 'cropperjs';
export default {
name: 'UpdataPhoto',
props: {
file: {
required: true
}
},
data() {
return {
image: window.URL.createObjectURL(this.file),
cropper: null,
}
},
mounted() {
const image = this.$refs.image;
this.cropper = new Cropper(image, {
viewMode: 1,
dragMode: 'move',
aspectRatio: 1,
cropBoxMovable: false,
cropBoxResizable: false,
background: false,
movable: true
});
},
methods: {
async getCroppedCanvas() {
return new Promise(resolve => {
this.cropper.getCroppedCanvas().toBlob((file) => {
resolve(file)
})
})
},
async handleSave() {
// 图片上传 格式Content-Type: multipart/form-data,则一定要提交FormatData对象
const fd = new FormData()
// fd.append('photo', this.file)
const file = await this.getCroppedCanvas()
fd.append('photo', file)
// api 调用 todo
// 更新父组件
this.$emit('upDataPhoto', window.URL.createObjectURL(file))
// 关闭弹出层
this.$emit('close')
// 保存成功
Toast('保存成功')
}
}
}
</script>
<style lang="less" scoped>
.updatePhoto_container {
height: 100vh;
}
/deep/ .cropper-view-box {
border-radius: 50%;
}
</style>