<script>
	import {
		mapMutations,mapState
	} from 'vuex';
	
	export default {
		methods: {
			...mapMutations(['setOpenid','login','showDialog','setcode','islogionsuccess','beginload'])
		},
		data(){
			return{
				username: {}
			}
		},
		computed:{
			...mapState([
			  'hasLogin',  // 用户是否同意授权
			  'userInfo',   // 同意授权用户头像
			  'successlogion', // 同意手机号授权 登录成功
			  'sessionId',   // 登录成功的token
			  'getcode',  // logion时候获取的code session_key oppen_id
			  'beginload'
			]),
		},
		onLaunch: function() {
			// 用户登录  avatarUrl   nickName 
			let that = this;
			let sessionId =  uni.getStorageSync('sessionId');// 用户token
			let userInfo = uni.getStorageSync('userInfo');// 用户头像信息
			console.log(userInfo);
			console.log("打印用户信息")
			let getcode= uni.getStorageSync('getcode');// 用户code oppenid
			console.log(userInfo)
			if(sessionId != '') {
				uni.checkSession({
					   success(){  
					     console.log("登录未过期")  
						 that.request({
						 	url:'/v2/member/info',
						 	method:'GET',
						 	success: (res)=>{
								let { data } = res.data;
						 		that.username.avatarUrl= data.member_avatar;
								that.username.nickName = data.member_truename;
								console.log(that.username)
								that.login(that.username);
						 	}
						 })
						 that.setOpenid(sessionId);
						 that.setcode(getcode);
						 that.showDialog(true);
						 that.beginload(true);
						 that.islogionsuccess(true);
					    },  
						fail(){  
						 console.log("登录过期")
			             // 登录过期 重新登录
					     uni.login({
					     		provider: 'weixin',
					     		success: (res) => {
									let code = res.code;
					     			 uni.request({
					     				  url: 'https://www.qzaiwang.com' +'/v1/entry/mp_openid',
					     				  method:'post',
					     				  data: {
					     					code:　code
					     				  },
					     				  success: (res) => {
											  let { data } = res.data;
											  console.log(data);
											  data.code = code;
											  that.setcode(data);
											  let token = data.token;
											  if(token == '') {
												   that.setOpenid(token); // 保存用户token
												   that.islogionsuccess(false);
											  }else {
												  that.setOpenid(token); // 保存用户token
												  that.islogionsuccess(true);
											  }
											  that.beginload(true);
											  that.showDialog(false); // 没有同意授权头像等;
											  that.login(); // 清空用户头像信息
					     				  }
					     			  })
					     		},
					     		fail: function(res) {
					     			
					     		}
					     })
						}  
				})
			}else {
			  console.log("登录")
			  uni.login({
			  		provider: 'weixin',
			  		success: (res) => {
						let code = res.code;
						console.log(code);
			  			 uni.request({
			  				  url: 'https://www.qzaiwang.com' +'/v1/entry/mp_openid',
							  method:'post',
			  				  data: {
								code:　code
			  				  },
			  				  success: (res) => {
								  let { data } = res.data;
								  data.code = code;
								  this.setcode(data); // 保存用户登录code session
								  let token = data.token;
								  if(token == '') {
									   this.setOpenid(token); // 保存用户token
									   this.islogionsuccess(false);
								  }else {
									  this.setOpenid(token); // 保存用户token
									  this.islogionsuccess(true);
								  }
								   this.beginload(true);
								   this.showDialog(false); // 没有同意授权头像等;
								   this.login(); // 清空用户头像信息
			  				  }
			  			  })
			  		},
			  		fail: function(res) {
			  			consoel.log(res);
			  		}
			  })
			};
		
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import '/wxcomponents/dist/common/index.wxss';
	@import '/common/uin.css';
</style>
