request.js
3.8 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
import { secretKey, domain, path, sign,scrmSecretKey } from './http.js'
import util from '~/utils/util.js'
/**接口 */
class Request {
get(method, data, noSignAttr, requestPath, domainName) {
return this.request('GET', method, data, noSignAttr, requestPath, domainName)
}
post(method, data, noSignAttr, requestPath, domainName) {
return this.request('POST', method, data, noSignAttr, requestPath, domainName)
}
request(RequestMethods, method, data, noSignAttr = [], requestPath, domainName = 'dj') {
let _this = this
return new Promise((resolve, reject) => {
wx.showNavigationBarLoading()
if (!data) data = {}
let header =
RequestMethods === 'POST'
? {
'content-type': 'application/x-www-form-urlencoded'
}
: {}
data = _this.getData(data, method, noSignAttr)
wx.request({
url: `${domain[domainName]}${requestPath || path}?method=${method}`,
data,
method: RequestMethods,
header,
dataType: 'json',
responseType: 'text',
success(response) {
/**业务代码 */
let {
data: { code, message },
data
} = response
// // console.log(thisPage, 'thisPage')
// let { route, data: pageData } = thisPage
// if (pageData.shareQuery) {
// wx.$util.setStorageSync('shareQuery', {
// route,
// shareQuery: pageData.shareQuery
// })
// }
if (code == 20 || code == 21) {
let thisPage = wx.$router.getPageInstance()
// console.log(thisPage);
wx.hideLoading()
if (thisPage.route != 'pages/wxLogin/wxLogin') {
wx.showModal({
title: '温馨提示',
content: '登录过期,请重新登录。',
showCancel: false,
confirmText: '去登录',
confirmColor: '#3582EE',
success(res) {
if (res.confirm) {
getApp().login(true)
}
}
})
}
} else if (code == -1) {
wx.showToast({
title: message || '网络请求失败',
icon: 'none'
})
resolve(data)
} else {
resolve(data)
}
},
fail(error) {
wx.hideLoading()
console.error(`接口异常:${domain[domainName]}${requestPath || path}?method=${method}`, error)
wx.showToast({
title: '系统异常',
icon: 'error'
})
reject(error)
// if (fail && typeof fail == 'function') {
// fail(error, reject)
// } else {
// }
},
complete() {
wx.stopPullDownRefresh()
wx.hideNavigationBarLoading()
}
})
})
}
getData(data, method, noSignAttr) {
let sessionId = util.getSessionId()
if (sessionId) data['sessionId'] = sessionId
console.log(data,'request');
data['v'] = '1.0.0'
data['method'] = method
data['format'] = 'json'
data['locale'] = 'zh_CN'
data['source'] = 'dj-wechat'
// 兼容线上appkey和secret不一样的情况
let djapi = data.method.includes('dj.api')
let scmrapi = data.method.includes('scrm.api')
console.log(scmrapi,'scmrapi');
const appKey = scmrapi?scrmSecretKey.appKey:djapi?secretKey.appKey:''
const secret = scmrapi?scrmSecretKey.secret:djapi?secretKey.secret:''
data['appKey'] = appKey
let obj = sign(data, secret, noSignAttr)
//data['sign'] = obj.sign;
return obj
}
}
export default new Request()