123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { getRequestPath, notifyErrorMessage } from '@cip/request/util'
- import axios from 'axios'
- import CipMessage from '@cip/components/cip-message'
- const CancelToken = axios.CancelToken
- export class RequestFile {
- cancelMessage = ''
- constructor ({ method, apiName, url, pathParams, params, config, data, headers }) {
- this.method = method.toLocaleLowerCase()
- this.path = getRequestPath(apiName, url, pathParams)
- this.data = data
- this.params = params
- config.timeout = 0
- this.cancel = () => {} // noop
- this.abort = this.abort.bind(this) // 显示的绑定
- const _this = this
- config.cancelToken = new CancelToken(function (c) {
- _this.cancel = c
- })
- config.headers = Object.assign({}, config.headers, headers)
- this.config = config
- }
- request () {
- const { method, params, data, path, config } = this
- console.log(method, params, data, path, config)
- return axios({ ...config, url: path, method, params, data })
- }
- send () {
- throw new Error('overwrite [send] method')
- }
- notifyError (err) {
- notifyErrorMessage(err).then(message => {
- CipMessage.error(message)
- })
- }
- abort () {
- this.cancel(this.cancelMessage)
- }
- }
|