<script>
	var wx = require('jweixin-module')
	import {authorization,getOpenid } from '@/api/send'
	export default {
		// http://localhost:8080/ 
		onLaunch:  function(e) {
			const openId = uni.getStorageSync('openId')
			console.log(openId);
			if(!openId && !e.query.code){
				getApp().authorization()
			}else if(e.query.code) {
				console.log(2);
				getApp().getOpenid(e.query.code)
			}
			// else {
			// 	uni.showModal({
			// 		content: 'open:'+openId + 'code:'+e.query.code,
			// 		showCancel: false
			// 	});
			// }
			console.log(e.query.code);
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 拿code
			async authorization(){
			  try {
			    const res = await authorization(window.location.href)
			    console.log('authorization', res)
				window.location.href = res.url
			    // 保存数据
			  } catch (err) {
			 	uni.showToast({ title:err,icon:'none' })
			    console.log('authorization', err)
			  }
			},
			// 获取openid
			async getOpenid(code){
			  try {
			    const res = await getOpenid(code)
			    console.log('getOpenid', res)
				uni.setStorageSync('openId',res.openid)
			    // 保存数据
			  } catch (err) {
			 	setTimeout(()=>{
					uni.showToast({ title:err,icon:'none' })
				},300)
				uni.redirectTo({
					url:'/pages/index/index'
				})
			    console.log('getOpenid', err)
			  }
			},
		},
	}
</script>

<style lang="scss">
	/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	uni-page-head {
		display:none !important;
	}
	/*每个页面公共css */
	.wh100 {
		width: 100%;
		height: 100%;
	}
	
	.flexC {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.flexD {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.flex {
		display: flex;
	}
	
	.flexA {
		display: flex;
		align-items: center;
	}
	
	.fw700 {
		font-weight: 700;
	}
	
	.flexwrap {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	
	.flexcolum {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	
	.serviceBtn {
		opacity: 0;
		position: absolute;
		left: 0;
		bottom: 0;
		width: 120rpx;
		height: 120rpx;
	}
</style>
<style>
	
	/*每个页面公共css */
</style>