request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import axios from "axios"
  2. import XEUtils from "xe-utils"
  3. import sysConfig from "@/config"
  4. import tool from "@/utils/tool"
  5. import router from "@/router"
  6. axios.defaults.baseURL = ""
  7. axios.defaults.timeout = sysConfig.TIMEOUT
  8. // HTTP request 拦截器
  9. axios.interceptors.request.use(
  10. (config) => {
  11. let token = tool.cookie.get("MES_TOKEN")
  12. if (token && XEUtils.startsWith(token, sysConfig.TOKEN_PREFIX)) {
  13. config.headers[sysConfig.TOKEN_NAME] = token
  14. }
  15. if (!sysConfig.REQUEST_CACHE && config.method == "get") {
  16. config.params = config.params || {}
  17. config.params["_"] = new Date().getTime()
  18. }
  19. Object.assign(config.headers, sysConfig.HEADERS)
  20. return config
  21. },
  22. (error) => {
  23. return Promise.reject(error)
  24. }
  25. )
  26. //FIX 多个API同时401时疯狂弹窗BUG
  27. let MessageBox_401_show = false
  28. // HTTP response 拦截器
  29. axios.interceptors.response.use(
  30. (response) => response,
  31. (error) => {
  32. if (error.response) {
  33. if (error.response.status == 404) {
  34. ElNotification.error({
  35. title: "请求错误",
  36. message: "Status:404,正在请求不存在的服务器记录!"
  37. })
  38. } else if (error.response.status == 503) {
  39. console.log(error.response.data.message)
  40. } else if (error.response.status == 401) {
  41. if (!MessageBox_401_show) {
  42. MessageBox_401_show = true
  43. ElMessageBox.confirm("当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。", "无权限访问", {
  44. customStyle: { "--el-messagebox-width": "480px" },
  45. type: "error",
  46. center: true,
  47. showClose: false,
  48. closeOnPressEscape: false,
  49. closeOnClickModal: false,
  50. confirmButtonText: "重新登录",
  51. showCancelButton: false,
  52. beforeClose: (action, instance, done) => {
  53. MessageBox_401_show = false
  54. done()
  55. }
  56. }).then(() => {
  57. tool.cookie.remove("MES_TOKEN")
  58. tool.data.remove("USER_INFO")
  59. router.replace({ path: "/login" })
  60. }).catch(() => {})
  61. }
  62. } else {
  63. ElNotification.error({
  64. title: "请求错误",
  65. message: error.response.data.message || `Status:${error.response.status},未知错误!`
  66. })
  67. }
  68. return Promise.reject(error.response)
  69. } else {
  70. ElNotification.error({
  71. title: "请求错误",
  72. message: "请求服务器无响应!"
  73. })
  74. return Promise.reject(error)
  75. }
  76. }
  77. )
  78. var http = {
  79. /** get 请求
  80. * @param {string} url 接口地址
  81. * @param {object} params 请求参数
  82. * @param {object} config 参数
  83. */
  84. get: function (url, params = {}, config = {}) {
  85. return new Promise((resolve, reject) => {
  86. axios({
  87. url,
  88. method: "get",
  89. params,
  90. ...config
  91. }).then(response => {
  92. resolve(response.data)
  93. }).catch(error => {
  94. reject(error)
  95. })
  96. })
  97. },
  98. /** post 请求
  99. * @param {string} url 接口地址
  100. * @param {object} data 请求参数
  101. * @param {object} config 参数
  102. * @param {object} params 路径请求参数
  103. */
  104. post: function (url, data = {}, config = {}, params = {}) {
  105. return new Promise((resolve, reject) => {
  106. let emptyField = []
  107. if (!XEUtils.isFormData(data)) {
  108. emptyField = XEUtils.keys(XEUtils.pick(data, val => val === "" || XEUtils.isNull(val) || XEUtils.isUndefined(val)))
  109. // 更新接口 置空
  110. url.endsWith("/update") && XEUtils.set(data, "emptyField", emptyField)
  111. }
  112. axios({
  113. url,
  114. method: "post",
  115. data: emptyField.length ? XEUtils.omit(data, emptyField) : data,
  116. ...config,
  117. params
  118. }).then(response => {
  119. resolve(response.data)
  120. }).catch(error => {
  121. reject(error)
  122. })
  123. })
  124. }
  125. }
  126. export default http