WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<template>
  <div>
    <div desktop="12" tablet="8">
      <dl>
        <dt>Login successful</dt>
        <dt>Your browser should be redirected soon</dt>
      </dl>
    </div>
  </div>
</template>
 
<script>
import applicationUserManager from "../Auth/applicationusermanager";
 
import {
  requestLogin,
  requestLoginMock,
  getUserByToken,
  getNavigationBar
} from "../api/api";
 
import router from "@/router";
import { resetRouter, filterAsyncRouter } from "@/router/index";
 
export default {
  name: "logincallback-view",
  data() {
    return {};
  },
  async created() {
    try {
      await applicationUserManager.signinRedirectCallback();
      let _this = this;
      let user = await applicationUserManager.getUser();
      _this.$store.commit("saveToken", user.access_token);
      debugger;
      var curTime = new Date();
      var expiredate = new Date(
        curTime.setSeconds(curTime.getSeconds() + user.expires_in)
      );
      _this.$store.commit("saveTokenExpire", expiredate);
 
      window.localStorage.refreshtime = expiredate;
      window.localStorage.expires_in = user.expires_in;
 
      _this.$notify({
        type: "success",
        message: `成功获取令牌,项目初始化中...`,
        duration: 3000
      });
      
      user.uRealName=user.profile.name;
      user.uLoginName=user.profile.preferred_username;
      user.uID=user.profile.sub;
      window.localStorage.user = JSON.stringify(user);
      if (user.uID > 0) {
        _this.GetNavigationBar(user.uID);
      }
 
    } catch (e) {
      this.$root.$emit("show-snackbar", { message: e });
    }
  },
  methods: {
    // 获取路由树
    GetNavigationBar(uid) {
      var _this = this;
      var loginParams = { uid: uid, t: new Date() };
 
      getNavigationBar(loginParams).then(data => {
 
        if (!data.success) {
          _this.$message({
            message: data.message,
            type: "error"
          });
        } else {
          // _this.closeAlert()
 
          _this.$message({
            message: "后台初始化成功",
            type: "success"
          });
          let userinfo = JSON.parse(
            window.localStorage.user ? window.localStorage.user : null
          );
          _this.$notify({
            type: "success",
            message: `登录成功 \n 欢迎管理员:${
              userinfo.uRealName
            } !Token 将在 ${parseInt(window.localStorage.expires_in / 60)} 分钟后过期!`,
            duration: 6000
          });
 
          window.localStorage.router = JSON.stringify(data.response.children);
 
          let getRouter = data.response.children; //后台拿到路由
          getRouter = filterAsyncRouter(getRouter); //过滤路由
          router.$addRoutes(getRouter); //动态添加路由
 
          _this.$router.replace(
            _this.$route.query.redirect ? _this.$route.query.redirect : "/"
          );
        }
      });
    }
  }
};
</script>
 
<style>
</style>