request.js
3.98 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
import { secretKey, pathEnum, domain, sign } from './http.js'
import { getSessionId } from '~/utils/cache/storageApi'
import network from '~/utils/network'
/**接口 */
class Request {
get(params, returnAll = false) {
return this.request('GET', params, returnAll)
}
post(params, returnAll = false) {
return this.request('POST', params, returnAll)
}
async request(RequestMethods, params, returnAll) {
const type = await network.getNetworkType()
if (type == 'none') return
return new Promise((resolve, reject) => {
wx.showNavigationBarLoading()
let header =
RequestMethods === 'POST'
? {
'content-type': 'application/x-www-form-urlencoded'
}
: {}
let projectName = this.getRopProject(params.method)
if (!projectName) projectName = this.getRopProject(params.method)
let path = pathEnum[projectName]
if (!path) path = `/rop-scrm-${projectName}/`
params = this.getData(params)
wx.request({
url: `${domain}${path}router?method=${params.method}`,
data: params,
method: RequestMethods,
header,
dataType: 'json',
responseType: 'text',
success(response) {
/**业务代码 */
let {
data: { code, message },
data
} = response
if (code == 20 || code == 21) {
wx.p.storage.rm('sessionId')
wx.p.storage.rm('userInfo')
const thisPage = wx.p.router.getPageInstance(),
autoLoginObject = wx.p.storage.get('auto_login_object')
if (thisPage.route == 'login/loading/loading') {
resolve(data)
return
} else {
if (autoLoginObject) wx.p.router.reLaunch('login.loading')
else wx.p.router.reLaunch('login.index')
}
} else {
if (returnAll) {
resolve(data)
return
}
if (code == '0') {
resolve(data.data)
} else {
// console.trace(data)
wx.showToast({
title: message || '系统异常',
icon: 'none'
})
}
}
},
fail(error) {
wx.hideLoading()
console.log(`接口异常:`, error)
wx.showToast({
title: '系统异常',
icon: 'error'
})
reject(error)
},
complete() {
wx.stopPullDownRefresh()
wx.hideNavigationBarLoading()
}
})
})
}
getData(params) {
let projectName = params.projectName
if (!projectName) projectName = this.getRopProject(params.method)
else delete params.projectName
let appKey = '48e5e13229b82c1b4e6e8c96151f0637'
let secret = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
if (secretKey[projectName]) {
appKey = secretKey[projectName].appKey
secret = secretKey[projectName].secret
}
const sessionId = getSessionId()
let noSignAttr = []
if (!params.sessionId && sessionId) params.sessionId = sessionId
if (params.noSignAttr) {
noSignAttr = params.noSignAttr
delete params.noSignAttr
}
params.v = '1.0.0'
params.format = 'json'
params.locale = 'zh_CN'
params.source = 'dj-wechat'
params.appKey = appKey
return sign(params, secret, noSignAttr)
}
getRopProject(method) {
const projectName = method.split('.')[2]
return projectName
}
getMock() {
let url = 'https://oss-config.dev.glsx.com.cn/mock/6264ff4760f415276f6dfe62/config'
if (index == 3) url = 'https://oss-config.test.glsx.com.cn/mock/6274e3050da5034a58680aa6/config'
return new Promise(resolve => {
wx.request({
url,
data: {},
method: 'GET',
header: {},
dataType: 'json',
responseType: 'text',
success(response) {
resolve(response.data)
}
})
})
}
}
export default new Request()