1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import { RequestFile } from './request-file'
- import apiConfig from './apiConfig'
- import { isSuccess } from '@cip/request/util'
- export class UploadFile extends RequestFile {
- // formData 参数即将废弃 使用data代替
- constructor ({ type, method = 'post', apiName, url, formData, data, params = {}, config = {}, pathParams = {}, headers = {}, resolveCb }) {
- data = data ?? formData
- method = type || method
- super({ method, apiName, url, data, pathParams, params, config, headers })
- this.cancelMessage = '已取消上传'
- this.resolveCb = resolveCb ?? ((_data) => _data)
- this.servicePath = apiName ? apiConfig[apiName] : '' // 用于resolveCb的入参
- }
- async send () {
- try {
- const res = await this.request()
- if (isSuccess(res, this.config)) {
- const { servicePath } = this
- console.log(res, this.resolveCb(res.data?.data, servicePath))
- const data = this.resolveCb(res.data?.data, servicePath)
- return { ...res.data, data }
- } else {
- // 抛给cache来处理错误
- throw res.data
- }
- } catch (err) {
- this.notifyError(err)
- throw err
- }
- }
- }
- // export class UploadFile {
- // constructor ({ type = 'post', apiName, url, formData, params = {}, config = {}, pathParams = {}, resolveCb }) {
- // this.type = type.toLocaleLowerCase()
- // this.path = getRequestPath(apiName, url, pathParams)
- // config.timeout = 0
- // config.params = params
- // this.cancel = () => {} // noop
- // this.abort = this.abort.bind(this) // 显示的绑定
- // const _this = this
- // config.cancelToken = new CancelToken(function (c) {
- // _this.cancel = c
- // })
- // // upload 特有的处理
- // this.resolveCb = resolveCb ?? ((data) => data)
- // this.servicePath = apiName ? apiConfig[apiName] : '' // 用于resolveCb的入参
- // this.formData = formData
- //
- // this.config = config
- // }
- //
- // send () {
- // return new Promise((resolve, reject) => {
- // axios[this.type](this.path, this.formData, this.config).then(res => {
- // if (isSuccess(res, this.config, DEFAULT_SUCCESS_CODE)) {
- // resolve({ ...res.data, data: this.resolveCb(res.data?.data, this.servicePath) })
- // } else {
- // notifyErrorMessage(res).then(message => {
- // CipMessage.error(message)
- // })
- // reject(res.data)
- // }
- // }).catch(err => {
- // notifyErrorMessage(err).then(message => {
- // CipMessage.error(message)
- // })
- // reject(err)
- // })
- // })
- // }
- //
- // abort () {
- // this.cancel('已取消上传')
- // }
- // }
|