network_util.js 4.55 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
// 测试用户‘花雪莲’,手机号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){})