// 测试用户‘花雪莲’,手机号15928512992,验证码666666
// 0测试环境 1正式环境 2演示环境 3衡水
var Builing_Release_AppStore = 0
// 获取服务器地址
function kServerBaseUrl() {
switch (Builing_Release_AppStore) {
case 0:
return 'https://dev-app-member-api.healthbaby.com.cn/';
case 1:
return 'https://app-member-api.healthbaby.com.cn/';
case 2:
return 'https://stage-app-member-api.healthbaby.com.cn/';
case 3:
return 'https://weixin-bj.healthbaby.com.cn/';
}
}
/*
* port: 接口名称 类型字符串
* params: 参数 类型json
* success: 成功回调 类型function
* fail: 失败回调 类型function
*/
function _get(port, params, success, fail) {
console.log('---------------request_get-----------url:' + kServerBaseUrl() + port)
wx.request({
url: kServerBaseUrl() + port,
data: params,
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'application/json',
'Authorization': getApp().globalData.token ? getApp().globalData.token : ''
}, // 设置请求的 header
success: function (res) {
// success
successData(res, success, fail)
},
fail: function (res) {
var error = {}
error.errorcode = 404
error.errormsg = '数据获取失败,请稍后再试'
console.log(res)
// fail
fail(error)
},
complete: function (res) {
// complete
wx.hideToast()
// 小程序提供的api,通知页面停止下拉刷新效果
wx.stopPullDownRefresh();
}
})
}
function _post(port, params, success, fail, contentType) {
console.log('---------------request_post-----------url:' + port)
wx.request({
url: kServerBaseUrl() + port,
data: params,
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': contentType ? contentType : 'application/x-www-form-urlencoded',
'Authorization': getApp().globalData.token ? getApp().globalData.token : ''
}, // 设置请求的 header
success: function (res) {
// success
successData(res, success, fail)
},
fail: function (res) {
var error = {}
error.errorcode = 404
error.errormsg = '数据获取失败,请稍后再试'
console.log(res)
// fail
fail(error)
},
complete: function () {
// complete
wx.hideToast()
// 小程序提供的api,通知页面停止下拉刷新效果
wx.stopPullDownRefresh;
}
})
}
function _put(port, params, success, fail, contentType) {
console.log('---------------request_PUT-----------url:' + port)
wx.request({
url: kServerBaseUrl() + port,
data: params,
method: 'PUT', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': contentType ? contentType : 'application/x-www-form-urlencoded',
'Authorization': getApp().globalData.token ? getApp().globalData.token : ''
}, // 设置请求的 header
success: function (res) {
// success
successData(res, success, fail)
},
fail: function (res) {
var error = {}
error.errorcode = 404
error.errormsg = '数据获取失败,请稍后再试'
console.log(res)
// fail
fail(error)
},
complete: function () {
// complete
wx.hideToast()
// 小程序提供的api,通知页面停止下拉刷新效果
wx.stopPullDownRefresh;
}
})
}
function successData(res, success, fail) {
if (res.errorcode && res.errorcode == 0) {
success(res)
return
}
if (res.statusCode == 200) {
if (res.data.errorcode == 0) {
success(res)
} else {
var error = {}
error.errorcode = res.data.errorcode
error.errormsg = res.data.errormsg ? res.data.errormsg : '数据获取失败,请稍后再试'
console.log(res)
fail(error)
}
} else {
var error = {}
error.errorcode = res.statusCode
error.errormsg = '数据获取失败,请稍后再试'
fail(error)
}
}
function showLoading(text) {
wx.showToast({
title: text == null ? '加载中...' : text,
icon: 'loading',
duration: 60000
})
}
function showErrorToast(text) {
wx.showModal({
title: '提示',
content: text,
showCancel: false,
success: function (res) {
}
})
}
module.exports = {
_get: _get,
_post: _post,
_put: _put,
showLoading: showLoading,
showErrorToast: showErrorToast,
Builing_Release_AppStore: Builing_Release_AppStore,
}
// networkUtil._get(api,param,function(res){},function(res){})