request.js
4.42 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import config from '@/config.js'
import {
pcLogin,
getNowOpenId,
initData,
showLogin,
h5LoginAuto
} from './login'
const excludeUrls = ['/system/wx/getUserInfo']
// 获取Token
function getToken() {
try {
const token = uni.getStorageSync('token');
if (token) {
return token
} else {
return ''
}
} catch (e) {
console.log(e)
}
}
// 获取请求头
function getHeaders() {
const header = {
"isToken": false,
'content-type': 'application/x-www-form-urlencoded'
}
return header
}
let request = function(req) {
req.method = req.method.toUpperCase()
// if (!['GET', 'POST', 'PUT', 'DELETE'].includes(req.method)) {
if (!['GET', 'POST'].includes(req.method)) {
uni.showToast({
title: `暂不支持的请求方式: ${req.method}`,
icon: 'none'
});
return
}
// if (req.method == 'POST' && !req.hideLoding) {
// uni.showLoading({
// title: '提交中...'
// })
// }
const token = getToken()
let tempUrl = req.url
let url, data
if (config.localPort) {
//生产环境
// const baseUrl = location.origin + '/jsintszxd/request'
//本地离线环境
const baseUrl = config.localPort + '/jsintszxd/request'
if (req.method === 'POST') {
if (token) {
req.params.userNo = token
}
url = baseUrl
data = {
appSecretId: config.appSecretId,
restApi: encodeURIComponent(tempUrl),
params: 'jsonBody=' + JSON.stringify(req.params)
}
} else {
if (token) {
if (tempUrl.indexOf('?') > -1) {
tempUrl += '&userNo=' + token
} else {
tempUrl += '?userNo=' + token
}
}
if (req.params) {
for (const key of Object.keys(req.params)) {
if (tempUrl.indexOf('?') > -1) {
tempUrl += `&${key}=${req.params[key]}`
} else {
tempUrl += `?${key}=${req.params[key]}`
}
}
}
url = `${baseUrl}?appSecretId=${config.appSecretId}&restApi=${encodeURIComponent(tempUrl)}`
}
} else {
if (req.method === 'POST') {
if (token) {
req.params.userNo = token
}
data = {
jsonBody: JSON.stringify(req.params)
}
} else {
if (req.params) {
for (const key of Object.keys(req.params)) {
if (tempUrl.indexOf('?') > -1) {
tempUrl += `&${key}=${req.params[key]}`
} else {
tempUrl += `?${key}=${req.params[key]}`
}
}
}
}
url = config.baseUrl_api + tempUrl
}
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: req.method,
data: data,
header: getHeaders()
}).then(res => {
switch (res.statusCode) {
case 200:
let tempRes
if (config.localPort) {
tempRes = res.data
} else {
tempRes = res
}
const resData = tempRes.data || {};
if (resData.code == 200) {
resolve(resData)
} else {
//登录超时
if (resData.code == 60002 || resData.code == 60001) {
showLogin()
} else if (resData.code == 401) {
h5LoginAuto()
.then(() => {
uni.hideLoading();
uni.redirectTo({
url: getCurrentPages()[getCurrentPages()
.length - 1].$page.fullPath
})
})
} else {
if (!excludeUrls.includes(req.url)) {
uni.showToast({
title: resData.msg || resData.error,
icon: 'none',
duration: 3000
})
}
}
reject(tempRes)
}
break
default:
reject(tempRes)
}
}).catch(res => {
reject(res)
}).finally(() => {
// if (req.method == 'POST' && !req.hideLoding) {
// uni.hideLoading()
// }
})
})
}
function reLogin() {
const currUser = uni.getStorageSync('currUser') || {}
if (currUser.openId) {
const accountInfo = uni.getAccountInfoSync()
pcLoginByOpenId(accountInfo.miniProgram.appId, currUser.openId)
.then((res) => {
let user = res.data;
uni.setStorageSync('token', user.token);
uni.setStorageSync('currUser', user);
uni.showToast({
title: '一走神把您的操作遗漏了,请重试',
icon: 'none'
})
}).catch(() => {
uni.showToast({
title: '服务异常,请稍后重试',
icon: 'none'
})
})
}
}
function pcLoginByOpenId(appId, openId) {
return request({
url: "/system/wx/loginByOpenId",
method: "POST",
params: {
appId: appId,
openId: openId
}
})
}
export default request;