request-file.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { getRequestPath, notifyErrorMessage } from '@cip/request/util'
  2. import axios from 'axios'
  3. import CipMessage from '@cip/components/cip-message'
  4. const CancelToken = axios.CancelToken
  5. export class RequestFile {
  6. cancelMessage = ''
  7. constructor ({ method, apiName, url, pathParams, params, config, data, headers }) {
  8. this.method = method.toLocaleLowerCase()
  9. this.path = getRequestPath(apiName, url, pathParams)
  10. this.data = data
  11. this.params = params
  12. config.timeout = 0
  13. this.cancel = () => {} // noop
  14. this.abort = this.abort.bind(this) // 显示的绑定
  15. const _this = this
  16. config.cancelToken = new CancelToken(function (c) {
  17. _this.cancel = c
  18. })
  19. config.headers = Object.assign({}, config.headers, headers)
  20. this.config = config
  21. }
  22. request () {
  23. const { method, params, data, path, config } = this
  24. console.log(method, params, data, path, config)
  25. return axios({ ...config, url: path, method, params, data })
  26. }
  27. send () {
  28. throw new Error('overwrite [send] method')
  29. }
  30. notifyError (err) {
  31. notifyErrorMessage(err).then(message => {
  32. CipMessage.error(message)
  33. })
  34. }
  35. abort () {
  36. this.cancel(this.cancelMessage)
  37. }
  38. }