request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import axios from 'axios'
  2. import {apiPrefix} from '@/config'
  3. import {MessageBox, Notification} from 'element-ui'
  4. import Message from '@ele/component/Message'
  5. import store from '@/store'
  6. import {isLogin} from '@/util/auth'
  7. const instance = axios.create({
  8. baseURL: apiPrefix,
  9. // withCredentials: true, // send cookies when cross-domain requests
  10. timeout: 60000 // request timeout
  11. })
  12. instance.interceptors.request.use(
  13. config => {
  14. //header添加token
  15. if (store.state.user.token) {
  16. config.headers['X-Token'] = store.state.user.token
  17. }
  18. return config
  19. },
  20. error => Promise.reject(error)
  21. )
  22. instance.interceptors.response.use(
  23. response => {
  24. const res = response.data, {responseType = 'json'} = response.config
  25. //当返回类型非{status,data,msg}的接口请求时,不使用status来判断请求是否成功
  26. if (!('status' in res) || res.status === 200) {
  27. //当返回类型为json时,返回response.data
  28. return responseType === 'json' ? res : response
  29. }
  30. //服务器异常
  31. if (res.status === 500) {
  32. Message.error(res.msg || '操作失败')
  33. return Promise.reject(res.msg)
  34. }
  35. //未登录
  36. if (res.status === 401) {
  37. if (store.state.user.prepareLogout) return Promise.reject()
  38. return MessageBox.alert('请登录后重试', {
  39. type: 'warning',
  40. beforeClose: (action, instance, done) => {
  41. store.dispatch('user/logout').then(done)
  42. }
  43. })
  44. }
  45. //没有权限
  46. if (res.status === 403) {
  47. Message.error(res.msg || '没有权限进行该操作')
  48. return Promise.reject(res.msg)
  49. }
  50. //其他错误
  51. Message.error(res.msg || '接口有误')
  52. return Promise.reject(res)
  53. },
  54. error => {
  55. if (axios.isCancel(error)) return
  56. error && Notification.error({
  57. title: '错误',
  58. message: '请求错误,请稍后重试'
  59. })
  60. return Promise.reject(error)
  61. }
  62. )
  63. class Api {
  64. /**
  65. * 数据接口定义
  66. * @param url 请求url,不带参数
  67. * @param arg 对传入参数的处理方法,返回值将作为axios[get,post]的第二个参数
  68. * @param chain 形参为请求返回的promise
  69. * @param method 请求方法,小写,get、post...
  70. */
  71. constructor(url, arg, chain, method) {
  72. this.url = url
  73. this.arg = arg
  74. this.chain = chain
  75. this.method = method
  76. }
  77. request(...args) {
  78. const params = this.arg ? this.arg(...args) : undefined
  79. const promise = instance[this.method](this.url, params).catch(e => console.error(e))
  80. return this.chain ? this.chain(promise) : promise
  81. }
  82. }
  83. export class PostApi extends Api {
  84. constructor(url, arg, chain) {
  85. if (!arg) arg = data => data
  86. super(url, arg, chain, 'post')
  87. }
  88. }
  89. export class GetApi extends Api {
  90. constructor(url, arg, chain) {
  91. console.log(url)
  92. super(url, arg, chain, 'get')
  93. }
  94. }
  95. export default instance