-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
174 lines (156 loc) · 4.32 KB
/
app.js
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
//app.js
var util = require('/utils/util.js')
App({
onLaunch: function () {
var that = this
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
wx.setStorageSync('token', '')
},
getTokenInfo: function () {
var that = this
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (res.code) {
var code = res.code
console.log(res.code)
wx.showLoading({
title: '鉴权中',
})
that.http.request({
url: "auth/hairdresser/" + code,
header: {
'content-type': 'application/json'
},
data: {
},
method: "GET",
success: function (res) {
console.log(res)
wx.hideLoading()
if (res.statusCode != 200) {
console.log("鉴权失败 " + code)
wx.showModal({
title: '鉴权失败',
content: util.checkMsg(res.data.reMsg),
showCancel: false,
})
return
}
var newtoken = ''
if (res.header.authorization) {
newtoken = res.header.authorization.split(" ")[1]
} else if (res.header.Authorization) {
newtoken = res.header.Authorization.split(" ")[1]
}
wx.setStorageSync('token', newtoken)
console.log(newtoken)
if (that.tokenInfoReadyCallback) {
that.tokenInfoReadyCallback(res)
}
},
fail: function (res) {
console.log('login fail')
console.log(res)
}
})
} else {
console.log('获取用户登录状态失败!' + res.Msg)
}
}
})
},
refreshTokenInfo: function () {
var that = this
var oldtoken = wx.getStorageSync('token')
if (util.isNull(oldtoken)) {
return
}
that.http.request({
url: "refresh",
header: {
'content-type': 'application/json',
'Authorization': "Bearer " + oldtoken,
},
data: {
},
method: "GET",
success: function (res) {
console.log(res)
if (res.statusCode != 200) {
console.log("刷新token失败")
wx.showModal({
title: '刷新token失败',
content: util.checkMsg(res.data.reMsg),
showCancel: false,
})
return
}
var newtoken = ''
if (res.header.authorization) {
newtoken = res.header.authorization.split(" ")[1]
} else if (res.header.Authorization) {
newtoken = res.header.Authorization.split(" ")[1]
}
wx.setStorageSync('token', newtoken)
console.log(newtoken)
if (that.refreshTokenInfoReady) {
that.refreshTokenInfoReady(res)
}
},
fail: function (res) {
console.log("fail:" + res)
}
})
},
refreshTokenInfoNoCallback: function () {
var that = this
var oldtoken = wx.getStorageSync('token')
if (util.isNull(oldtoken)) {
return
}
that.http.request({
url: "refresh",
header: {
'content-type': 'application/json',
'Authorization': "Bearer " + oldtoken,
},
data: {
},
method: "GET",
success: function (res) {
console.log(res)
if (res.statusCode != 200) {
console.log("刷新token失败")
wx.showModal({
title: '刷新token失败',
content: util.checkMsg(res.data.reMsg),
showCancel: false,
})
return
}
var newtoken = ''
if (res.header.authorization) {
newtoken = res.header.authorization.split(" ")[1]
} else if (res.header.Authorization) {
newtoken = res.header.Authorization.split(" ")[1]
}
wx.setStorageSync('token', newtoken)
console.log(newtoken)
},
fail: function (res) {
console.log("fail:" + res)
}
})
},
globalData: {
userInfo: null,
openid: "",
//token: ""
},
http: require('/utils/request.js')
})