From 15a55f79a84e5dab670d54a9dc0c6b9133a18441 Mon Sep 17 00:00:00 2001
From: Liuyi <candymxq888@outlook.com>
Date: 星期一, 21 十月 2024 17:29:37 +0800
Subject: [PATCH] 添加更改用户头像,添加故障上报校验

---
 pages/userInfo/index.vue |   78 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/pages/userInfo/index.vue b/pages/userInfo/index.vue
index b6e4b2e..90d1241 100644
--- a/pages/userInfo/index.vue
+++ b/pages/userInfo/index.vue
@@ -1,14 +1,66 @@
 <script setup>
 	import { onMounted, ref } from 'vue'
+	import { BASE_URL } from '../../config/baseUrl';
 	import { editUserInfoApi,getUserInfo } from '../../api/index.js'
 	const isEdit = ref(false)
 	const editForm = ref({id:'',userName:'',userPhone:''})
 	const userInfo = ref(JSON.parse(uni.getStorageSync('userInfo')))
+	//修改用户头像
+	function changeImg(){
+		uni.chooseImage({
+			success: async(res) =>{
+				 //建立表单,请求参数传表单形式
+				 let fileForm = {
+					 file1:res.tempFiles,
+					 path:'qingyuan'
+				 }
+				uni.uploadFile({
+					url:BASE_URL+ '/file/upload', 
+					filePath:res.tempFilePaths[0],
+					name: 'file',
+					formData:fileForm,
+					success: async(success) => {
+						console.log('success',success);
+						let dataObj = JSON.parse(success.data)
+						let headImgStatic =  BASE_URL + '/upload' + dataObj.data.newFileName
+						await editUserInfoApi({id:userInfo.value.id,headImg:headImgStatic}).then(async(res) =>{
+							if(res.code == 200){
+								uni.showToast({
+									title: '修改成功!',
+									duration: 1000,
+								});
+								await getUserInfo().then((res) =>{
+									userInfo.value.headImg = res.data.userInfo.headImg
+									uni.setStorageSync('userInfo',JSON.stringify(userInfo.value))
+								})
+							}else{
+								uni.showToast({
+									title: '网络错误!请重试',
+									duration: 1000,
+									icon:'none'
+								});
+							}
+						})
+					},
+					fail: (err) => {
+						uni.showToast({
+							title: '上传失败,请重试',
+							duration: 2000,
+							icon:'none'
+						});
+						console.log('err',err,res.tempFilePaths[0])
+					}
+				});
+			}
+		});
+	}
+	//修改用户信息
 	function edit(){
 		editForm.value.id = userInfo.value.id
 		editForm.value.userName = userInfo.value.userName
 		editForm.value.userPhone = userInfo.value.userPhone
 		isEdit.value = true
+		console.log('userInfo',userInfo.value)
 	}
 	function cancel(){
 		isEdit.value = false
@@ -29,7 +81,7 @@
 				})
 			}else{
 				uni.showToast({
-					title: '请求错误!请重试',
+					title: '网络错误!请重试',
 					duration: 2000,
 					icon:'none'
 				});
@@ -47,7 +99,11 @@
 				<view class="info-up">
 					<view class="info-img">
 						<text>头像</text>
-						<image src='../../static/images/index/head.png' alt=''></image>
+						<view class="img-box" @click="changeImg">
+							<image v-if="userInfo.headImg" :src='userInfo.headImg' alt=''></image>
+							<image v-else src='../../static/images/index/head.png' alt=''></image>
+							<image src='../../static/images/other/more.png' alt=''></image>
+						</view>
 					</view>
 					<view class="info-name">
 						<text>用户名称</text>
@@ -131,10 +187,20 @@
 						display: flex;
 						justify-content:space-between;
 						align-items:center;
-						image{
-							width:100rpx;
-							height:100rpx;
-							border-radius: 50rpx;
+						.img-box{
+							display: flex;
+							align-items: center;
+							image:first-child{
+								width:100rpx;
+								height:100rpx;
+								border-radius: 50rpx;
+								margin-right:10rpx;
+							}
+							image:last-child{
+								width:30rpx;
+								height:36rpx;
+								border-radius: 50rpx;
+							}
 						}
 					}
 					.info-name{

--
Gitblit v1.9.3