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,24 +69,23 @@ async function handleUploadImage(event: any) { ...@@ -66,24 +69,23 @@ async function handleUploadImage(event: any) {
return return
} }
const URL = window.URL || window.webkitURL if (imageCropperRef.value && file) {
const img = new Image() const URL = window.URL || window.webkitURL
img.src = URL.createObjectURL(file) imageCropperRef.value.cropImage(URL.createObjectURL(file)).then(async (file) => {
const formData = new FormData()
formData.append('file', file)
img.onload = async function () { uploadLoading.value = true
const formData = new FormData()
formData.append('file', file)
uploadLoading.value = true const res = await fetchUpload(formData).finally(() => {
uploadLoading.value = false
event.target.value = null
})
const res = await fetchUpload(formData).finally(() => { if (res.code === 0) {
uploadLoading.value = false uploadImageUrl.value = res.data as string
event.target.value = null }
}) })
if (res.code === 0) {
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