<script>
	import {
		authorization,
		getOpenid
	} from '@/api/send'
	export default {
		onLaunch: function(e) {
			// !openId && 
			if (!e.query.code) {
				console.log('获取code');
				getApp().authorization()
			} else if (e.query.code) {
				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')
		},
		onHide: function() {
			console.log('App Hide')

		},
		watch: {

		},
		methods: {
			// 拿code
			async authorization() {
				const appid = uni.getStorageSync('appid')
				try {
					const res = await authorization(window.location.href,appid)
					console.log('authorization', res)
					if(res.is_new_appid==1){
						window.location.href = res.url
						uni.setStorageSync('appid', res.appid)
					}else{
						return
					}
					// 保存数据
				} 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;
	}

	.flexbetom {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.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>