<template>
	<view >
		<view class="content">
			<view class="info-item">
				<view class="item">
					<image  class="head"    :src="user.avatar==null?'https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/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="item">
					<input type="nickname"  @blur="bindblur" @input="bindinput" placeholder="请点击授权昵称" v-model="nickName" class="input" /> 
				</view>
			</view>
			<view class="btn-box">
				<view class="btn" @click="submit()">保存</view>
			</view>
		</view>
		 
	</view>
</template>

<script>
	import {getUserInfo,editUser} from '@/api/user.js'
	export default {
		data() {
			return {
				user:{
					nickName:"",
					avatar:null,
					user:null,
				}
			}
		},
		onLoad() {
			
		},
		methods: {
			bindblur(e) {
				this.nickName = e.detail.value; // 获取微信昵称
			},
			bindinput(e){
				this.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(){
				if(this.nickName==null||this.nickName==""){
					uni.showToast({
						icon:'none',
						title: "请输入昵称",
					});
					return;
				}
				this.user.nickName=this.nickName;
				this.user.isWeixinAuth=1;
				editUser(this.user).then(
					res => {
						if(res.code==200){
							uni.setStorageSync('avatar',this.avatar);
							uni.setStorageSync('nickName',this.nickName);
							this.$emit('updateUser');
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			getUserInfo(){
				getUserInfo().then(
					res => {
						if(res.code==200){
							if(res.user!=null){
								this.user=res.user;
								console.log(this.user)
							}
							else{
								this.utils.loginOut();
							}
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			 
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		width: 100%;
		padding: 0 60upx;
		z-index: 100;
	}
	.info-item{
		width: 100%;
		height: 104upx;
		background: #FFFFFF;
		
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 15rpx 0rpx;
		.item{
			
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			.input{
				width: 100%;
				height: 50rpx;
				padding: 15rpx 30rpx;
				border-radius: 30rpx;
				background-color: #F5F6FA;
				text-align: left;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #0F1826;
			}
			.wx-head{
				position: absolute;
				width: 120upx;
				height: 120upx;
				opacity: 0;
			}
		}
		.head{
			border-radius: 50%;
			width: 120upx;
			height: 120upx;
		}
		 
		.input{
			text-align: right;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #0F1826;
		}
		 
	}
	.btn-box{
		height: 140upx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		.btn{
			width: 100%;
			height: 80upx;
			line-height: 80upx;
			text-align: center;
			font-size: 34upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			background: #078C9D;
			border-radius: 60upx;
		}
	}
</style>
