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

<script>
	const projectName = require('$root/config/project');
	import * as API_WeiXin from '$project/apis/weixin'
	import * as API_User from '$project/apis/user'
	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'
	import * as Dictionaries from '$project/utils/dictionaries'
	export default {
		name: 'Common',
		components: {
			Loading,
		},
		props: {
			//是否获取openid
			checkOpenId: {
				require: false,
				default: true,
			},
			//用户角色
			role: {
				require: false,
				default: '',
			},
			//是否检测项目，进行跳转
			projectCheck: {
				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_WeiXin.getDataByOpenId(this.openId).then(response => {

					this.isLoading = false;

					//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);

					var commonIconInfoList = response ? response.commonIconInfoList : '';
					this.set_common_menu_list(commonIconInfoList);

					//获取所有功能列表
					if (token) {
						this.getAuthFunList();
					} else {
						this.asynCallBack();
					}

				}).catch(error => {
					mui.toast(error);
				})
			},
			//获取所有功能监权列表
			getAuthFunList() {
				this.isLoading = true;
				API_User.getAuthFunList().then(response => {

					this.isLoading = false;

					if (response && response.allIconRoute) {
						var allIconRoute = response.allIconRoute;
						this.set_auth_fun_list(allIconRoute);

						this.asynCallBack();
					} else {
						mui.toast('未返回allIcon');
					}


				}).catch(error => {
					mui.toast(error);
				})
			},
			//判断权限然后进行跳转
			projectExchange() {
				//如果不想跳转，还可以传参exchange，一般用于home注册切换等功能
				if (this.projectCheck && this.$route.query.exchange != '1' && this.person_popedom && this.person_popedom.sceneId) {
					let project = Dictionaries.getProject(this.person_popedom.sceneId);
					if (projectName.name != project ) {
						window.location.href = '#/master';
					}
				}
			},


			//异步回调父组件的方法
			asynCallBack() {
				this.projectExchange();
				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,
				set_auth_fun_list: types.SET_AUTH_FUN_LIST,
				set_app_version: types.SET_APP_VERSION,
				set_common_menu_list: types.SET_COMMON_MENU_LIST,
			})
		},
		mounted() {
			//如果是开发环境则直接读取findbyopenid信息
			if (process.env.VUE_APP_NODE_NAME == 'devlopment') {
				this.getDataByOpenId();
			}
		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				menu_list: 'menu_list',
				common_menu_list: 'common_menu_list',
				person_temp: 'person_temp',
				auth_fun_list: 'auth_fun_list',
				app_version: 'app_version'
			})
		}
	}
</script>

<style>
</style>
