upload-file.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { RequestFile } from './request-file'
  2. import apiConfig from './apiConfig'
  3. import { isSuccess } from '@cip/request/util'
  4. export class UploadFile extends RequestFile {
  5. // formData 参数即将废弃 使用data代替
  6. constructor ({ type, method = 'post', apiName, url, formData, data, params = {}, config = {}, pathParams = {}, headers = {}, resolveCb }) {
  7. data = data ?? formData
  8. method = type || method
  9. super({ method, apiName, url, data, pathParams, params, config, headers })
  10. this.cancelMessage = '已取消上传'
  11. this.resolveCb = resolveCb ?? ((_data) => _data)
  12. this.servicePath = apiName ? apiConfig[apiName] : '' // 用于resolveCb的入参
  13. }
  14. async send () {
  15. try {
  16. const res = await this.request()
  17. if (isSuccess(res, this.config)) {
  18. const { servicePath } = this
  19. console.log(res, this.resolveCb(res.data?.data, servicePath))
  20. const data = this.resolveCb(res.data?.data, servicePath)
  21. return { ...res.data, data }
  22. } else {
  23. // 抛给cache来处理错误
  24. throw res.data
  25. }
  26. } catch (err) {
  27. this.notifyError(err)
  28. throw err
  29. }
  30. }
  31. }
  32. // export class UploadFile {
  33. // constructor ({ type = 'post', apiName, url, formData, params = {}, config = {}, pathParams = {}, resolveCb }) {
  34. // this.type = type.toLocaleLowerCase()
  35. // this.path = getRequestPath(apiName, url, pathParams)
  36. // config.timeout = 0
  37. // config.params = params
  38. // this.cancel = () => {} // noop
  39. // this.abort = this.abort.bind(this) // 显示的绑定
  40. // const _this = this
  41. // config.cancelToken = new CancelToken(function (c) {
  42. // _this.cancel = c
  43. // })
  44. // // upload 特有的处理
  45. // this.resolveCb = resolveCb ?? ((data) => data)
  46. // this.servicePath = apiName ? apiConfig[apiName] : '' // 用于resolveCb的入参
  47. // this.formData = formData
  48. //
  49. // this.config = config
  50. // }
  51. //
  52. // send () {
  53. // return new Promise((resolve, reject) => {
  54. // axios[this.type](this.path, this.formData, this.config).then(res => {
  55. // if (isSuccess(res, this.config, DEFAULT_SUCCESS_CODE)) {
  56. // resolve({ ...res.data, data: this.resolveCb(res.data?.data, this.servicePath) })
  57. // } else {
  58. // notifyErrorMessage(res).then(message => {
  59. // CipMessage.error(message)
  60. // })
  61. // reject(res.data)
  62. // }
  63. // }).catch(err => {
  64. // notifyErrorMessage(err).then(message => {
  65. // CipMessage.error(message)
  66. // })
  67. // reject(err)
  68. // })
  69. // })
  70. // }
  71. //
  72. // abort () {
  73. // this.cancel('已取消上传')
  74. // }
  75. // }