newRequest.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // 统一的请求发送
  2. import axios from 'axios'
  3. import qs from 'qs'
  4. import { Modal, message, notification } from 'ant-design-vue'
  5. import sysConfig from '@/config/index'
  6. import tool from '@/utils/tool'
  7. // 以下这些code需要重新登录
  8. const reloadCodes = [401, 1011007, 1011008]
  9. const errorCodeMap = {
  10. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  11. 401: '用户没有权限(令牌、用户名、密码错误)。',
  12. 403: '用户得到授权,但是访问是被禁止的。',
  13. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  14. 406: '请求的格式不可得。',
  15. 410: '请求的资源被永久删除,且不会再得到的。',
  16. 422: '当创建一个对象时,发生一个验证错误。',
  17. 500: '服务器发生错误,请检查服务器。',
  18. 502: '网关错误。',
  19. 503: '服务不可用,服务器暂时过载或维护。',
  20. 504: '网关超时。'
  21. }
  22. // 定义一个重新登录弹出窗的变量
  23. const loginBack = ref(false)
  24. // 创建 axios 实例
  25. const service = axios.create({
  26. baseURL: '/api', // api base_url
  27. timeout: sysConfig.TIMEOUT // 请求超时时间
  28. })
  29. // HTTP request 拦截器
  30. service.interceptors.request.use(
  31. (config) => {
  32. const token = tool.data.get('TOKEN')
  33. if (token) {
  34. config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token
  35. }
  36. if (!sysConfig.REQUEST_CACHE && config.method === 'get') {
  37. config.params = config.params || {}
  38. config.params._ = new Date().getTime()
  39. }
  40. Object.assign(config.headers, sysConfig.HEADERS)
  41. return config
  42. },
  43. (error) => {
  44. return Promise.reject(error)
  45. }
  46. )
  47. // 保持重新登录Modal的唯一性
  48. const error = () => {
  49. loginBack.value = true
  50. Modal.error({
  51. title: '提示:',
  52. okText: '重新登录',
  53. content: '登录已失效, 请重新登录',
  54. onOk: () => {
  55. loginBack.value = false
  56. tool.data.remove('TOKEN')
  57. tool.data.remove('USER_INFO')
  58. tool.data.remove('MENU')
  59. tool.data.remove('PERMISSIONS')
  60. window.location.reload()
  61. }
  62. })
  63. }
  64. // HTTP response 拦截器
  65. service.interceptors.response.use(
  66. (response) => {
  67. // 配置了blob,不处理直接返回文件流
  68. if (response.config.responseType === 'blob') {
  69. if (response.status === 200) {
  70. return response
  71. } else {
  72. message.warning('文件下载失败或此文件不存在')
  73. return
  74. }
  75. }
  76. const data = response.data
  77. const code = data.code
  78. if (reloadCodes.includes(code)) {
  79. if (!loginBack.value) {
  80. error()
  81. }
  82. return
  83. }
  84. if (code !== 200) {
  85. const customErrorMessage = response.config.customErrorMessage
  86. message.error(customErrorMessage || data.msg)
  87. return Promise.reject(data)
  88. // 自定义错误提示,覆盖后端返回的message
  89. // 使用示例:
  90. // export function customerList (data) {
  91. // return request('list', data, 'get', {
  92. // customErrorMessage: '自定义错误消息提示'
  93. // });
  94. // }
  95. } else {
  96. // 统一成功提示
  97. const responseUrl = response.config.url
  98. const apiNameArray = [
  99. // 'add',
  100. 'edit',
  101. 'delete',
  102. 'update',
  103. 'grant',
  104. 'reset',
  105. 'stop',
  106. 'pass',
  107. 'disable',
  108. 'enable',
  109. 'revoke',
  110. 'suspend',
  111. 'active',
  112. 'turn',
  113. 'adjust',
  114. 'reject',
  115. 'saveDraft'
  116. ]
  117. apiNameArray.forEach((apiName) => {
  118. if (responseUrl.includes(apiName)) {
  119. message.success(data.msg)
  120. }
  121. })
  122. }
  123. return Promise.resolve(data)
  124. },
  125. (error) => {
  126. if (error) {
  127. const status = 503
  128. const description = errorCodeMap[status]
  129. notification.error({
  130. message: '请求错误',
  131. description
  132. })
  133. return Promise.reject(status)
  134. }
  135. }
  136. )
  137. // 适配器, 用于适配不同的请求方式
  138. export const baseRequest = (url, value = {}, method = 'post', options = {}) => {
  139. url = sysConfig.API_URL + url
  140. if (method === 'post') {
  141. return service.post(url, value, options)
  142. } else if (method === 'get') {
  143. return service.get(url, { params: value, ...options })
  144. } else if (method === 'formdata') {
  145. // form-data表单提交的方式
  146. return service.post(url, qs.stringify(value), {
  147. headers: {
  148. 'Content-Type': 'multipart/form-data'
  149. },
  150. ...options
  151. })
  152. } else {
  153. // 其他请求方式,例如:put、delete
  154. return service({
  155. method: method,
  156. url: url,
  157. data: value,
  158. ...options
  159. })
  160. }
  161. }
  162. // 模块内的请求, 会自动加上模块的前缀
  163. export const moduleRequest =
  164. (moduleUrl) =>
  165. (url, ...arg) => {
  166. return baseRequest(moduleUrl + url, ...arg)
  167. }
  168. export default service