<template>
	<div></div>
</template>

<script>
	import * as API_WeiXin from '$project/apis/weixin'
	import {
		getUrlParam,
		getWeixinRedirectURI,
		isWeiXin
	} from '$project/utils'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'Common',
		components: {},
		props: {
			//是否获取openid
			checkOpenId: {
				require: false,
				default: true,
			},
			//用户角色
			role: {
				require: false,
				default: '',
			}
		},
		data() {
			return {}
		},
		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() {
				var type = type || '';
				API_WeiXin.getDataByOpenId(this.openId).then(response => {
					//console.log(response)

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

					var person = response ? response.person : '';
					this.set_person_data(person);
					this.set_person_temp(person);

					var personPopedom = response ? response.personPopedom : '';
					this.set_person_popedom(personPopedom);

					var iconInfoList = response ? response.iconInfoList : '';
					this.set_menu_list(iconInfoList);

					this.asynCallBack();

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

			//异步回调父组件的方法
			asynCallBack() {
				this.$emit('asynCallBack');
			},
			...mapMutations({
				set_openid: types.SET_WEIXIN_OPENID,
				set_token: types.SET_TOKEN,
				set_person_data: types.SET_PERSON_DATA,
				set_person_popedom: types.SET_PERSON_POPEDOM,
				set_menu_list: types.SET_MENI_LIST,
				set_person_temp: types.SET_PERSON_TEMP,
			})
		},
		mounted() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				menu_list: 'menu_list',
				person_temp: 'person_temp'
			})
		}
	}
</script>

<style>
</style>
