http.js 11 KB

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