import loginApi from '@/api/auth/loginApi'
import userCenterApi from '@/api/sys/userCenterApi'
import dictApi from '@/api/sys/dev/dictApi'
import router from '@/router'
import tool from '@/utils/tool'
import {message} from 'ant-design-vue'
import {useGlobalStore} from '@/store'
import index from "@chenfengyuan/vue-qrcode";

export const afterLogin = async (loginToken) => {
	tool.data.set('TOKEN', loginToken)
	// 获取登录的用户信息
	const loginUser = await loginApi.getLoginUser()
	const globalStore = useGlobalStore()
	globalStore.setUserInfo(loginUser)
	tool.data.set('USER_INFO', loginUser)

	// 获取用户的菜单
	const menu = await userCenterApi.userLoginMenu()
	var firstMenu = menu[0].children[0];
	tool.data.set('MENU_MODULE_ID', menu[0].id)
	if (!(firstMenu.menuType && firstMenu.menuType === 'MENU')) {
		// 最多往下找两级
		firstMenu = firstMenu.children[0]
		if (!(firstMenu.menuType && firstMenu.menuType === 'MENU')) {
			firstMenu = firstMenu.children[0]
		}
	}
	let indexMenu = firstMenu.path
	tool.data.set('MENU', menu)
	// 重置系统默认应用
	message.success('登录成功')
	if (!!tool.data.get('LAST_VIEWS_PATH')) {
		// 如果有缓存，将其登录跳转到最后访问的路由
		indexMenu = tool.data.get('LAST_VIEWS_PATH')
	}
	await router.replace({
		path: indexMenu
	})
	dictApi.dictTree().then((data) => {
		// 设置字典到store中
		tool.data.set('DICT_TYPE_TREE_DATA', data)
	})
}
