Download files, download forms and other login authentication over time

Read on the line

/**
     * 
     * @param {*} type 请求的类型
     * @param {*} url 请求的接口地址
     * @param {*} params 请求的参数
     * @param {*} fileName 下载的文件名
     */
exportreport() {
      let srcipng = null;
      if (localStorage.getItem("locale")) {
        srcipng = localStorage.getItem("locale");
      } else {
        srcipng = localStorage.getItem("defaultLng");
      }
      this.url ="/finance/account/supplyStatementInfo/export";
        let params = {
          pageNum:1,
          pageSize:2000,
          language:srcipng,
        }
      if (this.startTimes) {
        params.beginDate = this.startTimes[0];
        params.endDate = this.startTimes[1];
      }
      //支付类型
      if (this.bankname) {
        params.tradeType =this.bankname;
      }
      //收支类型
      if (this.bankname4) {
        params.incomeType =this.bankname4;
      }
      if (this.bankname1) {
        //交易渠道状态
        params.payType =this.bankname1;
      }
      if (this.bankname2) {
        //商户类型
        params.userType =this.bankname2;
      }
      if (this.bankname3) {
        //交易状态
        if (this.bankname3 == "已付款") {
          this.bankname3 = "提现成功";
          params.examineStatus =this.bankname3;
        }else{
          params.examineStatus =this.bankname3;
        }
        // var a = this.url.concat("&examineStatus=" + this.bankname3);
        // this.url = a;
      }
      // 搜索框参数
      if (this.searchVal) {
        params.unionKeyWord =this.searchVal;

        // //.log(this.url)
      }
      let yyaipa = this.$t("nowan1.No1.statistics1");
      let daver = this.url;
      this.$utils.commonUtils.export('get',daver,params,yyaipa);
    },

The following is a method of packaging toke uploaded to log in to download files or to verify when exported, when the operation will jump toke expired only login page, based on the request after axios package

 export(type, url, params, fileName) {
      if(type == 'get'){
        return axios.get(`${base.baseUrl}${url}`,{params,responseType: 'blob'}).then((res)=>{
          this._handleDownload(res,fileName)
        })
      }else if(type == 'post'){
        return axios.post(`${base.baseUrl}${url}`, params).then((res)=>{
          this._handleDownload(res,fileName)
        })
      }
  },
  _handleDownload(res,fileName){
          const content = res
          const blob = new Blob([content])
          if ('download' in document.createElement('a')) { // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName + '.xls'
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else { // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
  },

Guess you like

Origin blog.csdn.net/weixin_43869524/article/details/91492132