-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
83 lines (76 loc) · 2.39 KB
/
Copy pathcommon.js
File metadata and controls
83 lines (76 loc) · 2.39 KB
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
window.base = {
g_restUrl: '',
getData: function (params) {
params.type || (params.type = 'get');
var that = this;
$.ajax({
type: params.type,
url: that.g_restUrl + params.url,
data: params.data,
beforeSend: function (XMLHttpRequest) {
if (params.tokenFlag) {
var token = (params.tokenFlag === 'refresh' ? that.getLocalStorage('refresh_token') : that.getLocalStorage('token'));
XMLHttpRequest.setRequestHeader('token', token);
}
},
success: function (res) {
params.success && params.success(res);
},
error: function (err) {
if (err.status == 401) {
window.location.href = 'login.html';
} else {
console.log(err);
params.fail && params.fail(err);
}
}
});
},
setLocalStorage: function (key, val, exp) {
exp || (exp = new Date().getTime() + 30 * 24 * 60 * 60 * 1000); //令牌过期时间
var obj = {
val: val,
exp: exp
};
localStorage.setItem(key, JSON.stringify(obj));
},
getLocalStorage: function (key) {
var info = localStorage.getItem(key);
if (info) {
info = JSON.parse(info);
if (info.exp > new Date().getTime()) {
return info.val;
}
else {
this.deleteLocalStorage('token');
}
}
return '';
},
deleteLocalStorage: function (key) {
return localStorage.removeItem(key);
},
getQueryString: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
},
loadLocalHtml: function (url, element) {
$.ajax({
url: url,
type: 'get',
success: function (res) {
$(element).html(res);
},
error: function (err) {
console.log(err);
}
});
},
strToTimestamp: function (str) {
str = str.replace(/-/g, '/');
var date = new Date(str);
var time = date.getTime();
return time;
}
};