http.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const defaultHeader = {
  2. 'Content-Type': 'application/x-www-form-urlencoded'
  3. }
  4. const jheader = {
  5. 'Content-Type': 'application/json'
  6. }
  7. // const url = 'http://118.24.176.28:8778'
  8. const url = 'https://api.winwin-sport.com'
  9. let RQ = []
  10. function request(api, method, header, data, hideLoading) {
  11. if (RQ.indexOf(api) > -1) {
  12. return console.error('http duplicate api:', api)
  13. }
  14. RQ.push(api)
  15. if (!hideLoading) {
  16. wx.showLoading({
  17. title: '加载中...'
  18. })
  19. }
  20. let pdata = {
  21. "companyId": 0,
  22. "from": 0,
  23. "page": 1,
  24. "reqdata": {},
  25. "rows": 10,
  26. "sidx": "",
  27. "sord": "",
  28. "token": "",
  29. "userId": wx.getStorageSync('id')||''
  30. }
  31. data = data || {}
  32. Object.assign(pdata, data)
  33. // data.token = getApp().data.at
  34. return new Promise((resolve, reject) => {
  35. wx.request({
  36. url: url + api,
  37. method: method,
  38. header: header,
  39. data: pdata,
  40. success: r => {
  41. if (r.statusCode == 200) {
  42. resolve(r.data)
  43. } else {
  44. wx.showToast({
  45. title: '系统繁忙!'
  46. })
  47. }
  48. },
  49. fail: e => {
  50. reject(e)
  51. },
  52. complete: e => {
  53. if (!hideLoading) {
  54. wx.hideLoading()
  55. }
  56. RQ.splice(RQ.indexOf(api), 1)
  57. }
  58. })
  59. })
  60. }
  61. function uploadFile(api, data, file, hideLoading) {
  62. if (!hideLoading) {
  63. wx.showLoading({
  64. title: '上传中...'
  65. })
  66. }
  67. data = data || {}
  68. // data.token = getApp().data.at
  69. return new Promise((resolve, reject) => {
  70. wx.uploadFile({
  71. url: url + api,
  72. filePath: file,
  73. name: 'file',
  74. formData: data,
  75. success(r) {
  76. if (r.statusCode == 200) {
  77. console.log(r);
  78. resolve({url: r.data, state: 100})
  79. } else {
  80. console.log(r);
  81. console.error(r.errMsg)
  82. wx.showToast({
  83. title: '系统繁忙!'
  84. })
  85. }
  86. },
  87. fail(e) {
  88. reject(e)
  89. },
  90. complete(e) {
  91. if (!hideLoading) {
  92. wx.hideLoading()
  93. }
  94. }
  95. })
  96. })
  97. }
  98. module.exports = {
  99. post(api, data, hideLoading) {
  100. return request(api, "POST", jheader, data, hideLoading)
  101. },
  102. get(api, data, hideLoading) {
  103. return request(api, "GET", defaultHeader, data, hideLoading)
  104. },
  105. upload(api, data, file, hideLoading) {
  106. return uploadFile(api, data, file, hideLoading)
  107. }
  108. }