http.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import axios from 'axios'
  2. import qs from 'qs'
  3. const TIME_OUT_MS = 60 * 1000 // 默认请求超时时间
  4. /*
  5. * @param response 返回数据列表
  6. */
  7. function handleResults (response) {
  8. let remoteResponse = response.data;
  9. return remoteResponse
  10. }
  11. function handleUrl (url) {
  12. if(url.indexOf('tdatasizeinfowarehouseunits') > -1) {
  13. url = LINK_URL + url;
  14. } else {
  15. url = BASE_URL + url;
  16. }
  17. return url
  18. }
  19. /*
  20. * @param data 参数列表
  21. * @return
  22. */
  23. function handleParams (data) {
  24. return data
  25. }
  26. export default {
  27. /*
  28. * @param url
  29. * @param data
  30. * @param response 请求成功时的回调函数
  31. * @param exception 异常的回调函数
  32. */
  33. post (url, data, response, exception) {
  34. axios({
  35. method: 'post',
  36. url: handleUrl(url),
  37. data: handleParams(qs.stringify(data)),
  38. // timeout: TIME_OUT_MS,
  39. headers: {
  40. //'Content-Type': 'application/json; charset=UTF-8'
  41. 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
  42. }
  43. }).then(
  44. (result) => {
  45. response(handleResults(result))
  46. }
  47. ).catch(
  48. (error) => {
  49. if (exception) {
  50. exception(error)
  51. } else {
  52. console.log(error)
  53. }
  54. }
  55. )
  56. },
  57. /*
  58. * get 请求
  59. * @param url
  60. * @param response 请求成功时的回调函数
  61. * @param exception 异常的回调函数
  62. */
  63. get (url , response, exception) {
  64. axios({
  65. method: 'get',
  66. url: handleUrl(url),
  67. headers: {
  68. 'Content-Type': 'application/json; charset=UTF-8'
  69. }
  70. }).then(
  71. (result) => {
  72. response(handleResults(result))
  73. }
  74. ).catch(
  75. (error) => {
  76. if (exception) {
  77. exception(error)
  78. } else {
  79. console.log(error)
  80. }
  81. }
  82. )
  83. },
  84. /*
  85. * 导入文件
  86. * @param url
  87. * @param data
  88. * @param response 请求成功时的回调函数
  89. * @param exception 异常的回调函数
  90. */
  91. uploadFile (url, data, response, exception) {
  92. axios({
  93. method: 'post',
  94. url: handleUrl(url),
  95. data: handleParams(data),
  96. dataType: 'json',
  97. processData: false,
  98. contentType: false
  99. }).then(
  100. (result) => {
  101. response(handleResults(result, data))
  102. }
  103. ).catch(
  104. (error) => {
  105. if (exception) {
  106. exception(error)
  107. } else {
  108. console.log(error)
  109. }
  110. }
  111. )
  112. },
  113. /*
  114. * 下载文件用,导出 Excel 表格可以用这个方法
  115. * @param url
  116. * @param param
  117. * @param fileName 如果是导出 Excel 表格文件名后缀最好用.xls 而不是.xlsx,否则文件可能会因为格式错误导致无法打开
  118. * @param exception 异常的回调函数
  119. */
  120. downloadFile (url, data, response, exception) {
  121. axios({
  122. method: 'post',
  123. url: handleUrl(url),
  124. data: handleParams(qs.stringify(data)),//handleParams(data),
  125. responseType: 'blob'
  126. }).then(
  127. (result) => {
  128. // const excelBlob = result.data
  129. // if ('msSaveOrOpenBlob' in navigator) {
  130. // window.navigator.msSaveOrOpenBlob(excelBlob, fileName)
  131. // } else {
  132. // const elink = document.createElement('a')
  133. // elink.download = fileName
  134. // elink.style.display = 'none'
  135. // const blob = new Blob([excelBlob])
  136. // elink.href = URL.createObjectURL(blob)
  137. // document.body.appendChild(elink)
  138. // elink.click()
  139. // document.body.removeChild(elink)
  140. // }
  141. response(handleResults(result,data))
  142. }
  143. ).catch(
  144. (error) => {
  145. if (exception) {
  146. exception(error)
  147. } else {
  148. console.log(error)
  149. }
  150. }
  151. )
  152. },
  153. uploadFileFormData (url, data, response, exception) {
  154. axios({
  155. method: 'post',
  156. url: handleUrl(url),
  157. data: data,
  158. // timeout: TIME_OUT_MS,
  159. headers: {
  160. 'Content-Type': 'multipart/form-data'
  161. }
  162. }).then(
  163. (result) => {
  164. response(handleResults(result))
  165. }
  166. ).catch(
  167. (error) => {
  168. if (exception) {
  169. exception(error)
  170. } else {
  171. console.log(error)
  172. }
  173. }
  174. )
  175. }
  176. }