小程序统一的接口请求封装
let siteinfo = require('../config');
let util = require('util');
const md5Util = require('md5')
const BASE_URL= siteinfo.siteroot + '/';
const SECRET = siteinfo.miniapp
/*
* 签名认证
*/
const getSign = obj => {
let keys = Object.keys(obj)
let params = []
keys.sort()
keys.forEach(e => {
if (!util.isNull(obj[e])) {
params.push(e + '=' + util.trim(obj[e]))
}
})
params.push('key=' + SECRET)
let paramStr = params.join('&')
let signResult = md5Util.md5(paramStr).toUpperCase()
return signResult
}
/**
* 网络请求封装
* [@param](/user/param) url url路径名 例:/books
* [@param](/user/param) method 请求方式 POST/GET/DELETE等
* [@param](/user/param) data 请求参数 string类型
* [@param](/user/param) success 成功回调
* [@param](/user/param) fail 失败回调
*/
function request(url, method, data, success, fail) {
if (!fail && !success && typeof data === 'function') {
success = data;
data = {publickey:util.getRandom(6)}
data.sign = getSign(data)
}else if (!fail) {
if (typeof data === 'function') {
fail = success
success = data
data = ""
} else if (typeof data === 'object') {
data.sign = getSign(data)
} else {
console.log("传递参数类型不正确");
}
}
var wxtask = wx.request({
url: BASE_URL + url,
header: {
'content-type':'application/json', //默认 application/json :数据序列化
'Cookie':'PHPSESSID='+ wx.getStorageSync('session_id'),
},
method: method,
data: data,
success: function (res) {
switch (res.data.code) {
case 200: //请求成功
success(res.data)
break
case 204: //成功请求空内容。
success(res.data)
break
case 301: //永久跳转
wx.showModal({
content: res.data.msg,showCancel: false,
success: function (rel) {
wx.redirectTo({url: res.data.url})
}
})
break
case 302: //临时跳转
wx.showModal({
content: res.data.msg,
success: function (rel) {
if (rel.confirm) {
wx.navigateTo({url: res.data.url})
}
}
})
break
case 303: //临时跳转
wx.showModal({
content: res.data.msg,showCancel: false,
success: function (rel) {
if (rel.confirm) {
wx.switchTab({url: res.data.url})
}
}
})
break
case 404: //空也没后退
wx.showModal({
content: res.data.msg,showCancel:false,
success: function (rel) {
wx.navigateBack({delta:1})
}
})
break
case 401: //解决请求用户认证
wx.navigateTo({ url: '/pages/helper/login' })
break
case 403: //请求失败code弹出提示
wx.showModal({content:res.data.msg,showCancel:false})
break
case 500: //请求失败code调整
wx.redirectTo({url:'/pages/helper/error?msg=' + res.data.msg})
default:
break
}
},
fail: function (res) {
if (fail) {
fail(res)
}
}
})
return wxtask;
}
/**
* 请求封装-Get
* [@param](/user/param) url 请求地址
* [@param](/user/param) data 请求参数
* [@param](/user/param) success 成功回调
* [@param](/user/param) fail 失败回调
* [@constructor](/user/constructor)
*
* 返回值为微信请求实例 用于取消请求
*/
function Get(url, data, success, fail) {
return new Promise((resolve, reject) => {
return request(url, "GET", data, success, fail)
})
}
/**
* 请求封装-Post
* [@param](/user/param) url 请求地址
* [@param](/user/param) data 请求参数
* [@param](/user/param) success 成功回调
* [@param](/user/param) fail 失败回调
* [@constructor](/user/constructor)
*
* 返回值为微信请求实例 用于取消请求
*/
function Post(url, data, success, fail) {
return new Promise((resolve, reject) => {
return request(url, 'POST', data, success, fail)
})
}
/**
* 请求封装-PUT
* [@param](/user/param) url 请求地址
* [@param](/user/param) data 请求参数
* [@param](/user/param) success 成功回调
* [@param](/user/param) fail 失败回调
* [@constructor](/user/constructor)
*
* 返回值为微信请求实例 用于取消请求
*/
function Put(url, data, success, fail) {
return new Promise((resolve, reject) => {
return request(url, 'PUT', data, success, fail)
})
}
/**
* 请求封装-Delete
* [@param](/user/param) url 请求地址
* [@param](/user/param) data 请求参数
* [@param](/user/param) success 成功回调
* [@param](/user/param) fail 失败回调
* [@constructor](/user/constructor)
*
* 返回值为微信请求实例 用于取消请求
*/
function Delete(url, data, success, fail) {
return new Promise((resolve, reject) => {
return request(url, 'DELETE', data, success, fail)
})
}
exports.Get = Get;
exports.Post = Post;
exports.Put = Put;
exports.Delete = Delete;