network_util.js 11.2 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
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
var userDefaults = require('./userDefaults.js');
//  测试用户‘花雪莲’,手机号 15928512992  ,验证码666666, 测试用户:用户名 李蛋 ,电话 18382670036 验证码 666666 环信账户 mommybabymem968, 18382670036

// 0测试环境 1正式环境 2演示环境 3衡水 4本地调试
var Builing_Release_AppStore = 1;
// 获取服务器地址
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/';
case 4:
return 'http://192.168.2.5:8080/member_api_war/'
}
}

/*
* 获取既往史类型
* port: 接口名称 类型字符串
* params: 参数 类型json
* success: 成功回调 类型function
* fail: 失败回调 类型function
*/
function _get2(port, params, success, fail) {

wx.request({
url: 'https://rp-api.healthbaby.com.cn/' + 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();
}
})
}

/*
获取地址
* port: 接口名称 类型字符串
* params: 参数 类型json
* success: 成功回调 类型function
* fail: 失败回调 类型function
*/
function _getAddress(addr,params, success, fail) {
wx.request({
url: addr,
data: params,
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'application/json'
}, // 设置请求的 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();
}
})
}

/*
* 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) {
}
})
}
// 跨页面传参
function urlParams(item,e) {
let p = e.currentTarget.dataset;
// console.log(item, "item",e,p,p[item])
return p[item]
}








var introList = [

{ key: 'guideoftz', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/guideoftz.png' },
{ key: 'guideofxt', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/guideofxt.png' },
{ key: 'guideofxy', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/guideofxy.png' },

{ key: 'jingzhunzhidaogaowei', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/jingzhunzhidaogaowei.jpg' },
{ key: 'jingzhunzhidao', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/jingzhunzhidao.jpg' },
{ key: 'xuetangUrl', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/xuetangUrl.jpg' },
{ key: 'xueyaUrl', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/xueyaUrl.jpg' },
{ key: 'tizhongUrl', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/tizhongUrl.jpg' },
{ key: 'tiwenUrl', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/tiwenUrl.jpg' },

{ key: '1shouye', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/1shouye.jpg' },
{ key: '3yunqibaogao', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/3yunqibaogao.jpg' },
{ key: '4chanjianxuzhi', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/4chanjianxuzhi.jpg' },
{ key: '5chuangweiyuyue', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/5chuangweiyuyue.jpg' },
{ key: '6lianxiyiyuan', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/6lianxiyiyuan.jpg' },
{ key: '7yunfuxuexiao', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/7yunfuxuexiao.jpg' },
{ key: '8yunmabidu', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/8yunmabidu.jpg' },
{ key: '9gaoweijingzhun', url: 'https://dev-rp-api.healthbaby.com.cn/id_photo/app_guide/9gaoweijingzhun.jpg' }
]



function checkIsNeedDownload(){

if (wx.getStorageSync('delectFile') != 'delect1'){
console.log('开始删除')
wx.setStorageSync('delectFile', 'delect1')
userDefaults.celarnAllCache()
delectFile()
}
wx.getSavedFileList({
success: function (res) {
var loadCount = res.fileList.length
console.log('目前图片数目', loadCount)
downLoadStartIndex(loadCount)
}
})
}

function delectFile(){
wx.getSavedFileList({
success: function (res) {
if (res.fileList.length > 0) {
for (var i = 0; i < res.fileList.length; i++) {
wx.removeSavedFile({
filePath: res.fileList[i].filePath,
complete: function (res) {
console.log(res)
}
})
}
}
}
})
}

var isLoadImage = false

function downLoadStartIndex(index){
// wx.showToast({
// title: 'lalal',
// duration:300,
// })
if (index < introList.length) {
downLoadWithUrl(index)
}
}

function downLoadWithUrl(index) {
console.log('下载', index, isLoadImage)
// wx.showToast({
// title: index + "lalal",
// duration: 200,
// })
var lastLoadImage = wx.getStorageSync('lastLoadImage')

if ((lastLoadImage > 0 && lastLoadImage == index) || isLoadImage == true){
return;
}
isLoadImage = true
wx.setStorageSync('currentLoadImage', index)
wx.downloadFile({
url: introList[index].url,
success: function (res) {
if (res.statusCode === 200) {
wx.saveFile({
tempFilePath: res.tempFilePath,
success: function (res) {
console.log('下载成功', introList[index].key)
isLoadImage = false
wx.setStorageSync(introList[index].key, res.savedFilePath)
// wx.showToast({
// title: introList[index].key + "lalal",
// duration: 200,
// })
index++
if (index < introList.length) {
downLoadWithUrl(index)
}
}
})
} else {
// wx.showToast({
// title: "请求失败,请求失败",
// duration: 200,
// })
}
},
fail: function (res) {
// wx.showToast({
// title: "请求失败,请求失败",
// duration: 200,
// })
console.log(res)
wx.setStorageSync('currentLoadImage', index --)
isLoadImage = false
}
})
}



module.exports = {
_get: _get,
_get2: _get2,
_post: _post,
_put: _put,
showLoading: showLoading,
showErrorToast: showErrorToast,
Builing_Release_AppStore: Builing_Release_AppStore,
_getAddress: _getAddress,
checkIsNeedDownload: checkIsNeedDownload,
isLoadImage: isLoadImage,
urlParams:urlParams
}

// networkUtil._get(api,param,function(res){},function(res){})