Nuxt3实战全栈开发简书

实现用户信息修改功能

Preview
  • 用户中心功能实现

用户中心功能实现

//pages/user/settings
<template>
  <nav-bar></nav-bar>
  <a-row type="flex" justify="center" class="settings">
    <a-col :span="12">
      <a-row>
        <a-col :span="7" class="aside">
          <div class="user-link-item active">
            <i-mdi-list-box/>
             <span>基础设置</span>
          </div>
          <div class="user-link-item ">
            <i-mdi-account-box/>
            <span>个人资料</span>
          </div>
          <div class="user-link-item ">
            <i-mdi-alpha-b-box/>
            <span>黑名单</span>
          </div>
        </a-col>
        <a-col :span="17">
          <div class="setting-base">
            <div class="settings-list">
              <div class="item-line">
                <a-avatar :size="85">
                  <template #icon>
                    <img :src="user.avatar?user.avatar:'/images/default-avatar.png'" alt="avatar">
                  </template>
                </a-avatar>
              </div>
              <div>
<!--                <a-button  shape="round" ghost>更改头像</a-button>-->
                <a-upload
                    name="avatar"
                    :show-upload-list="false"
                    :before-upload="beforeUpload"
                    @change="handleChange"
                >
                  <a-button  shape="round" ghost>更改头像</a-button>
                </a-upload>
              </div>
            </div>
            <div class="settings-list border-b">
              <div class="item-line">
                <span>昵称</span>
              </div>
              <div class="nickname">
                <a-input v-model:value="user.nickname" style="background-color: #F7F7F7;" :bordered="false" placeholder="输入昵称"></a-input>
              </div>
            </div>
            <div class="settings-list border-b">
              <div class="item-line">
                <span>电子邮件</span>
              </div>
              <div >
                71*****47@qq.com
              </div>
            </div>
            <div class="settings-list border-b">
              <div class="item-line">
                <span>手机</span>
              </div>
              <div >
                152****1073
              </div>
            </div>
            <div class="save-btn">
              <a-button  @click="editUser" shape="round" ghost>保存</a-button>
            </div>
          </div>
        </a-col>
      </a-row>
    </a-col>
  </a-row>
</template>

<script setup>
import {uploadCosFetch, userFetch} from "~/composables/useHttpFetch";

const user = ref({})
 user.value = useUserInfo().value
console.log('useUserInfo().value',useUserInfo().value)
const { $message } = useNuxtApp()
//修改头像
const beforeUpload = (file) => {
  console.log('file',file)
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' ||  file.type === 'image/jpg';
  if (!isJpgOrPng) {
    $message.error('请选择正确的图片类型!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    $message.error('图片大小不能超过2MB!');
  }
  const formData = new FormData();
  formData.append('avatar',file);
  uploadCosFetch({
    method: 'POST',
    body: formData,
    key:'uploadCosFetch'
  }).then(({data}) => {
    if (data.value.code === 1) {
      $message.error(data.value.msg)
    }
    $message.success('头像上传成功')
    user.value.avatar = data.value.data.avatarUrl
    const userInfo = useCookie('userInfo',{maxAge:60*60*24*7})
    userInfo.value.avatar = user.value.avatar
  })

};


const handleChange = (info) => {
  //console.log('handleChange',info.file)
}
//修改资料
const editUser = () => {
  userFetch({
    method:'PUT',
    body:{
      nickname:user.value.nickname
    },
    key:'editUser',
    server:false
  }).then(({data}) => {
    if (data.value.code === 1) {
      $message.error(data.value.msg)
      return
    }
    $message.success(data.value.msg)
    const userInfo = useCookie('userInfo',{maxAge:60*60*24*7})
    userInfo.value.nickname = user.value.nickname
  })
}
</script>

<style lang="scss" scoped>
.settings{
  padding-top: 80px;
  .aside{
    margin-top: 20px;
    .active{
      background-color: #F0F0F0;
      border-radius: 5px;
    }
    .user-link-item{
      padding: 5px 10px;
      display: flex;
      align-items: center;
      &:hover{
        background-color: #F0F0F0;
        border-radius: 5px;
      }
      svg{
        color: #A0A0A0;
        font-size: 32px;
      }
      span{
        font-size: 16px;
        margin-left: 12px;
        color: #333333;
      }
    }
  }
  .setting-base{
    margin-left: 50px;
    .border-b{
      border-bottom: 1px #F0F0F0 solid;
    }
    .settings-list{
      display: flex;
      align-items: center;
      padding: 20px 0;
      .item-line{
        width: 150px;
        span{
          font-size: 15px;
          color: #969696;
        }
      }
      .nickname{
        background-color: #F7F7F7; border: 1px solid #c8c8c8;border-radius: 6px;padding: 2px;
      }

    }
  }
}
</style>
<style>
.settings-list .ant-btn.ant-btn-background-ghost {
  color: #42C02E;
  border-color: #42C02E;
  height: 36px;
}
.save-btn .ant-btn.ant-btn-background-ghost{
  margin-top: 30px;
  color: #ffffff;
  border-color: #42C02E;
  background-color: #00BB29;
  height: 32px;

}
</style>