<template>
	<view class="login">
		<view class="img">
			<image src="https://t9.9026.com/imgs/bg_1.jpg" mode="scaleToFill"></image>

		</view>
		<view class="title">
			<view class="line"></view>
			<view class="logintype"><text>登录方式</text></view>
			<view class="line"></view>
		</view>
		<view class="typebtn">
			<view class="item" @click="goYouyue">
				<image src="/static/icon/youyue.png" mode="scaleToFill"></image>
				<text>加入优悦会</text>
			</view>
			<view class="item" @click="getmsg">
				<image src="/static/icon/wechart.png" mode="scaleToFill"></image>
				<text v-if="!isLoginOff">微信登录</text>
				<text v-if="isLoginOff">授权登录</text>
			</view>
		</view>

		<u-modal @close="closeMask" closeOnClickOverlay="true" :show="modal.show" :title="modal.title"
			:show-confirm-button="false">
			<view>
				<button class="avatar" open-type="chooseAvatar" @chooseavatar="handleChooseAvatar">
					<image class="user-avatar" style="height: 176rpx;width: 176rpx;border-radius: 50%;"
						:src="modal.avatar?modal.avatar:'/static/icon/avatar.png'" />
				</button>
				<input class="avatar" type="nickname" :value="modal.nickname" placeholder="填写昵称"
					@change="handleChangeNickname">
				<button class="confirm" @click="handleConfirmWechatUserInfo">提交</button>
			</view>
		</u-modal>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgBase64:'',
				isLoginOff: false,
				modal: {
					show: false,
					title: '用户信息获取',
					nickname: '',
					avatar: ''
				},
				code: '',
				encryptedData: '',
				iv: '',
				data: {}
			}
		},
		onLoad() {
			this.isLoginOff = this.$store.getters.userInfo
		},
		methods: {
			//关闭遮罩层
			closeMask() {
				this.modal.show = false
			},
			//提交
			handleConfirmWechatUserInfo() {
				if (!this.modal.avatar) {
					this.$u.toast('请上传头像')
					return
				}
				if (!this.modal.nickname) {
					this.$u.toast('请填写昵称')
					return
				}
				this.$loading('数据提交中...')
				this.$api.my.update({
					avatar: this.modal.avatar,
					nickname: this.modal.nickname
				}).then(res => {
					this.$hideLoading()
					this.$store.dispatch('user/info', res.data)
					this.modal.show = false
					uni.switchTab({
						url: '/pages/my/my'
					})
				})
			},
			handleGetWechatUserInfo() {
				this.modal.show = true
			},
			// 获取头像
			handleChooseAvatar(e) {
				console.log(e.detail, '------>e.detail');
				this.modal.avatar = e.detail.avatarUrl
				uni.getFileSystemManager().readFile({
					filePath: e.detail.avatarUrl, //选择图片返回的相对路径
					encoding: 'base64', //编码格式
					success: res => { //成功的回调
						console.log(res);
						this.imgBase64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符，在页面无法显示的哦
						this.$api.my.UserUploadFile({
							file:this.imgBase64 
						}).then(res=>{
							console.log(res.data,'------->res.data');
						})
						
					},
					fail: (e) => {
						console.log("图片转换失败");
					}
				})
			},
			//获取昵称
			handleChangeNickname(res) {
				this.modal.nickname = res.detail.value
			},
			// 加入优悦会
			goYouyue() {
				uni.navigateTo({
					url: '/pages/login/youyue'
				})
			},
			getCode() {
				return new Promise((resolve, reject) => {
					uni.getUserInfo({
						success: loginRes => {
							this.data.encryptData = loginRes.encryptedData,
								this.data.iv = loginRes.iv
							resolve(this.data)
						}
					})
				})
			},
			//获取微信登录的code码
			getmsg() {
				if (this.isLoginOff) {
					uni.switchTab({
						url: '/pages/my/my'
					})
				} else {
					uni.login({
						provider: uni.$u.platform,
						success: res => {
							this.getCode().then((data) => {
								const params = {
									code: res.code,
									iv: data.iv,
									encryptData: data.encryptData
								}
								console.log(params);
								this.$api.my.myLogin(params).then(res => {
									let {
										token
									} = res.data
									this.$store.dispatch('user/token', token)
									this.handleGetWechatUserInfo()
									this.handleConfirmWechatUserInfo()
								})
							})
						}
					})
				}

			},

		}
	}
</script>

<style lang="scss" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;

	.login {
		height: 100%;
		background: $bgColor;
		box-sizing: border-box;
	}

	.avatar {
		width: 176rpx;
		height: 176rpx;
		border-radius: 50%;
	}

	.user-avatar {
		object-fit: cover;
		object-position: center;
	}

	.userinfo {
		position: absolute;
		top: 38rpx;
		left: 200rpx;

		.unlogin {
			width: 220rpx;
			font-size: 44rpx;
			font-family: PingFang-SC-Heavy, PingFang-SC;
			font-weight: 500;
			color: #FFFFFF;
			display: block;
			margin-top: 20rpx;
		}

		.username {
			width: 184rpx;
			height: 44rpx;
			font-size: 44rpx;
			font-family: PingFang-SC-Heavy, PingFang-SC;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 44rpx;
			display: block;
			margin-bottom: 24rpx;
		}

		.userId {
			font-size: 32rpx;
			font-family: PingFang-SC-Medium, PingFang-SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 32rpx;
		}
	}



	.img {
		image {
			width: 100vw;
			height: 910rpx;
		}
	}

	.title {
		height: 40rpx;
		width: 690rpx;
		// margin-top: 168rpx;
		margin-left: 30rpx;
		margin-bottom: 72rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.line {
			width: 240rpx;
			height: 2rpx;
			background: #EDEDED;
			border-radius: 1rpx;
		}

		.logintype {
			text {
				font-size: 28rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #333333;
			}
		}

	}

	.typebtn {
		display: flex;
		align-items: center;
		justify-content: space-around;

		.item {
			width: 140rpx;
			height: 156rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				width: 104rpx;
				height: 104rpx;
				margin-bottom: 12rpx;
			}

			text {
				display: block;
				font-size: 28rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #666666;
			}
		}
	}
</style>
