restapi.js
7.67 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// /**
// *
// 0本地
// 1开发
// 2测试
// 3生产环境
// 4预发布环境
// */
// const accountInfo = wx.getAccountInfoSync()
// const systemInfo = wx.getSystemInfoSync()
// // console.log(systemInfo);
// // console.log(accountInfo.miniProgram.envVersion)
// //手动控制体验版环境的请求 true就依据下面index false就默认为2
// const manual = false
// const release = false
// let index =1
// // 判断小程序环境
// // develop:开发版(正常开发工具,测试时)
// // trial:体验版
// // release:正式版
// // console.log( systemInfo.platform);
// // console.log(accountInfo.miniProgram.envVersion);
// if (systemInfo.platform != 'devtools') {
// switch (accountInfo.miniProgram.envVersion) {
// case 'develop':
// // this.globalData.url = 'http://192.168.0.252:3000/api'; //
// break
// case 'trial':
// index = manual ? index : 2
// break
// case 'release':
// index = release ? index : 3
// break
// default:
// index = 3
// break
// }
// }
import { envIndex } from '~/config'
let index = envIndex
const domain_items = {
ip: 'http://192.168.3.181:9182',
dev: 'https://dj.dev.glsx.com.cn',
uat: 'https://dj.test.glsx.com.cn',
prod: 'https://dj.glsx.com.cn',
preRelease: 'https://dj.beta.glsx.com.cn'
}
const target = ['ip', 'dev', 'uat', 'prod','preRelease']
const domain= domain_items[target[index]]
/**
* 通用请求
* url: 请求路径
* data: 数据参数
* header: 请求头
* method: 请求类型,默认GET,需大写
* dataType: 返回数据格式,默认json
* responseType: 响应数据类型,默认text
* success: 响应成功回调函数
*/
function request(url, data, header, method, dataType, responseType, success, fail) {
//埋点
var requestStartTime = Date.now()
wx.showNavigationBarLoading()
// 请求超时时间默认60秒
var timeout = 60000
if (header && header['request-timeout']) {
timeout = header['request-timeout']
delete header['request-timeout']
}
// 设置请求头,携带Cookie信息
setHeader(header)
var that = this
wx.request({
url: domain + url,
data: data,
header: header,
timeout: timeout,
method: method,
dataType: dataType,
responseType: responseType,
success: function (response) {
// 未登录
setHeader(response.header)
if (response.statusCode == 401) {
// 跳转到正在登录页面
wx.navigateTo({
url: '/pages/auth/login/login'
})
} else if (response.statusCode == 200) {
success(response.data)
} else {
if (fail && 'function' === typeof fail) {
fail()
}
}
},
fail: function (error) {
console.log('error:' + JSON.stringify(error))
if (fail && 'function' === typeof fail) {
fail()
}
},
complete: function (data) {
wx.hideNavigationBarLoading()
}
})
}
/**
* 设置请求头Cookie信息
* header 源请求头信息
* return 包含Cookie信息header
*/
function setHeader(header) {
var app = getApp()
if (app && app.globalData.extConfig.merchantId) {
header['MerchantId'] = app.globalData.extConfig.merchantId
}
var sessionKey = 'DJ_S_'
// 本地获取存储的sessionId
var sessionId = wx.getStorageSync(sessionKey)
if (sessionId) {
// 优先从本地获取sessionId
header['Cookie'] = [sessionKey, '=', sessionId].join('')
return header
}
// 本地没有,则从服务器获取
var cookies = header['Set-Cookie']
if (cookies) {
var re = new RegExp('' + sessionKey + '=([^;]*)', 'ig')
sessionId = cookies.match(re) ? cookies.match(re)[0].substr(sessionKey.length + 1) : null
if (sessionId) {
// 从cookie中获取sessionId存储到本地
wx.setStorageSync(sessionKey, sessionId)
}
}
if (sessionId) {
header['Cookie'] = [sessionKey, '=', sessionId].join('')
}
return header
}
function relogin() {}
module.exports = {
/**
* 域名
*/
domain: domain,
/**
* 请求接口封装
*/
req: request,
/**
* 获取会话ID
*/
getSessionId: function () {
return wx.getStorageSync(djSessionId)
},
/**
* 利用jscode登录
* code: 登录凭证
* appId: 应用ID
* success: 成功返回处理
*/
jscodeLogin: function (code, appId, success) {
request(
'/web-dj-weapp/jscode/login',
{
code: code,
appId: appId
},
{},
'GET',
'json',
'text',
success
)
},
/**
* 获取门店信息
*/
getMerchantBussiness(merchantId, success, fail) {
request(
'/web-dj-weapp/servicer/getMerchantBussiness',
{
merchantId: merchantId
},
{},
'GET',
'json',
'text',
success,
fail
)
},
/**
* 授权登录
* wechatAuthSetting:微信授权信息
*/
authLogin: function (wechatAuthSetting, success, fail) {
request(
'/web-dj-weapp/auth/login',
JSON.stringify(wechatAuthSetting),
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success,
fail
)
},
/**
* 授权手机号
* wechatAuthSetting:微信授权信息
*/
authPhone: function (wechatAuthSetting, success) {
request(
'/web-dj-weapp/auth/phone',
JSON.stringify(wechatAuthSetting),
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success
)
},
/**
* 获取小程序头像
* appId: 应用ID
*/
getHeadImage: function (appId, success) {
request('/web-dj-weapp/' + appId + '/headimage/get', null, {}, 'GET', 'json', 'text', success)
},
/**
* 获取小程序应用信息
* merchantId:商户号
*/
getAppInfo: function (merchantId, success) {
request(
'/web-dj-weapp/appinfo/get',
{
merchantId: merchantId
},
{
MerchantId: merchantId
},
'GET',
'json',
'text',
success
)
},
/** 我的消息 - 获取最新未读客服信息内容 */
getCustomerMessageNew: function (data, success, fail) {
request(
'/web-dj-weapp/remindServiceInfo/getCustomerMessageNew',
data,
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success,
fail
)
},
/**
* 分享二维码
*/
createQrcode: function (vo, success, fail) {
request('/web-dj-weapp/servicer/createQrcode', vo, {}, 'GET', 'json', 'text', success, fail)
},
/**
* 根据场景id获取场景值
*/
qrCodeScene: function (qrcodeScene, success, fail) {
request('/web-dj-weapp/home/qrCodeScene/getSceneById', qrcodeScene, {}, 'GET', 'json', 'text', success, fail)
},
/**
* 退出登录
* @param {成功回调方法} success
* @param {失败回调方法} fail
*/
logout: function (success, fail) {
request('/web-dj-weapp/logout', {}, {}, 'GET', 'json', 'text', success, fail)
},
/**
* 页面访问数据收集
* @param {收集数据} data
* @param {成功回调方法} success
* @param {失败回调方法} fail
*/
pageVisitCollect: function (data, success, fail) {
request(
'/web-dj-weapp/page/visit/collect',
data,
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success,
fail
)
}
}