上传头像裁剪

This commit is contained in:
X1627315083@163.com
2026-03-27 09:36:21 +08:00
parent 41820a2f9d
commit 98d92cbf30
12 changed files with 801 additions and 10 deletions

View File

@@ -31,6 +31,7 @@
<div class="label">{{ $t('Home.logoutDevice') }}</div>
<button class="logout-btn" @click="logout">{{ $t('Home.logout') }}</button>
</div>
<clip-dialog ref="clipDialogRef" />
</template>
<script setup lang="ts">
@@ -41,6 +42,8 @@
import { useI18n } from 'vue-i18n'
import { uploadImage } from '@/api/upload'
import { UpdateUserAvatar, getAvatarLimit } from '@/api/user'
import clipDialog from '@/components/clipDialog.vue'
import { fileToBase64, base64Tofile } from '../../../components/Canvas/tools/tools'
const router = useRouter()
const { locale } = useI18n()
const userInfoStore = useUserInfoStore()
@@ -50,6 +53,7 @@
{ label: '中文', value: 'CHINESE_SIMPLIFIED' }
])
const remainingNum = ref(0)
const clipDialogRef = ref<typeof clipDialog>()
const changeLang = (value: string) => {
locale.value = value
localStorage.setItem('language', value)
@@ -72,13 +76,17 @@
const input = document.createElement('input')
input.type = 'file'
input.accept = 'image/png, image/jpeg, image/jpg'
input.addEventListener('change', (e) => {
input.addEventListener('change', async (e) => {
const file = e.target.files[0]
const formData = new FormData()
formData.append('avatar', file)
UpdateUserAvatar(formData).then((res) => {
userInfoStore.updateUserInfo({avatar: res})
getAvatarLimitNum()
let base64Img = await fileToBase64(file)
clipDialogRef.value?.open(base64Img).then((base64: string)=>{
const fileData = base64Tofile(base64,file.name)
const formData = new FormData()
formData.append('avatar', fileData)
UpdateUserAvatar(formData).then((res) => {
userInfoStore.updateUserInfo({avatar: res})
getAvatarLimitNum()
})
})
})
input.click()