newRequest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. console.log(response, '文件')
  68. // 配置了blob,不处理直接返回文件流
  69. if (response.config.responseType === 'blob') {
  70. if (response.status === 200) {
  71. return response
  72. } else {
  73. message.warning('文件下载失败或此文件不存在')
  74. return
  75. }
  76. }
  77. const data = response.data
  78. const code = data.code
  79. if (reloadCodes.includes(code)) {
  80. if (!loginBack.value) {
  81. error()
  82. }
  83. return
  84. }
  85. if (code !== 200) {
  86. const customErrorMessage = response.config.customErrorMessage
  87. message.error(customErrorMessage || data.msg)
  88. return Promise.reject(data)
  89. // 自定义错误提示,覆盖后端返回的message
  90. // 使用示例:
  91. // export function customerList (data) {
  92. // return request('list', data, 'get', {
  93. // customErrorMessage: '自定义错误消息提示'
  94. // });
  95. // }
  96. } else {
  97. // 统一成功提示
  98. const responseUrl = response.config.url
  99. const apiNameArray = [
  100. 'add',
  101. 'edit',
  102. 'delete',
  103. 'update',
  104. 'grant',
  105. 'reset',
  106. 'stop',
  107. 'pass',
  108. 'disable',
  109. 'enable',
  110. 'revoke',
  111. 'suspend',
  112. 'active',
  113. 'turn',
  114. 'adjust',
  115. 'reject',
  116. 'saveDraft'
  117. ]
  118. apiNameArray.forEach((apiName) => {
  119. if (responseUrl.includes(apiName)) {
  120. message.success(data.msg)
  121. }
  122. })
  123. }
  124. return Promise.resolve(data)
  125. },
  126. (error) => {
  127. if (error) {
  128. const status = 503
  129. const description = errorCodeMap[status]
  130. notification.error({
  131. message: '请求错误',
  132. description
  133. })
  134. return Promise.reject(status)
  135. }
  136. }
  137. )
  138. // 适配器, 用于适配不同的请求方式
  139. export const baseRequest = (url, value = {}, method = 'post', options = {}) => {
  140. url = sysConfig.API_URL + url
  141. if (method === 'post') {
  142. return service.post(url, value, options)
  143. } else if (method === 'get') {
  144. return service.get(url, { params: value, ...options })
  145. } else if (method === 'formdata') {
  146. // form-data表单提交的方式
  147. return service.post(url, qs.stringify(value), {
  148. headers: {
  149. 'Content-Type': 'multipart/form-data'
  150. },
  151. ...options
  152. })
  153. } else {
  154. // 其他请求方式,例如:put、delete
  155. return service({
  156. method: method,
  157. url: url,
  158. data: value,
  159. ...options
  160. })
  161. }
  162. }
  163. // 模块内的请求, 会自动加上模块的前缀
  164. export const moduleRequest =
  165. (moduleUrl) =>
  166. (url, ...arg) => {
  167. return baseRequest(moduleUrl + url, ...arg)
  168. }
  169. export default service