<template>
	<view>
		<view class='login-header'>
			<image class="infoImg" mode="aspectFill" :src="userInfo.avatarUrl || imgSrc"></image>
			<view class="logo-name">{{appletName}}</view>
			<view class="logo-title font24 color-99 mar-s20" style="text-align: center;">{{appleSubtitle}}</view>
		</view>
		<view class="login-footer">
			<view class="login-btn radius20 color-48 font36" :style="{background:'#febf00'}" type='primary' @tap="bindGetUserInfo">微信授权</view>
			<view class="agreement-box font30">如您点击授权，您将同意授权<text @tap="toAgreement" class="agreement">《免责声明》</text></view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userInfo: {}, //用户信息
				canIGetUserProfile: false,
				imgSrc: '/static/img/logo.png', //默认logo头像
				appletName:'“老农极鲜～土货”', //小程序名称
				appleSubtitle:'精选特产尽在土货',
				isShowP:false,
			};
		},
		onLoad() {
			if (uni.getUserProfile) {
				this.canIGetUserProfile = true;
			}
		},
		methods: {
			// 设置logo图
			setLogo(){
				this.$requst.get('index/base-config').then(res=>{
					this.imgSrc = this.$http + res.data.logo;
					this.appletName = res.data.appletName;
				})
			},
			refuse(){//暂不绑定事件
				this.isShowP=false;
				this.$toolAll.tools.showToast('登录成功','success')
				uni.reLaunch({url:'/pages/index/index'})
			},
			getphonenumber(e){//授权绑定手机号
				let ya = this;
				wx.login({
					success:(res)=>{
						this.$requst.post('/api/user/login',{code:res.code}).then(result => {
							if(e.detail.errMsg=="getPhoneNumber:ok"){
								this.$requst.post('user/bind-phone',{openid: result.data.openid,session_key:result.data.session_key, iv:e.detail.iv,encryptedData:e.detail.encryptedData}).then(res=>{
									console.log('手机号信息：',res);
									if(res.code==0){
										this.$toolAll.tools.showToast('手机号绑定成功');
										this.isShowP = true;
									} else this.$toolAll.tools.showToast(res.msg);
								},error=>{})
							} else {
								// console.log('取消授权手机号')
							}
						}).catch(err=>{
							// console.log(err);
						})
					}
				})
			},
			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('inviteCode')?uni.getStorageSync('inviteCode'):'',
					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/index/index'
							})
						}
					}
				},error => {})
			}
		}
	}
</script>
<style scoped>
	
</style>
