axios取消接口操作

// 发送axios接口
 let fileList = this.restfileList
 fileList.forEach((item, index) => {
      setTimeout(() => {
          let notifyKey = 'q' + nanoid() //生成唯一的id
          window.sessionStorage.setItem("cancelAxiosId", notifyKey)// 存浏览器session里面,请求拦截器用
          this.notifyInit[notifyKey] = this.$notify({
              iconClass: "el-icon-loading",
              title: '提示',
              duration: 0,
              offset: 65,
              dangerouslyUseHTMLString: true,
              message: item.name + '文件上传中...',
              onClose: () => {// 取消操作,取消axios接口请求
                  let cancelArr = window.axiosCancel;
                  cancelArr = cancelArr || [];
                  cancelArr.forEach((ele, index) => {
                      if (ele.id == notifyKey) {
                          this.$message({
                              message: '取消上传!',
                              type: 'warning'
                          });
                          ele.cancel("取消请求")  // 在失败函数中返回这里自定义的错误信息
                          delete window.axiosCancel[index]
                      }
                  })
              }
          });
          let formData = new FormData()
          formData.append("id", id);
          formData.append('file', item.raw)
          this.$api.systemApi.uploadFile(formData).then((res) => {
              if (res.success) {
              		// 接口成功以后,删除axiosCancel
                  let cancelArr = window.axiosCancel;
                  cancelArr = cancelArr || [];
                  cancelArr.forEach((ele, index) => {
                      if (ele.id == notifyKey) {
                          delete window.axiosCancel[index]
                      }
                  })
                  this.$notify({
                      title: '成功',
                      message: '文件上传成功!',
                      type: 'success'
                  });
                      this.notifyInit[notifyKey].close()
              } else {
              	  //  接口失败删除 window.axiosCancel
                  let cancelArr = window.axiosCancel;
                  cancelArr = cancelArr || [];
                  cancelArr.forEach((ele, index) => {
                      if (ele.id == notifyKey) {
                          delete window.axiosCancel[index]
                      }
                  })
                  this.$notify.error({
                      title: '错误',
                      message: item.name + "文件上传失败!"
                  });
                  setTimeout(() => {
                      this.notifyInit[notifyKey].close()
                  }, 100)
              }
          });
      }, 500)
  })  
// axios 请求拦截器
window.axiosCancel = []  // 全局定义一个存放取消请求的标识
// 请求拦截
axios.interceptors.request.use(config => {
	// 在发送请求之前做些什么 验证token之类的
	let isLogin = config.url.substring(config.url.length - 5, config.url.length);
	if (isLogin != 'login'&& config.url.indexOf("https://geo.datav.aliyun.com/") == -1) {
		let token = store.state.user.token;
		if(token) {
			config.headers['x-auth-token'] = `${token}`;
		}
	}
	if(config.url.indexOf("/minio/upload")!=-1||config.url.indexOf("/api/minio/move")!=-1){
		let cancelAxiosId = window.sessionStorage.getItem("cancelAxiosId")
		config.cancelToken = new axios.CancelToken(cancel => {
			window.axiosCancel.push({
				id:cancelAxiosId,
				cancel:cancel
			})
		})
	}
	NProgress.start();
	return config;
}, error => {
	// 对请求错误做些什么
	return Promise.reject(error);
})


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值