Commit c494f7af authored by nick zheng's avatar nick zheng

chore: 应用更新头像支持裁切

parent b9b62616
<script setup lang="ts"> <script setup lang="ts">
import { computed, ref, useSlots } from 'vue' import { computed, ref, useSlots, useTemplateRef } from 'vue'
import CustomIcon from '../custom-icon/custom-icon.vue' import CustomIcon from '../custom-icon/custom-icon.vue'
import { fetchUpload } from '@/apis/upload' import { fetchUpload } from '@/apis/upload'
import ImageCropper from '@/components/image-cropper/image-cropper.vue'
interface Emit { interface Emit {
(e: 'formatError'): void (e: 'formatError'): void
...@@ -20,7 +21,7 @@ const props = defineProps({ ...@@ -20,7 +21,7 @@ const props = defineProps({
}, },
listType: { listType: {
type: Array, type: Array,
default: () => ['jpg', 'png', 'jpeg', 'svg', 'gif'], default: () => ['jpg', 'png', 'jpeg', 'gif'],
}, },
width: { width: {
type: Number, type: Number,
...@@ -36,6 +37,8 @@ const emit = defineEmits<Emit>() ...@@ -36,6 +37,8 @@ const emit = defineEmits<Emit>()
const slots = useSlots() const slots = useSlots()
const imageCropperRef = useTemplateRef<InstanceType<typeof ImageCropper> | null>('imageCropperRef')
const uploadLoading = ref(false) const uploadLoading = ref(false)
const isShowImageMask = ref(false) const isShowImageMask = ref(false)
...@@ -66,11 +69,9 @@ async function handleUploadImage(event: any) { ...@@ -66,11 +69,9 @@ async function handleUploadImage(event: any) {
return return
} }
if (imageCropperRef.value && file) {
const URL = window.URL || window.webkitURL const URL = window.URL || window.webkitURL
const img = new Image() imageCropperRef.value.cropImage(URL.createObjectURL(file)).then(async (file) => {
img.src = URL.createObjectURL(file)
img.onload = async function () {
const formData = new FormData() const formData = new FormData()
formData.append('file', file) formData.append('file', file)
...@@ -84,6 +85,7 @@ async function handleUploadImage(event: any) { ...@@ -84,6 +85,7 @@ async function handleUploadImage(event: any) {
if (res.code === 0) { if (res.code === 0) {
uploadImageUrl.value = res.data as string uploadImageUrl.value = res.data as string
} }
})
} }
} }
</script> </script>
...@@ -120,5 +122,7 @@ async function handleUploadImage(event: any) { ...@@ -120,5 +122,7 @@ async function handleUploadImage(event: any) {
<input id="upload" type="file" :accept="uploadImageType" class="hidden" @change="handleUploadImage" /> <input id="upload" type="file" :accept="uploadImageType" class="hidden" @change="handleUploadImage" />
</div> </div>
<ImageCropper ref="imageCropperRef" />
</div> </div>
</template> </template>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment