先看全部代码
<template>
<div>
<el-page-header @back="goBack" :icon="null" title="企业门户网站管理系统">
<!-- 插槽 -->
<template #content>
<span class="text-large font-600 mr-3"> 个人中心 </span>
</template>
</el-page-header>
<el-row :gutter="20" class="el-row">
<el-col :span="8">
<el-card class="box-card">
<el-avatar :size="100" :src="avatarUrl" />
<h1>{{ store.state.userInfo.username }}</h1>
<h5>{{ store.state.userInfo.role === 1 ? '管理员' : '编辑' }}</h5>
</el-card>
</el-col>
<el-col :span="16">
<el-card>
<template #header>
<div class="card-header">
<span>个人信息</span>
</div>
</template>
<el-form ref="userFormRef" style="max-width: 600px" :model="userForm" :rules="userFormRules"
label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
<el-form-item label="用户名" prop="username">
<el-input v-model="userForm.username" />
</el-form-item>
<el-form-item label="性别">
<el-select v-model="userForm.gender" placeholder="请选择">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="个人简介" prop="introduction">
<el-input type="textarea" v-model="userForm.introduction" />
</el-form-item>
<!-- 点击上传图片文件部分 -->
<el-form-item label="头像" prop="avatar">
<!-- <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:show-file-list="false" :on-change="handleChange" :auto-upload="false">
<img v-if="userForm.avatar" :src="uploadAvatar" class="avatar" />
<el-icon v-else class="avatar-uploader-icon">
<Plus />
</el-icon>
</el-upload> -->
<!-- 封装的上传图片组件 -->
<Upload :avatar="userForm.avatar" @xmyChange="handleChange2"></Upload>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm()">更新</el-button>
</el-form-item>
</el-form>
</el-card>
</el-col>
</el-row>
</div>
</template>
<script setup>
import { useStore } from "vuex";
import { computed, ref, reactive } from "vue";
const store = useStore()
import axios from "axios"
import { ElMessage } from "element-plus";
console.log("store", store.state);
// 引用
import upload from "../../util/upload.js"
// 引用
import Upload from "@/components/upload/Upload.vue"
// 头像计算属性
const avatarUrl = computed(() => store.state.userInfo.avatar ? 'http://localhost:3000' + store.state.userInfo.avatar : `https://ts1.cn.mm.bing.net/th/id/R-C.fd81516a06ce33c15b194494272fa6e9?rik=XAfnJ6A9NFvAyA&riu=http%3a%2f%2fimg.touxiangwu.com%2fuploads%2fallimg%2f2022053117%2fivhiashhpu1.jpg&ehk=Yi2aDhWvd0rnBKl1xloJy8F1RfGd8%2bcC75k4ff8dVXk%3d&risl=&pid=ImgRaw&r=0`)
// 上传表单的头像计算属性
// const uploadAvatar = computed(() => userForm.avatar.includes("blob") ? userForm.avatar : 'http://localhost:3000' + userForm.avatar)
// 表单
const { username, gender, introduction, avatar } = store.state.userInfo
const userFormRef = ref()
const userForm = reactive({
username,
gender,
introduction,
avatar,
file: null
})
// 规则
const userFormRules = reactive({
username: [
{ required: true, message: '请输入名字', trigger: 'blur' },
],
gender: [
{ required: true, message: '请选择性别', trigger: 'blur' },
],
introduction: [
{ required: true, message: '请输入简介', trigger: 'blur' },
],
avatar: [
{ required: true, message: '请上传头像', trigger: 'blur' },
],
})
// 性别选择
const options = [{
value: 0,
label: '保密',
},
{
value: 1,
label: '男',
},
{
value: 2,
label: '女',
},]
// 每次选择完图片之后的回调
// const handleChange = (file) => {
// console.log(file);
// userForm.avatar = URL.createObjectURL(file.raw)
// userForm.file = file.raw
// };
const handleChange2 = (file) => {
console.log(file);
userForm.avatar = URL.createObjectURL(file)
userForm.file = file
}
// 未封装upload.js 上传版本
// const submitForm = () => {
// userFormRef.value.validate((valid) => {
// if (valid) {
// console.log("霓虹", userForm);
// // 有文件上传的话需要变成formdata这种形式的
// 以下是处理数据为formdata形式的代码
// const params = new FormData()
// for (let i in userForm) {
// params.append(i, userForm[i])
// }
// console.log(params);
// 发起请求
// axios.post("/adminapi/user/upload", params, {
// headers: {
// "Content-Type": "multipart/form-data"
// }
// }).then(res => {
// console.log(res.data, "res.data");
// if (res.data.ActionType === "Ok") {
// store.commit("changeUserInfo", res.data.data)
// ElMessage.success("更新成功")
// }
// })
// }
// })
// }
// 封装 upload.js 上传版本
const submitForm = () => {
userFormRef.value.validate(async (valid) => {
// valid 如果为true的话 则执行下面代码
if (valid) {
console.log("霓虹", userForm);
// 有文件上传的话需要变成formdata这种形式的
const res = await upload("/adminapi/user/upload", userForm)
if (res.ActionType === "Ok") {
store.commit("changeUserInfo", res.data)
ElMessage.success("更新成功")
}
}
})
}
</script>
<style lang="scss" scoped>
.el-row {
margin-top: 20px;
}
.box-card {
text-align: center;
}
h3 {
margin-bottom: 6px;
}
h3,
h5 {
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif
}
</style>
<style scoped>
.avatar-uploader .avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
border: 1px dashed var(--el-border-color);
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
text-align: center;
}
</style>