http.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import axios from 'axios'
  2. import qs from 'qs'
  3. import imgPath from "@/assets/image/jiazai.gif"
  4. const TIME_OUT_MS = 180 * 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. if(localStorage.getItem('lang') == 'en') {
  54. data.lang = 'english'
  55. }
  56. axios({
  57. method: 'post',
  58. url: handleUrl(url),
  59. data: handleParams(qs.stringify(data)),
  60. timeout: TIME_OUT_MS,
  61. headers: {
  62. //'Content-Type': 'application/json; charset=UTF-8'
  63. 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
  64. 'Token': token
  65. }
  66. }).then(
  67. (result) => {
  68. response(handleResults(result))
  69. }
  70. ).catch(
  71. (error) => {
  72. if (exception) {
  73. var str = error + ''
  74. if(str.indexOf('504') != '-1' || str.indexOf('502') != '-1') {
  75. if (flgs == 0) {
  76. timer = setTimeout(() => {
  77. prompt()
  78. }, 100)
  79. flgs++
  80. clearTimeout(timer2)
  81. timer2 = setTimeout(() => {
  82. flgs = 0
  83. }, 12000)
  84. }
  85. // exception(false)
  86. } else {
  87. exception(error)
  88. }
  89. } else {
  90. console.log(error, 3)
  91. }
  92. }
  93. )
  94. },
  95. WPGpost (url, data, response, exception) {
  96. let user = sessionStorage.getItem('user') , token = "";
  97. if(user != null){
  98. token = JSON.parse(user).id
  99. }
  100. axios({
  101. method: 'post',
  102. url: handleUrl(url),
  103. // timeout: TIME_OUT_MS,
  104. headers: {
  105. //'Content-Type': 'application/json; charset=UTF-8'
  106. 'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
  107. 'Token': token,
  108. 'app_id': '3bf356d5-bdba-48d4-b1f5-e91468beefa3',
  109. 'app_key': 'f2960f3bf3c5ca58ee0c6970c1242e87-1655272886142-371814'
  110. },
  111. data: {
  112. 'headers' : {
  113. 'Authorization' : ''
  114. },
  115. 'IT_TAB': {
  116. 'PSPHI': '',
  117. 'PROJK': '',
  118. 'GJAHR': '',
  119. 'MONAT': '',
  120. 'ZUNAM': '',
  121. 'ZID': '',
  122. 'ZDEP': '',
  123. 'DMBTR': '',
  124. }
  125. }
  126. }).then(
  127. (result) => {
  128. response(handleResults(result))
  129. }
  130. ).catch(
  131. (error) => {
  132. if (exception) {
  133. var str = error + ''
  134. if(str.indexOf('504') != '-1' || str.indexOf('502') != '-1') {
  135. if (flgs == 0) {
  136. timer = setTimeout(() => {
  137. prompt()
  138. }, 100)
  139. flgs++
  140. clearTimeout(timer2)
  141. timer2 = setTimeout(() => {
  142. flgs = 0
  143. }, 12000)
  144. }
  145. // exception(false)
  146. } else {
  147. exception(error)
  148. }
  149. } else {
  150. console.log(error, 3)
  151. }
  152. }
  153. )
  154. },
  155. /*
  156. * get 请求
  157. * @param url
  158. * @param response 请求成功时的回调函数
  159. * @param exception 异常的回调函数
  160. */
  161. get (url , response, exception) {
  162. let user = sessionStorage.getItem('user') , token = "";
  163. if(user != null){
  164. token = JSON.parse(user).id
  165. // data.token = token
  166. }
  167. axios({
  168. method: 'get',
  169. url: handleUrl(url),
  170. headers: {
  171. 'Content-Type': 'application/json; charset=UTF-8',
  172. 'Token': token
  173. }
  174. }).then(
  175. (result) => {
  176. response(handleResults(result))
  177. }
  178. ).catch(
  179. (error) => {
  180. if (exception) {
  181. exception(error)
  182. } else {
  183. console.log(error)
  184. }
  185. }
  186. )
  187. },
  188. /*
  189. * 导入文件
  190. * @param url
  191. * @param data
  192. * @param response 请求成功时的回调函数
  193. * @param exception 异常的回调函数
  194. */
  195. uploadFile (url, data, response, exception) {
  196. let user = sessionStorage.getItem('user') , token = "";
  197. if(user != null){
  198. token = JSON.parse(user).id
  199. // data.token = token
  200. }
  201. axios({
  202. method: 'post',
  203. url: handleUrl(url),
  204. data: handleParams(data),
  205. dataType: 'json',
  206. processData: false,
  207. contentType: false,
  208. headers: {
  209. 'Token': token
  210. }
  211. }).then(
  212. (result) => {
  213. response(handleResults(result, data))
  214. }
  215. ).catch(
  216. (error) => {
  217. if (exception) {
  218. exception(error)
  219. } else {
  220. console.log(error)
  221. }
  222. }
  223. )
  224. },
  225. /*
  226. * 下载文件用,导出 Excel 表格可以用这个方法
  227. * @param url
  228. * @param param
  229. * @param fileName 如果是导出 Excel 表格文件名后缀最好用.xls 而不是.xlsx,否则文件可能会因为格式错误导致无法打开
  230. * @param exception 异常的回调函数
  231. */
  232. downloadFile (url, data, fileName, exception) {
  233. var user = sessionStorage.getItem('user') , token = "";
  234. if(user != null){
  235. token = JSON.parse(user).id
  236. }
  237. axios({
  238. method: 'post',
  239. url: handleUrl(url),
  240. data: handleParams(qs.stringify(data)),
  241. responseType: 'blob',
  242. headers: {
  243. 'Token': token
  244. }
  245. }).then(
  246. (result) => {
  247. const excelBlob = result.data
  248. if ('msSaveOrOpenBlob' in navigator) {
  249. window.navigator.msSaveOrOpenBlob(excelBlob, fileName)
  250. } else {
  251. const elink = document.createElement('a')
  252. elink.download = fileName
  253. elink.style.display = 'none'
  254. const blob = new Blob([excelBlob])
  255. elink.href = URL.createObjectURL(blob)
  256. document.body.appendChild(elink)
  257. elink.click()
  258. document.body.removeChild(elink)
  259. }
  260. }
  261. ).catch(
  262. (error) => {
  263. if (exception) {
  264. exception(error)
  265. } else {
  266. console.log(error)
  267. }
  268. }
  269. )
  270. },
  271. uploadFileFormData (url, data, response, exception) {
  272. axios({
  273. method: 'post',
  274. url: handleUrl(url),
  275. data: data,
  276. // timeout: TIME_OUT_MS,
  277. headers: {
  278. 'Content-Type': 'multipart/form-data'
  279. }
  280. }).then(
  281. (result) => {
  282. response(handleResults(result))
  283. }
  284. ).catch(
  285. (error) => {
  286. if (exception) {
  287. exception(error)
  288. } else {
  289. console.log(error)
  290. }
  291. }
  292. )
  293. }
  294. }