请求报文示例 请求报文示例
// request.js https://XXXXXXXX/XXX/yy/yyy
var API_URL = 'https://XXXXXXXX/XXX/'
function request(method, requestHandler) {
var api_key = 'XXX' //用于加密的key
var APPEND_URL = requestHandler.url
var tradeId = requestHandler.tradeId
// 获取当前时间戳
var timestamp = getDate(new Date)
var param = {
"head": {
"timestamp": timestamp,
"tradeId": tradeId,
"validCode": "",
// "userId" : wx.getStorageSync('userId'),
// "token" : wx.getStorageSync('token')
},
"body": {
}
}
//封装head节dian
var head_dic = param.head
var plainStr
if( wx.getStorageSync('userId') != null || wx.getStorageSync('token')!= null) {
head_dic.userId = wx.getStorageSync('userId')
head_dic.token = wx.getStorageSync('token')
plainStr = head_dic.tradeId + head_dic.timestamp +head_dic.userId+head_dic.token+api_key
} else {
plainStr = head_dic.tradeId + head_dic.timestamp +api_key
}
var validCode1 = utilMd5.hexMD5(plainStr) // md5加密
var validCode = validCode1.toUpperCase()
head_dic.validCode = validCode
param.head = head_dic
param.body = requestHandler.params
wx.request({
url: API_URL + APPEND_URL+'.json',
data: { param: JSON.stringify(param) }, //json转son字符串
method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-Type': 'application/x-www-form-urlencoded'
}, // 设置请求的 header
success: function (res) {
//注意:可以对参数解密等处理
requestHandler.success(res)
},
fail: function () {
requestHandler.fail()
},
complete: function () {
// complete
}
})
}
var requestHandler = {
url: '',
tradeId:'',
params: {},
success: function (res) {
// success
},
fail: function () {
// fail
},
}
// GET
function GET(requestHandler) {
request('GET', requestHandler)
}
// POST
function POST(requestHandler) {
request('POST', requestHandler)
}
module.exports = {
GET: GET,
POST: POST,
showToast: showToast
}
调用
createShopRequest: function () {
var that = this
var params = new Object()
params.shopName = ''
requestServer.POST(
{
url: 'yy/yyy',
tradeId: 'yyy',
params: params,
success: function (res) {
console.log(res.data)
},
fail: function () {
},