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

<script>
import storage from "@/utils/storage";
import { mapMutations } from "vuex";
import { getHomeData } from "@/api/home";
import { generateRoutes, filterBtn } from "@/utils";
import { calcMenu } from "@/router";
export default {
  data() {
    return {
      menuList: [],
    };
  },
  created() {
    this.getToken();
  },
  methods: {
    ...mapMutations("user", [
      "SET_token",
      "SET_sysName",
      "SET_sysLogo",
      "SET_path",
      "SET_userInfo",
      "SET_permissions",
      "SET_routes",
      "SET_menusList",
    ]),
    // 获取token
    async getToken() {
      let { token, userInfo, siteid, siteName, sysName, sysLogo, path } =
        this.$route.query;
      if (token && userInfo) {
        userInfo = JSON.parse(userInfo);
        this.SET_userInfo(userInfo);
        this.SET_token(token);
        this.SET_sysName(sysName);
        this.SET_sysLogo(sysLogo);
        this.SET_path(path);
        storage.set(2, "siteId", siteid);
        storage.set(2, "siteName", siteName);
        await this.getIndixData();
      } else {
        this.$message.warning("跳转失败,请重新登录");
        setTimeout(() => {
          location.href = process.env.VUE_APP_API_portal_URL;
        }, 2000);
      }
    },
    // 获取菜单列表
    async getIndixData() {
      let res = await getHomeData();
      if (res.data.code == 1) {
        let { menuList } = res.data.data;
        // 过滤掉按钮
        let menus = filterBtn(menuList, false);
        let routes = generateRoutes(menus);
        // this.SET_routes(routes);
        this.SET_menusList(menus);
        this.setBtnPermissions(menuList);
        calcMenu();
        if (routes.length) {
          let path = routes[0].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>
