restapiQrPay.js
5.46 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
// const restapi = require('../utils/util.js')
// const domain = 'https://dj.test.glsx.com.cn'
const domain = 'https://dj.dev.glsx.com.cn'
// import restapi from '../utils/restapi.js'
// console.log(restapi)
// const restapi = require('../utils/restapi.js')
const restapi = {}
restapi.req = request
// console.log(restapi.req)
/**
* 通用请求
* 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: 'https://dj.dev.glsx.com.cn' + url,
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
}
module.exports = {
/** 获取订单数据*/
GETORDERINFO: function (oParams, success, fail) {
restapi.req('/web-dj-weapp/smartcarlife/goodsOrder/createWxPayOrder', oParams, {}, 'GET', 'json', 'text', success, fail)
},
/**
* 根据场景id获取场景值
*/
qrCodeScene: function (qrcodeScene, success, fail) {
restapi.req('/web-dj-weapp/home/qrCodeScene/getSceneById', qrcodeScene, {}, 'GET', 'json', 'text', success, fail)
},
/** 查询支付结果 */
queryPayOrderStatus: function (params, success, fail) {
restapi.req(
'/web-dj-weapp/smartcarlife/goodsOrder/queryPayOrderStatus',
params,
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success,
fail
)
},
/** 发起微信支付 */
createWxPayOrderH5: function (params, success, fail) {
restapi.req(
'/web-dj-weapp/smartcarlife/goodsOrder/createWxPayOrderH5',
params,
// {
// 'content-type': 'application/x-www-form-urlencoded'
// },
{
'content-type': 'application/json'
},
'POST',
'json',
'text',
success,
fail
)
},
/**
* 授权登录
* wechatAuthSetting:微信授权信息
*/
authOpenId: function (params, success, fail) {
restapi.req(
'/web-dj-weapp/jscode/getOpenId',
// JSON.stringify(code),
params,
{
'content-type': 'application/x-www-form-urlencoded'
},
'POST',
'json',
'text',
success,
fail
)
},
/** 获取园区店加微二维码 */
yqMerchantDetail: function (params, success, fail) {
restapi.req(
'/web-dj-weapp/smartcarlife/yqMerchant/getYqMerchantConfig',
// JSON.stringify(code),
params,
{
'content-type': 'application/x-www-form-urlencoded'
},
// 'POST',
'GET',
'json',
'text',
success,
fail
)
},
/** 获取隐私协议标题查询 */
getTitleByModule: function (params, success, fail) {
restapi.req(
'/rop-dj-car-boutique/router?method=dj.api.smartcarlife.privacyAgreement.getTitleByModule',
// '/rop-dj-car-boutique/smartcarlife/privacyAgreement/getTitleByModule',
// JSON.stringify(code),
params,
{
'content-type': 'application/x-www-form-urlencoded'
},
'POST',
// 'GET',
'json',
'text',
success,
fail
)
}
}