http.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import axios from 'axios'
  2. import qs from 'qs'
  3. import imgPath from "@/assets/image/jiazai.gif"
  4. const TIME_OUT_MS = 60 * 1000 // 默认请求超时时间
  5. function prompt() {
  6. window.ELEMENT.Message({
  7. message: `<div style="display: flex;justify-content: center;flex-wrap: wrap;margin: 20px 0">
  8. <img src="${imgPath}" alt="" style="width: 122px;height: 85px;margin-bottom: 20px">
  9. <p style="width: 100%;text-align: center;color: #999"> 技术人员更新系统中,请一分钟后重试</p></div>`,
  10. type: 'info',
  11. dangerouslyUseHTMLString: true,
  12. center: true,
  13. duration: 10000,
  14. iconClass: 'el-icon'
  15. })
  16. console.log('执行多少次')
  17. }
  18. // 定时器
  19. let timer = null
  20. let timer2 = null
  21. let flgs = 0
  22. /*
  23. * @param response 返回数据列表
  24. */
  25. function handleResults (response) {
  26. let remoteResponse = response.data;
  27. return remoteResponse
  28. }
  29. function handleUrl (url) {
  30. url = BASE_URL + url;
  31. return url
  32. }
  33. /*
  34. * @param data 参数列表
  35. * @return
  36. */
  37. function handleParams (data) {
  38. return data
  39. }
  40. export default {
  41. /*
  42. * @param url
  43. * @param data
  44. * @param response 请求成功时的回调函数
  45. * @param exception 异常的回调函数
  46. */
  47. post (url, data, response, exception) {
  48. let user = sessionStorage.getItem('user') , token = "";
  49. if(user != null){
  50. token = JSON.parse(user).id
  51. // data.token = token
  52. }
  53. axios({
  54. method: 'post',
  55. url: handleUrl(url),
  56. data: handleParams(qs.stringify(data)),
  57. // timeout: TIME_OUT_MS,
  58. headers: {
  59. //'Content-Type': 'application/json; charset=UTF-8'
  60. 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
  61. 'Token': token
  62. }
  63. }).then(
  64. (result) => {
  65. response(handleResults(result))
  66. }
  67. ).catch(
  68. (error) => {
  69. if (exception) {
  70. var str = error + ''
  71. if(str.indexOf('504') != '-1' || str.indexOf('502') != '-1') {
  72. if (flgs == 0) {
  73. timer = setTimeout(() => {
  74. prompt()
  75. }, 100)
  76. flgs++
  77. clearTimeout(timer2)
  78. timer2 = setTimeout(() => {
  79. flgs = 0
  80. }, 12000)
  81. }
  82. // exception(false)
  83. } else {
  84. exception(error)
  85. }
  86. } else {
  87. console.log(error, 3)
  88. }
  89. }
  90. )
  91. },
  92. /*
  93. * get 请求
  94. * @param url
  95. * @param response 请求成功时的回调函数
  96. * @param exception 异常的回调函数
  97. */
  98. get (url , response, exception) {
  99. let user = sessionStorage.getItem('user') , token = "";
  100. if(user != null){
  101. token = JSON.parse(user).id
  102. // data.token = token
  103. }
  104. axios({
  105. method: 'get',
  106. url: handleUrl(url),
  107. headers: {
  108. 'Content-Type': 'application/json; charset=UTF-8',
  109. 'Token': token
  110. }
  111. }).then(
  112. (result) => {
  113. response(handleResults(result))
  114. }
  115. ).catch(
  116. (error) => {
  117. if (exception) {
  118. exception(error)
  119. } else {
  120. console.log(error)
  121. }
  122. }
  123. )
  124. },
  125. /*
  126. * 导入文件
  127. * @param url
  128. * @param data
  129. * @param response 请求成功时的回调函数
  130. * @param exception 异常的回调函数
  131. */
  132. uploadFile (url, data, response, exception) {
  133. let user = sessionStorage.getItem('user') , token = "";
  134. if(user != null){
  135. token = JSON.parse(user).id
  136. // data.token = token
  137. }
  138. axios({
  139. method: 'post',
  140. url: handleUrl(url),
  141. data: handleParams(data),
  142. dataType: 'json',
  143. processData: false,
  144. contentType: false,
  145. headers: {
  146. 'Token': token
  147. }
  148. }).then(
  149. (result) => {
  150. response(handleResults(result, data))
  151. }
  152. ).catch(
  153. (error) => {
  154. if (exception) {
  155. exception(error)
  156. } else {
  157. console.log(error)
  158. }
  159. }
  160. )
  161. },
  162. /*
  163. * 下载文件用,导出 Excel 表格可以用这个方法
  164. * @param url
  165. * @param param
  166. * @param fileName 如果是导出 Excel 表格文件名后缀最好用.xls 而不是.xlsx,否则文件可能会因为格式错误导致无法打开
  167. * @param exception 异常的回调函数
  168. */
  169. downloadFile (url, data, fileName, exception) {
  170. var user = sessionStorage.getItem('user') , token = "";
  171. if(user != null){
  172. token = JSON.parse(user).headImgurl
  173. }
  174. axios({
  175. method: 'post',
  176. url: handleUrl(url),
  177. data: handleParams(data),
  178. responseType: 'blob'
  179. }).then(
  180. (result) => {
  181. const excelBlob = result.data
  182. if ('msSaveOrOpenBlob' in navigator) {
  183. window.navigator.msSaveOrOpenBlob(excelBlob, fileName)
  184. } else {
  185. const elink = document.createElement('a')
  186. elink.download = fileName
  187. elink.style.display = 'none'
  188. const blob = new Blob([excelBlob])
  189. elink.href = URL.createObjectURL(blob)
  190. document.body.appendChild(elink)
  191. elink.click()
  192. document.body.removeChild(elink)
  193. }
  194. }
  195. ).catch(
  196. (error) => {
  197. if (exception) {
  198. exception(error)
  199. } else {
  200. console.log(error)
  201. }
  202. }
  203. )
  204. },
  205. uploadFileFormData (url, data, response, exception) {
  206. axios({
  207. method: 'post',
  208. url: handleUrl(url),
  209. data: data,
  210. // timeout: TIME_OUT_MS,
  211. headers: {
  212. 'Content-Type': 'multipart/form-data'
  213. }
  214. }).then(
  215. (result) => {
  216. response(handleResults(result))
  217. }
  218. ).catch(
  219. (error) => {
  220. if (exception) {
  221. exception(error)
  222. } else {
  223. console.log(error)
  224. }
  225. }
  226. )
  227. }
  228. }