You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.0 KiB
104 lines
3.0 KiB
var utilMd5 = require('md5.js');
|
|
var userInfo = wx.getStorageSync('userInfo');
|
|
var _header = {
|
|
"Content-Type": "application/json",
|
|
"HTTP_USERID": userInfo.id,
|
|
"HTTP_SUBUSERID": userInfo.subaccount_id
|
|
}
|
|
//请求密钥设置
|
|
var nonic = 654321;
|
|
var appid = 'Wechat-1501448888';
|
|
var secret = '9ed13d2eb2482473b20f37a334eac05019426296';
|
|
var openkey = utilMd5.md5(appid + nonic + secret);
|
|
|
|
function sendGet(url, data = {}) {
|
|
var userInfo = wx.getStorageSync('userInfo'); //这里要重新获取,因为引入这个文件只会引入一次
|
|
var header = this._header = {
|
|
"Content-Type": "application/json",
|
|
"USERID": userInfo.id > 0 ? userInfo.id : 0,
|
|
"CLIENT": "WECHAT",
|
|
"APPID": appid,
|
|
"NONCE": nonic,
|
|
"OPENKEY": openkey,
|
|
"SUBUSERID": userInfo.subaccount_id > 0 ? userInfo.subaccount_id : 0
|
|
}
|
|
return sendRequest(url, data, header, 'GET')
|
|
}
|
|
//post请求
|
|
function sendPost(url, data = {}) {
|
|
var userInfo = wx.getStorageSync('userInfo'); //这里要重新获取,因为引入这个文件只会引入一次
|
|
var header = this._header = {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"USERID": userInfo.id > 0 ? userInfo.id : 0,
|
|
"CLIENT": "WECHAT",
|
|
"APPID": appid,
|
|
"NONCE": nonic,
|
|
"OPENKEY": openkey,
|
|
"SUBUSERID": userInfo.subaccount_id > 0 ? userInfo.subaccount_id : 0
|
|
}
|
|
return sendRequest(url, data, header, 'POST')
|
|
}
|
|
//网络请求的工具类
|
|
function sendRequest(url, data = {}, header = this._heade, method = 'get') {
|
|
// console.log("url::" + url + data + header);
|
|
// console.log(header, 'header')
|
|
return new Promise((resolve, reject) => {
|
|
wx.showLoading({
|
|
title: '加载中',
|
|
})
|
|
wx.request({
|
|
url: url,
|
|
data: data,
|
|
method: method,
|
|
header: header,
|
|
dataType: 'json', //默认也是json的,这里可以改成其他的
|
|
success: function(res) {
|
|
// console.log(res);
|
|
if (res.statusCode === 200) {
|
|
if (res.data.code == 415) {
|
|
wx.redirectTo({
|
|
url: '/pages/login/index',
|
|
})
|
|
}
|
|
if (res.data.code == 416) {
|
|
wx.redirectTo({
|
|
url: '/pages/member/pay',
|
|
})
|
|
}
|
|
if (res.data.code == 51016){
|
|
wx.showModal({
|
|
title: '温馨提示',
|
|
content: res.data.msg,
|
|
showCancel:true,
|
|
success(res) {
|
|
if (res.confirm) {
|
|
wx.switchTab({
|
|
url: '/pages/member/index',
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消')
|
|
}
|
|
}
|
|
})
|
|
|
|
}
|
|
//200: 服务端业务处理正常结束
|
|
resolve(res)
|
|
} else {
|
|
reject(res)
|
|
}
|
|
wx.hideLoading()
|
|
},
|
|
fail: function(res) {
|
|
reject(res)
|
|
wx.hideLoading()
|
|
}
|
|
})
|
|
})
|
|
|
|
}
|
|
//这里暴露两个方法就行了
|
|
module.exports = {
|
|
sendGet: sendGet,
|
|
sendPost: sendPost
|
|
}
|