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

<script>
import { departInterence } from '@/api/api.js';
export default {
	data() {
		return {

		}
	},
	created() {
		this.init()

	},
	methods: {
		async init() {
			if (Object.keys(this.$route.query).length <= 0) {
				this.$router.replace('/login')
				return
			}
			// debugger;

			const res = await departInterence.useCodeLogin(this.$route.query)
			// console.log(res);
			// debugger;
			if (res.Ret !== 200) {
				this.$router.replace('/login?HZ=1')
				return
			}

			localStorage.setItem('auth', res.Data.Authorization)
			localStorage.setItem('userName', res.Data.RealName)
			localStorage.setItem('Role', res.Data.RoleTypeCode)
			localStorage.setItem('RoleIdentity', res.Data.SysRoleTypeCode)
			localStorage.setItem('RoleType', res.Data.ProductName)
			localStorage.setItem('ManageType', res.Data.Authority)
			localStorage.setItem('AdminId', res.Data.AdminId)
			localStorage.setItem('AdminName', res.Data.AdminName)
			// 如果路由参数有redirect_uri则跳转到redirect_uri,redirect_uri通过encode
			if (this.$route.query.redirect_uri) {
				const path = decodeURIComponent(this.$route.query.redirect_uri)
				this.$router.push(path)
				return
			}

			let path = "";
			switch (res.Data.RoleTypeCode) {
				case "rai_researcher":
				case "ficc_researcher":
				case "researcher":
					path = "/reportlist";
					break;
				// case "compliance": //合规
				// path = "/contractapprovallist";
				// break;
				case "special_researcher": //特邀研究员
					path = "/dataList";
					break;
				// case "special_ficc_seller":
				// path = "/meetingCalendar";
				// break;
				default:
					path = await this.getOtherRolePath("myCalendar");
			}

			this.$router.push({ path });
		},
		getOtherRolePath(pathVal) {
			return departInterence.getMenu().then(res => {
				let resolvePath = ''
				if (res.Ret === 200) {
					let menuList = res.Data.List || [];
					if (!menuList.length) {
						this.$message.error('该账号没有任何菜单权限,请联系管理员')
						return
					}
					// 是否已经拿到菜单信息
					sessionStorage.setItem('hasGetMenu', 'true')
					sessionStorage.setItem('MenuList', JSON.stringify(menuList));
					/* 是否有数据报表权限 */
					this.$store.commit('SET_DATA_AUTH', menuList.some(item => item.name === '报表统计'))

					for (let i = 0; i < menuList.length; i++) {
						const element = menuList[i];
						const arr = menuList[i].children || []
						if (arr.some(it => it.path == pathVal)) {
							resolvePath = '/' + pathVal
							break
						}
					}
					return resolvePath || '/' + menuList[0].children[0].path
				}
				return '/' + pathVal
			})
		}
	}
}
</script>

<style></style>