<template>
	<view >
		<view class="content">
			<view class="info-item">
				<view class="label">头像</view>
				<view class="right">
					<image  class="head"    :src="user.avatar==null?'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/fs/20240229/22cb9518a55040dea74d8f730551a7a2.jpg':user.avatar" mode=""></image>
					<button class="wx-head" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					</button>
				</view>
			</view>
			<view class="info-item">
				<view class="label">昵称</view>
				<view class="right">
					<input type="nickname"  @blur="bindblur" @input="bindinput" v-model="user.nickName" class="input" /> 
					<image class="image" src="/static/images/icon_edit.png" mode=""></image>
				</view>
			</view>
			 
			<view class="info-item">
				<view class="label">手机号</view>
				<view class="right">
					<text class="text" v-if="user!=null">{{$parsePhone(user.phone)}}</text>
					<image class="image" src="/static/images/icon_lock.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="btn-box">
			<view class="btn" @click="submit()">保存修改</view>
		</view>
	</view>
</template>

<script>
	import {getUserInfo,editUser} from '@/api/user.js'
	export default {
		data() {
			return {
				user:{
					avatar:null,
					phone:""
				}
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			bindblur(e) {
				this.user.nickName = e.detail.value; // 获取微信昵称
			},
			bindinput(e){
				this.user.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮，会出现修改不成功的情况。
			},
			onChooseAvatar(e){
				let {
					avatarUrl
				} = e.detail;
				uni.uploadFile({
					url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例，非真实的接口地址
					filePath: avatarUrl,
					name: 'file',
					formData: {
						'user': 'test'  // 上传附带参数
					},
					success: (uploadFileRes) => {
						this.user.avatar =JSON.parse(uploadFileRes.data).url
					}
				});
			},
			// chooseImage() {
			// 	var that = this;
			// 	uni.chooseImage({
			// 		count: 1, // 默认9
			// 		sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
			// 		sourceType: ['album', 'camera'], //从相册选择
			// 		success: (res) => {
			// 			uni.uploadFile({
			// 				url: uni.getStorageSync('requestPath')+'/api/common/uploadOSS', //仅为示例，非真实的接口地址
			// 				filePath: res.tempFilePaths[0],
			// 				name: 'file',
			// 				formData: {
			// 					'user': 'test'  // 上传附带参数
			// 				},
			// 				success: (uploadFileRes) => {
			// 					console.log(uploadFileRes)
			// 					this.user.avatar =JSON.parse(uploadFileRes.data).data.url
			// 				}
			// 			});
						 
			// 		}
			// 	});
			// },
			submit(){
				this.user.isWeixinAuth=1;
				editUser(this.user).then(
					res => {
						if(res.code==200){
							uni.setStorageSync('avatar',this.avatar);
							uni.setStorageSync('nickName',this.nickName);
							uni.showToast({
								icon:'success',
								title: "修改成功",
							});
							
							//修改IM个人信息
							
							this.getUserInfo()
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			getUserInfo(){
				getUserInfo().then(
					res => {
						if(res.code==200){
							if(res.user!=null){
								this.user=res.user;
							}
							else{
								this.utils.loginOut();
							}
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			 
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		padding-top: 20rpx;
		border-top: 1px solid #F5F6FA;
	}
	.info-item{
		height: 104upx;
		background: #FFFFFF;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #F5F6FA;
		&:last-child{
			border-bottom: none;
		}
		.label{
			min-width: 200rpx;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		.right{
			display: flex;
			align-items: center;
			justify-content: center;
			.text{
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
			.image{
				min-width: 30rpx;
				margin-left: 10upx;
				width: 30upx;
				height: 30upx;
			}
			.input{
				text-align: right;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
			.wx-head{
				position: absolute;
				margin-left: 10upx;
				width: 30upx;
				height: 30upx;
				opacity: 0;
			}
		}
		.head{
			border-radius: 50%;
			width: 80upx;
			height: 80upx;
		}
		.arrow{
			width: 30upx;
			height: 30upx;
		}
		.text{
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		.input{
			text-align: right;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		&.password{
			margin-top: 20upx;
			.right{
				display: flex;
				align-items: center;
				.text{
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #0F1826;
					margin-right: 15upx;
				}
			}
		}
	}
	.btn-box{
		height: 140upx;
		z-index: 9999;
		width: 100%;
		padding: 0rpx 30upx;
		position: fixed;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		.btn{
			 
			width: 100%;
			height: 88upx;
			line-height: 88upx;
			text-align: center;
			font-size: 34upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			background: #C39A58;
			border-radius: 10upx;
		}
	}
</style>
