http.js 2.3 KB

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