<template>
	<view>
		<view class='login-header'>
			<image class="infoImg" mode="aspectFill" :src="userInfo.avatarUrl || imgSrc"></image>
			<view class="logo-name">{{appletName}}</view>
		</view>
		<view class="login-footer">
			<view class="login-btn" type='primary' @tap="bindGetUserInfo">微信授权</view>
			<view class="agreement-box">如您点击授权，您将同意授权<text @tap="toAgreement" class="agreement">《免责声明》</text></view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userInfo: {}, //用户信息
				imgSrc: '/static/public/logo.jpg', //默认logo头像
				appletName:'传武佳', //小程序名称
				isShowP:false,
			};
		},
		onLoad() {
			
		},
		methods: {
			// 查看免责声明
			toAgreement(){
				uni.navigateTo({
					url:'/pagesB/disclaimers/disclaimers'
				})
			},
			//调起登录授权
			bindGetUserInfo(e) {
				let ya = this;
				//新版登录方式
				uni.getUserProfile({
					desc: '登录',
					lang: 'zh_CN',
					success: (res) => {
						ya.userInfo = res.userInfo;
						uni.login({
							provider: 'weixin',
							success: (res)=> {
								if (res.code) {
									ya.updateUserInfo(res.code);
								} else {
									uni.showToast({
										title: '登录失败！',
										duration: 2000
									});
								}
							},
						});
					},
					fail: (res) => {}
				});
			},
			
			//调用登录接口
			updateUserInfo(code) {
				let ya = this;
				uni.showToast({
					title: '登录中...',
					icon:'loading',
					duration:10000
				})
				var params = {
					code:code,
					nickname: ya.userInfo.nickName,//用户昵称
					headimgurl: ya.userInfo.avatarUrl,//用户头像
					country: ya.userInfo.country,//用户所在国家
					province: ya.userInfo.province,//用户所在省份
					city: ya.userInfo.city,//用户所在城市
					gender: ya.userInfo.gender,//用户性别
					language:ya.userInfo.language,//语言
					invite_code:uni.getStorageSync('invite_code')?uni.getStorageSync('invite_code'):'',
					is_active:1
				}
				this.$requst.post('/api/user/login',params).then(res => {
					if(res.code == 0){
						uni.setStorageSync('userId',res.data.account_id)
						uni.setStorageSync('token',res.data.token)//缓存token
						uni.setStorageSync('openid',res.data.openid)//缓存openid
						uni.setStorageSync('expire',res.data.expire)//缓存失效时间（时间戳格式）
						uni.setStorageSync('phone_active',res.data.phone_active)//是否授权手机号
						uni.setStorageSync('is_active',res.data.is_active)//是否授权头像和昵称
						uni.setStorageSync('invite_code',res.data.invite_code)//缓存邀请码
						if(uni.getStorageSync('page-path-options')) {
							uni.reLaunch({ // 重新进入当前页面
								url:uni.getStorageSync('page-path-options')
							})
						} else {
							uni.reLaunch({
								url:'/pages/tabbar/pagehome/pagehome'
							})
						}
					}
				},error => {})
			}
		}
	}
</script>
<style scoped>
	.login-header {
		justify-content: center;
		align-items: center;
		flex-direction: column;
		margin-top: 0;
		position: fixed;
		left: 50%;
		top: 40%;
		transform: translate(-50%,-50%);
	}
	.infoImg {
		width: 224rpx;
		height: 224rpx;
		border-radius: 100%;
		border: 2rpx solid #d8d8d8;
	}
	.logo-name {
		margin-top: 20rpx;
		font-size: 30rpx;
		font-weight: bold;
		text-align: center;
		letter-spacing: 4rpx;
		text-indent: 4rpx;
	}
	.login-footer {
		width: 100%;
		text-align: center;
		position: fixed;
		left: 0;
		bottom: 120rpx;
	}
	.login-btn {
		width: calc(100% - 80rpx);
		line-height: 100rpx;
		margin: 0 auto;
		background-color: #e42417;
		border-radius: 20rpx;
		color: #ffffff;
		font-size: 34rpx;
		letter-spacing: 4rpx;
		text-indent: 4rpx;
		box-shadow: 0rpx 8rpx 20rpx rgba(228,36,23,.5);
	}
	.agreement-box{
		margin-top: 25rpx;
		font-size: 28rpx;
	}
	.agreement{
		color: #e42417;
	}
</style>