http.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const isTest = true;
  2. const baseUrl = isTest ? 'http://app.rt/api/mini/' : 'http://t18.9026.com/api/mini/';
  3. const http = (data) => {
  4. var data = Object.assign({}, {
  5. url: '',
  6. method: 'POST',
  7. data: {},
  8. success: null,
  9. error: null,
  10. loadTitle: '加载中',
  11. showLoading: true
  12. }, data)
  13. if (data.showLoading) {
  14. wx.showLoading({
  15. title: data.loadTitle,
  16. })
  17. }
  18. var userinfo = wx.getStorageSync('sg-userinfo')
  19. var token = userinfo ? userinfo.token : ''
  20. wx.request({
  21. url: baseUrl + data.url,
  22. method: data.method,
  23. data: data.data,
  24. header: {
  25. 'X-Token': token
  26. },
  27. success: function (res) {
  28. if (data.showLoading) wx.hideLoading()
  29. if(res.statusCode != 200) {
  30. wx.showToast({
  31. title: res.data.message,
  32. icon: 'none'
  33. })
  34. return false
  35. }
  36. if (res.data.code != 0) {
  37. wx.showToast({
  38. title: res.data.msg,
  39. icon: 'none'
  40. })
  41. }
  42. typeof data.success === "function" && data.success(res.data)
  43. },
  44. fail: function (res) {
  45. console.log(res)
  46. typeof data.error === "function" && data.error(res.data)
  47. }
  48. })
  49. }
  50. module.exports = http