微信小程序头像,昵称调整

This commit is contained in:
xinjie 2022-11-21 15:52:00 +08:00
parent c38c46c09f
commit af92e6209c
1 changed files with 64 additions and 29 deletions

View File

@ -1,13 +1,20 @@
<template> <template>
<!-- Main Start --> <!-- Main Start -->
<!-- 头部修改头像 --> <!-- 头部修改头像 -->
<view class="header bg-white pt-[30rpx]"> <view class="header bg-white py-[30rpx]">
<view class="flex justify-center"> <view class="flex">
<image @click="uploaderAvatar" :src="userInfo?.avatar"></image> <button
class="flex flex-col items-center after:border-0"
hover-class="none"
open-type="chooseAvatar"
style="background-color: transparent"
@click="chooseAvatar"
@chooseavatar="chooseAvatar"
>
<image :src="userInfo?.avatar"></image>
<view class="mt-[10rpx] text-center text-muted text-xs"> 点击修改头像 </view>
</button>
</view> </view>
<view class="mt-[20rpx] text-center text-muted text-xs" @click="uploaderAvatar"
>点击修改头像</view
>
</view> </view>
<!-- 用户ID --> <!-- 用户ID -->
@ -76,20 +83,36 @@
</view> </view>
<!-- 昵称修改组件 --> <!-- 昵称修改组件 -->
<u-popup v-model="showNickName" :closeable="true" mode="center" border-radius="20"> <u-popup
v-model="showNickName"
:closeable="true"
mode="center"
:maskCloseAble="false"
border-radius="20"
>
<view class="px-[50rpx] py-[40rpx] bg-white" style="width: 85vw"> <view class="px-[50rpx] py-[40rpx] bg-white" style="width: 85vw">
<view class="mb-[70rpx] text-xl text-center">修改昵称</view> <form @submit="changeNameConfirm">
<u-form-item borderBottom> <view class="mb-[70rpx] text-xl text-center">修改昵称</view>
<u-input <u-form-item borderBottom>
class="flex-1" <input
v-model="newNickname" class="nr h-[60rpx] w-full"
placeholder="请输入昵称" :value="userInfo.nickname"
:border="false" name="nickname"
/> type="nickname"
</u-form-item> placeholder="请输入昵称"
<view class="mt-[80rpx]"> />
<u-button @click="changeNameConfirm" type="primary" shape="circle"> 确定 </u-button> </u-form-item>
</view> <view class="mt-[80rpx]">
<button
class="bg-primary text-white w-full h-[80rpx] !text-lg !leading-[80rpx] rounded-full"
form-type="submit"
hover-class="none"
size="mini"
>
确定
</button>
</view>
</form>
</view> </view>
</u-popup> </u-popup>
@ -171,7 +194,7 @@ import { FieldType, SMSEnum } from '@/enums/appEnums'
import { uploadFile } from '@/utils/util' import { uploadFile } from '@/utils/util'
// //
const userInfo = ref<any | null>(null) const userInfo = ref<any>({})
// //
const fieldType = ref(FieldType.NONE) const fieldType = ref(FieldType.NONE)
// //
@ -244,11 +267,18 @@ const setUserInfoFun = async (value: string): Promise<void> => {
} }
// //
const uploaderAvatar = () => { const chooseAvatar = (e: any) => {
fieldType.value = FieldType.AVATAR fieldType.value = FieldType.AVATAR
// #ifndef MP-WEIXIN
uni.navigateTo({ uni.navigateTo({
url: '/uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-cropper?destWidth=300&rectWidth=200&fileType=jpg' url: '/uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-cropper?destWidth=300&rectWidth=200&fileType=jpg'
}) })
// #endif
// #ifdef MP-WEIXIN
if (e.detail.avatarUrl) {
uploadAvatar(e.detail.avatarUrl)
}
// #endif
} }
// //
@ -264,22 +294,24 @@ const changeSexConfirm = (value) => {
} }
// //
const changeUserNameConfirm = () => { const changeUserNameConfirm = async () => {
if (newUsername.value == '') return uni.$u.toast('账号不能为空') if (newUsername.value == '') return uni.$u.toast('账号不能为空')
if (newUsername.value.length > 10) return uni.$u.toast('账号长度不得超过十位数') if (newUsername.value.length > 10) return uni.$u.toast('账号长度不得超过十位数')
fieldType.value = FieldType.USERNAME fieldType.value = FieldType.USERNAME
setUserInfoFun(newUsername.value) await setUserInfoFun(newUsername.value)
showUserName.value = false showUserName.value = false
} }
// //
const changeNameConfirm = () => { const changeNameConfirm = async (e: any) => {
newNickname.value = e.detail.value.nickname
if (newNickname.value == '') return uni.$u.toast('昵称不能为空') if (newNickname.value == '') return uni.$u.toast('昵称不能为空')
if (newNickname.value.length > 10) return uni.$u.toast('昵称长度不得超过十位数') if (newNickname.value.length > 10) return uni.$u.toast('昵称长度不得超过十位数')
showNickName.value = false
fieldType.value = FieldType.NICKNAME fieldType.value = FieldType.NICKNAME
setUserInfoFun(newNickname.value) await setUserInfoFun(newNickname.value)
showNickName.value = false
} }
// //
@ -305,8 +337,7 @@ const goPage = (url: string) => {
}) })
} }
// const uploadAvatar = (path: string) => {
uni.$on('uAvatarCropper', (path) => {
uni.showLoading({ uni.showLoading({
title: '正在上传中...', title: '正在上传中...',
mask: true mask: true
@ -320,6 +351,11 @@ uni.$on('uAvatarCropper', (path) => {
uni.hideLoading() uni.hideLoading()
uni.$u.toast('上传失败') uni.$u.toast('上传失败')
}) })
}
//
uni.$on('uAvatarCropper', (path) => {
uploadAvatar(path)
}) })
onShow(async () => { onShow(async () => {
@ -334,7 +370,6 @@ onUnload(() => {
<style lang="scss"> <style lang="scss">
.header { .header {
width: 100%; width: 100%;
height: 240rpx;
image { image {
width: 120rpx; width: 120rpx;