feat: upload avatar!!!

This commit is contained in:
KUN1007 2023-10-16 01:22:32 +08:00
parent 5d271ac233
commit c639b81133
7 changed files with 41 additions and 15 deletions

View file

@ -94,6 +94,7 @@ export type UserInfoResponseData = KUNGalgameResponseData<UserInfo>
export type UserUpdateAvatarResponseData = KUNGalgameResponseData<{
avatar: string
avatarMin: string
}>
export type UserUpdateBioResponseData = KUNGalgameResponseData<{}>

View file

@ -1,26 +1,24 @@
<script setup lang="ts">
// Vue
import { defineAsyncComponent, ref } from 'vue'
//
import { Icon } from '@iconify/vue'
// css
import 'animate.css'
//
import { topBarItem } from './topBarItem'
import { onBeforeRouteLeave } from 'vue-router'
// hamburger
// hamburger
const Hamburger = defineAsyncComponent(() => import('./Hamburger.vue'))
//
//
const KUNGalgameSettingsPanel = defineAsyncComponent(
() => import('../setting-panel/KUNGalgameSettingPanel.vue')
)
//
//
const KUNGalgameUserInfo = defineAsyncComponent(
() => import('./KUNGalgameUserInfo.vue')
)
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
import { storeToRefs } from 'pinia'
const { avatarMin } = storeToRefs(useKUNGalgameUserStore())
//
const showKUNGalgamePanel = ref(false)
// hamburger
@ -91,7 +89,7 @@ onBeforeRouteLeave(() => {
<div class="avatar">
<img
@click="showKUNGalgameUserPanel = true"
src="../../assets/images/KUN.jpg"
:src="avatarMin"
alt="KUN"
/>
</div>

View file

@ -63,6 +63,7 @@ export const useKUNGalgameUserStore = defineStore({
uid: 0,
name: '',
avatar: '',
avatarMin: '',
moemoepoint: 0,
moemoeAccessToken: '',
roles: 0,
@ -80,6 +81,7 @@ export const useKUNGalgameUserStore = defineStore({
this.uid = uid
this.name = name
this.avatar = avatar
this.avatarMin = avatar.replace(/\.webp$/, '-77.webp')
this.moemoepoint = moemoepoint
this.roles = roles
},

View file

@ -3,6 +3,7 @@ export interface KUNGalgamerStore {
uid: number
name: string
avatar: string
avatarMin: string
moemoepoint: number
moemoeAccessToken: string
roles: number

View file

@ -38,7 +38,7 @@ onMounted(async () => {
<div class="root">
<!-- 信息区域容器 -->
<div class="container">
<Header :name="user?.name" :moemoepoint="user?.moemoepoint" />
<Header :moemoepoint="user?.moemoepoint" />
<!-- 内容区 -->
<div class="content">
<NavBar :uid="uid" />

View file

@ -4,6 +4,7 @@ import Message from '@/components/alert/Message'
//
import { checkImageValid, resizeImage } from '../utils/handleFileChange'
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
import { storeToRefs } from 'pinia'
//
const uploadedImage = ref<Blob>()
@ -11,6 +12,8 @@ const uploadedImage = ref<Blob>()
const selectedFileUrl = ref<string>('')
// input
const input = ref<HTMLElement>()
//
const { avatar, avatarMin } = storeToRefs(useKUNGalgameUserStore())
//
const uploadImage = async (file: File) => {
@ -71,7 +74,20 @@ const handleChangeAvatar = async () => {
console.log(uploadedImage.value)
await useKUNGalgameUserStore().updateAvatar(formData)
const res = await useKUNGalgameUserStore().updateAvatar(formData)
if (res.code === 200) {
//
const avatarLink = res.data.avatar
avatar.value = avatarLink
avatarMin.value = avatarLink.replace(/\.webp$/, '-77.webp')
//
selectedFileUrl.value = ''
Message('Update avatar successfully!', '更新头像成功', 'success')
} else {
Message('Update avatar failed!', '更新头像失败', 'error')
}
}
</script>

View file

@ -1,8 +1,11 @@
<script setup lang="ts">
import { computed } from 'vue'
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
import { storeToRefs } from 'pinia'
const { name, avatar } = storeToRefs(useKUNGalgameUserStore())
const props = defineProps<{
name?: string
moemoepoint?: number
}>()
@ -16,11 +19,11 @@ const mpWidth = computed(() => {
<div class="header">
<!-- 用户头像 -->
<div class="avatar">
<img src="@/assets/images/KUN.jpg" alt="KUN" />
<img v-if="avatar" :src="avatar" alt="KUN" />
</div>
<!-- 用户名 -->
<div class="name">
<span>{{ props.name }}</span>
<span>{{ name }}</span>
<span>KUNGalgame</span>
</div>
<!-- 用户萌萌点 -->
@ -49,10 +52,15 @@ const mpWidth = computed(() => {
position: absolute;
/* 头像的定位 */
top: 5px;
width: 140px;
height: 140px;
background-color: var(--kungalgame-trans-blue-0);
border-radius: 50%;
left: 50px;
z-index: 2;
img {
width: 140px;
height: 140px;
border-radius: 50%;
box-shadow: 0px 0px 2px 4px var(--kungalgame-trans-red-2);
&:hover {