zhanmingkan
2022-05-17 ad3562d25c2bcb4044ba631bd91d20703ab3bfe7
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//import Http from './http.js'
//会话信息
var access_token="";
$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    },
    crossDomain:true 
});
 
$.ajaxSettings.beforeSend = function(xhr,request){
    if(access_token==null || access_token==""){
        access_token = window.localStorage.getItem('webgis_access_token');     
    } 
    xhr.setRequestHeader('access_token',access_token); 
}
//处理后
$(document).ajaxComplete(function(event,request, settings) {
 
});
 
var error_page="../view/error.html";
var isLogin=true;
$(function(){
    
    access_token = window.localStorage.getItem('webgis_access_token');
    //var access_token_expire_time=window.localStorage.getItem('webgis_access_token');
    if(access_token==null || access_token==""){
        access_token=getAccessTokenWithUrl();
        
        if(access_token==null ||  access_token==""){
            window.open(error_page,"_self",""); 
        }else{ 
            window.localStorage.setItem('webgis_access_token', access_token);
        }
    }
    
});
 
function getAccessTokenWithUrl(){
    var url=window.location.href;
    var token="";
       var urlParams = url.split("?")[1];//或者url.search获取参数字符串
       if(urlParams==null){
           return "";
       }
       var paramArray = urlParams.split("&");
       var len = paramArray.length;
 
       for (var i = 0; i < len; i++) {
            arr = paramArray[i].split("=");
            if(arr[0]=="access_token"){
                token = arr[1];
                break;
            }
        }    
       return token;
}
 
function isTokenValid( token ){
//    $.ajax({
//        type : "post",
//        url : "localhost",
//        async : false,
//        contentType : "application/json;charset=utf-8",
//        success : function(data) {
//            
//        
//        },
//        error:function(data){
//            window.open(error_page,"_self",""); 
//        }
//    });
    return true;
}
 
function destroySession(){
     window.localStorage.removeItem('access_token');
}
 
 
/**
 * 会话管理
 */
//export default class Session {
//  getAccessToken() {
//    return window.localStorage.getItem('access_token')
//  }
//
//  setAccessToken(accessToken) {
//    window.localStorage.setItem('access_token', accessToken)
//  }
//
//  getAccessTokenExpireTime() {
//    return window.localStorage.getItem('access_token_expire_time')
//  }
//
//  setAccessTokenExpireTime(accessTokenExpireTime) {
//    window.localStorage.setItem('access_token_expire_time', accessTokenExpireTime)
//  }
//
//  getCurrentUser() {
//    const user = window.localStorage.getItem('user')
//    return user ? JSON.parse(user) : undefined
//  }
//
//  setCurrentUser(user) {
//    window.localStorage.setItem('user', JSON.stringify({ id: user.id, realname: user.realname, avatar: user.user_info.avatar }))
//  }
//
//  getCurrentCompany() {
//    const company = window.localStorage.getItem('company')
//    return company ? JSON.parse(company) : undefined
//  }
//
//  setCurrentCompany(company) {
//    window.localStorage.setItem('company', JSON.stringify({ id: company.id, name: (company.abbreviation != "" && company.abbreviation) ? company.abbreviation : company.name }))
//  }
//
//  getSwitchCompanys() {
//    const companys = window.localStorage.getItem('switchCompanys')
//    return companys ? JSON.parse(companys) : undefined
//  }
//
//  setSwitchCompanys(companys) {
//    window.localStorage.setItem('switchCompanys', JSON.stringify(companys.map(i => ({ id: i.id, name: (i.abbreviation != "" && i.abbreviation) ? i.abbreviation : i.name }))))
//    let accountType = 1 //1为个人版,2为企业版
//    if (companys.length > 0) {
//      accountType = 2
//    }
//    window.localStorage.setItem('account_type', accountType)
//  }
//
//  getAccountType() {
//    const accountType = window.localStorage.getItem('account_type')
//    return accountType
//  }
//
//  getRedirectUrl() {
//    const url = window.localStorage.getItem('redirect_url')
//    window.localStorage.removeItem('redirect_url')
//    return url
//  }
//
//  setRedirectUrl(url) {
//    window.localStorage.setItem('redirect_url', url)
//  }
//
//  //是否有效登陆
//  isValidLogin() {
//    const accessToken = this.getAccessToken()
//    const accessTokenExpireTime = this.getAccessTokenExpireTime()
//    return (accessToken && accessTokenExpireTime && moment().unix() < accessTokenExpireTime) ? true : false
//  }
//
//  //创建会话
//  createSession(accessToken) {
//    this.setAccessToken(accessToken.access_token)
//    this.setAccessTokenExpireTime(moment().unix() + accessToken.expires_in - (60 * 30))
//    window.localStorage.removeItem('company')
//  }
//
//  //销毁会话
//  destroySession() {
//    window.localStorage.removeItem('access_token')
//    window.localStorage.removeItem('company')
//    window.localStorage.removeItem('user')
//    window.localStorage.removeItem('module')
//  }
//
//  /* 模块切换(项目群、部门群)*/
//  getSwitchModules() {
//    const modules = window.localStorage.getItem('switchModules')
//    return modules ? JSON.parse(modules) : undefined
//  }
//
//  setSwitchModules(modules) {
//    const newArray = [...modules.projects, ...modules.departments]
//    window.localStorage.setItem('switchModules', JSON.stringify(newArray.map(i => ({ id: i.data_id, name: i.data_name, module_id: i.module_id, module_name: i.module_name }))))
//  }
//
//  getCurrentModule() {
//    const module = window.localStorage.getItem('module')
//    return module ? JSON.parse(module) : undefined
//  }
//
//  setCurrentModule(module) {
//    window.localStorage.setItem('module', JSON.stringify({ id: module.data_id, name: module.data_name, module_id: module.module_id, module_name: module.module_name }))
//  }
//}