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

<script>
	import * as API_WeiXin from '$project/apis/weixin'
	import * as API_Merchant from '@/apis/merchant'
	import Loading from '$project/components/Loading.vue'
	import {
		getUrlParam,
		getWeixinRedirectURI,
		isWeiXin
	} from '$project/utils'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'CommonMerchant',
		components: {
			Loading,
		},
		props: {
			//是否获取openid
			checkOpenId: {
				require: false,
				default: true,
			},
			doLoading: {
				require: false,
				default: false,
			},
		},
		data() {
			return {
				isLoading: false,
			}
		},
		created() {
			//公共组件的执行方法放在created中执行，父组件的create放在motuned中执行
			if (isWeiXin()) {
				if (this.checkOpenId) {
					if (!this.openId) {
						this.getOpenid();
					} else {
						this.getDataByOpenId();
					}
				}
			} 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.openId) {
						const step1 = API_WeiXin.getDataByCode(code).then(response => {
							//console.log(response)
							this.set_openid(response.openid);

							//角色判定调用不同的信息获取详情
							this.getDataByOpenId();

							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, '');
					}
				}
			},
			//角色判定调用不同的信息获取详情
			getDataByOpenId() {
				this.getUserInfoByOpenId();
			},
			//获取用户详情
			getUserInfoByOpenId() {
				this.isLoading = true;

				var type = type || '';
				API_Merchant.findByOpenIdAndMerchant(this.openId, this.parking_merchant_id).then(response => {

					this.isLoading = false;

					console.log(response)

					var token = response ? response.token : '';
					this.set_token(token);

					this.set_parking_merchant_data(response);


					this.asynCallBack();

				}).catch(error => {
					mui.toast(error);
				})
			},



			//异步回调父组件的方法
			asynCallBack() {
				this.$emit('asynCallBack');
			},
			...mapMutations({
				set_openid: types.SET_WEIXIN_OPENID,
				set_token: types.SET_TOKEN,
				set_parking_merchant_data: types.SET_PARKING_MERCHANT_DATA
			})
		},
		mounted() {
			//如果是开发环境则直接读取findbyopenid信息
			if (process.env.VUE_APP_NODE_NAME == 'devlopment') {
				this.getDataByOpenId();
			}
		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				parking_merchant_id: 'parking_merchant_id',
				parking_merchant_data: 'parking_merchant_data'
			})
		}
	}
</script>

<style>
</style>
