network.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 网络请求公用方式,以后都这样用
  3. * 2018-5-10 16:57
  4. */
  5. var isTest = require('../utils/wxconstant.js').isTest()
  6. function request(url, params, success, fail) {
  7. this.requestLoading(url, params, "", success, fail)
  8. }
  9. function requestLoading(url, params, message, method,success, fail) {
  10. wx.showNavigationBarLoading()
  11. if (message != "") {
  12. wx.showLoading({
  13. title: message,
  14. })
  15. }
  16. wx.request({
  17. url: url,
  18. data: params,
  19. header: {
  20. //'Content-Type': 'application/json'
  21. 'content-type': 'application/x-www-form-urlencoded'
  22. },
  23. method: method,
  24. success: function (res) {
  25. if (isTest){
  26. console.log(method+':'+url + JSON.stringify(params), res)
  27. }
  28. wx.hideNavigationBarLoading()
  29. if (message != "") {
  30. wx.hideLoading()
  31. }
  32. if (res.statusCode == 200) {
  33. success(res)
  34. } else {
  35. fail()
  36. }
  37. },
  38. fail: function (res) {
  39. wx.hideNavigationBarLoading()
  40. if (message != "") {
  41. wx.hideLoading()
  42. }
  43. fail()
  44. },
  45. complete: function (res) {
  46. },
  47. })
  48. }
  49. module.exports = {
  50. request: request,
  51. requestLoading: requestLoading,
  52. }
  53. //引用:
  54. // var that = this
  55. // var data = { openid: wx.getStorageSync('openid') }
  56. // var url = baseUrl + BASEURL.getUrl.getAllNorm
  57. // netUtil.requestLoading(url, data, '正在加载数据...', 'POST', function (res) {
  58. // }, function (res) {
  59. // wx.showToast({
  60. // title: '加载数据失败',
  61. // icon: 'none',
  62. // duration: 2000
  63. // })
  64. // })