<template>
	<div>
		<loading :visible="isLoading" :doLoading="doLoading"></loading>
	</div>
</template>

<script>
	import * as API_WeiXin from '$project/apis/weixin'
	import * as API_Ali from '$project/apis/ali'
	import Loading from '$project/components/Loading.vue'
	import {
		getUrlParam,
		getWeixinRedirectURI,
		getAlipayRedirectURI
	} from '$project/utils'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'CommonCar',
		components: {
			Loading,
		},
		props: {
			//是否获取openid
			checkOpenId: {
				require: false,
				default: true,
			},
			doLoading: {
				require: false,
				default: false,
			},
		},
		data() {
			return {
				isLoading: false,
			}
		},
		created() {
			//公共组件的执行方法放在created中执行，父组件的create放在motuned中执行
			if (IS_WEIXIN) {
				if (this.checkOpenId) {
					if (!this.wx_openId) {
						this.getOpenid();
					} else {
						this.asynCallBack();
					}
				}
			} else if (IS_ALI) {
				if (this.checkOpenId) {
					if (!this.ali_openId) {
						this.getUserId();
					} else {
						this.asynCallBack();
					}
				}
			} else {
				if (this.checkOpenId) {
					/* mui.alert('请使用微信浏览打开!', '提示', function() {
						//info.innerText = '你刚关闭了警告框';
					}); */
				}
			}
		},
		methods: {
			//获取openid
			getOpenid() {
				const code = getUrlParam('code');
				if (!code) {
					window.location.href = getWeixinRedirectURI(process.env.VUE_APP_WXAPPID, document.URL);
				} else {
					console.log(code);

					if (!this.wx_openId) {
						const step1 = API_WeiXin.getDataByCode(code).then(response => {
							//console.log(response)
							this.set_wx_openid(response.openid);

							//回调
							this.asynCallBack();

							return Promise.resolve(response.openid);
						}).catch(error => {
							//console.log(error);
							mui.toast(error);
							return Promise.reject(error);
						});
						step1.then(() => {
							let redirect = this.$route.query.redirect;
							if (redirect) {
								this.$router.push({
									path: decodeURIComponent(redirect)
								});
							} else {
								window.location.href = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
							}
						});
					} else {
						//如果存在openid，把链接中code字符去除
						window.location.href = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
					}
				}
			},
			//获取openid
			getUserId() {
				var code = getUrlParam('auth_code');
				if (!code) {
					//alert(getAlipayRedirectURI(process.env.VUE_APP_ALIAPPID, document.URL))
					//这里用car/search来代替
					var authUrl = document.URL.replace('car/search', 'auth');
					window.location.href = getAlipayRedirectURI(process.env.VUE_APP_ALIAPPID, authUrl + '&page=' +
						encodeURIComponent(document.URL));
				} else {
					console.log(code);
					if (!this.ali_openId) {
						const step1 = API_Ali.getDataByCode(code).then(response => {
							//alert(JSON.stringify(response))
							this.set_ali_openid(response.openid);

							//回调
							this.asynCallBack();

							return Promise.resolve(response.openid);
						}).catch(error => {
							//console.log(error);
							return Promise.reject(error);
						});
						step1.then(() => {
							let redirect = this.$route.query.redirect;
							if (redirect) {
								this.$router.push({
									path: decodeURIComponent(redirect)
								});
							} else {
								window.location.href = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
							}
						});
					} else {
						//如果存在openid，把链接中code字符去除
						window.location.href = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
					}
				}
			},

			//异步回调父组件的方法
			asynCallBack() {
				this.$emit('asynCallBack');
			},
			...mapMutations({
				set_wx_openid: types.SET_WEIXIN_OPENID,
				set_ali_openid: types.SET_ALI_OPENID,
			})
		},
		mounted() {},
		computed: {
			...mapGetters({
				wx_openId: 'wx_openid',
				ali_openId: 'ali_openid',
			})
		}
	}
</script>

<style>
</style>
