http.js 1.1 KB

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