<template>
  <div>页面跳转中......</div>
</template>

<script>
import local from "@/utils/local";
import { mapMutations } from "vuex";
import { calcMenu, menusFilter } from "@/router";
import { generateRoutes, filterBtn } from "@/utils";
import { getMenuTree } from "@/api/system";
export default {
  data() {
    return {
      portal: process.env.VUE_APP_API_portal_URL,
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    ...mapMutations([
      "SET_token",
      "SET_sysName",
      "SET_sysLogo",
      "SET_path",
      "SET_userInfo",
      "SET_permissions",
      "SET_routes",
      "SET_menusList",
      "SET_secondaryRoutes",
    ]),
    // 获取token和站点信息
    async getInfo() {
      let { token, siteid, sysName, sysLogo, path } = this.$route.query;
      if (token) {
        local.setLocal("siteId", siteid);
        this.SET_token(token);
        this.SET_sysName(sysName);
        this.SET_sysLogo(sysLogo);
        this.SET_path(path);
        calcMenu();
        await this.getIndixData();
      } else {
        this.$message.warning("跳转失败，请重新登录");
        setTimeout(() => {
          location.href = this.portal;
        }, 2000);
      }
    },
    // 获取菜单列表
    async getIndixData() {
      let res = await getMenuTree();
      if (res.data.code == 1) {
        let { menuList } = res.data.data;
        // 过滤掉按钮
        let menus = filterBtn(menuList, false);
        let routes = generateRoutes(menus);
        routes = menusFilter(routes);
        this.SET_menusList(menus);
        this.setBtnPermissions(menuList);
        calcMenu();
        if (routes.length) {
          let path = routes[0].path;
          this.SET_secondaryRoutes(path);
          this.$router.push(path);
        } else {
          this.$message.warning("暂无页面权限，请联系管理员！");
        }
      }
    },

    // 添加按钮权限字符
    setBtnPermissions(menuList) {
      let btnPermissions = filterBtn(menuList)
        .filter((v) => v.perms)
        .map((v) => v.perms);
      this.SET_permissions(btnPermissions);
    },
  },
};
</script>

<style lang="less" scoped></style>
