常见js工具积累1

downloadFile(url, fileName) {
      this.$axios({
        method: "get",
        url: "/ossfile/download/" + url,
        responseType: 'blob'
      }).then(function (response) {
        var blob = new Blob([response.data])
        var downloadElement = document.createElement('a');
        var href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        let newFileName = fileName;
        if (newFileName.indexOf("!") != -1) {
          let nameArr = newFileName.split("!");
          if (nameArr.length > 1) {
            newFileName = nameArr[0];
            let sufixs = nameArr[1].split(".");
            newFileName = newFileName + "." + sufixs[1];
          }
        }
        downloadElement.download = newFileName; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
      })
        .catch(function (error) {
          console.log(error);
        });
    },

2.字符串是否为空以及js金额大小写转换

   isNullOrEmpty(strVal) {
      if (strVal == '' || strVal == null || strVal == undefined || strVal.length == 0) {
        return true;
      }
      return false;
    },
    capitalAmount(amount) {
          // 汉字的数字
          const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
          // 基本单位
          const cnIntRadice = ["", "拾", "佰", "仟"];
          // 对应整数部分扩展单位
          const cnIntUnits = ["", "万", "亿", "兆"];
          // 对应小数部分单位
          const cnDecUnits = ["角", "分", "毫", "厘"];
          // 整数金额时后面跟的字符
          const cnInteger = "整";
          // 整型完以后的单位
          const cnIntLast = "元";
          // 最大处理的数字
          const maxNum = 9999999999999999.99;
          // 金额整数部分
          let integerNum;
          // 金额小数部分
          let decimalNum;
          // 输出的中文金额字符串
          let chineseStr = "";
          // 分离金额后用的数组,预定义
          let parts;
          let orginAmount=amount
          amount=Math.abs(amount)
          if (amount === "") { return ""; }
          amount = parseFloat(amount);
          if (amount >= maxNum) {
            // 超出最大处理数字
            return "";
          }
          if (amount === 0) {
            chineseStr = cnNums[0] + cnIntLast + cnInteger;
            return chineseStr;
          }
          // 转换为字符串
          amount = amount.toString();
          if (amount.indexOf(".") === -1) {
            integerNum = amount;
            decimalNum = "";
          } else {
            parts = amount.split(".");
            integerNum = parts[0];
            decimalNum = parts[1].substr(0, 4);
          }
          // 获取整型部分转换
          if (parseInt(integerNum, 10) > 0) {
            let zeroCount = 0;
            const IntLen = integerNum.length;
            for (let i = 0; i < IntLen; i++) {
              const n = integerNum.substr(i, 1);
              const p = IntLen - i - 1;
              const q = p / 4;
              const m = p % 4;
              if (n === "0") {
                zeroCount++;
              } else {
                if (zeroCount > 0) {
                  chineseStr += cnNums[0];
                }
                // 归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m];
              }
              if (m === 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
              }
            }
            chineseStr += cnIntLast;
          }
          // 小数部分
          if (decimalNum !== "") {
            const decLen = decimalNum.length;
            for (let i = 0; i < decLen; i++) {
              const n = decimalNum.substr(i, 1);
              if (n !== "0") {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
              }
            }
          }
          if (chineseStr === "") {
            chineseStr += cnNums[0] + cnIntLast + cnInteger;
          } else if (decimalNum === "") {
            chineseStr += cnInteger;
          }
          if(orginAmount<0) chineseStr = "负" + chineseStr;
          return chineseStr;
        }
原文地址:https://www.cnblogs.com/xmyfsj/p/14951628.html