vue 项目相关开发工具utils

阿拉伯数字转汉字NumberToChinese(num) { var chnNumChar = [ "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" ]; var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"]; var chnUnitChar = ["", "十".
摘要由CSDN通过智能技术生成

阿拉伯数字转汉字

//方案一
NumberToChinese(num) {
   
    var chnNumChar = [
      "零",
      "一",
      "二",
      "三",
      "四",
      "五",
      "六",
      "七",
      "八",
      "九"
    ];
    var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
    var chnUnitChar = ["", "十", "百", "千"];
    var unitPos = 0;
    var strIns = "",
      chnStr = "";
    var needZero = false;

    if (num === 0) {
   
      return chnNumChar[0];
    }

    while (num > 0) {
   
      var section = num % 10000;
      if (needZero) {
   
        chnStr = chnNumChar[0] + chnStr;
      }
      strIns = this.SectionToChinese(section);
      strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0];
      chnStr = strIns + chnStr;
      needZero = section < 1000 && section > 0;
      num = Math.floor(num / 10000);
      unitPos++;
    }
    return chnStr;
  },
  lazyLoadScript(url) {
   
    return new Promise((resolve, reject) => {
   
      let script = document.createElement("script");
      script.src = url;
      script.type = "text/javascript";
      let head = document.getElementsByTagName("head")[0];
      head.appendChild(script);
      script.onload = () => {
   
        resolve();
      };
      script.onerror = err => {
   
        reject(err);
      };
    });
  },
  //方案二
  DX(n) {
   
    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法";
    if (n === 0) {
   
      return "零";
    }
    let unit = "万千百拾亿千百拾万千百拾元角分";
    let str = "";
    n += "00";
    let p = n.indexOf(".");
    if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
    unit = unit.substr(unit.length - n.length);
    for (var i = 0; i < n.length; i++) {
   
      str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);
    }
    return str
      .replace(/零(千|百|拾|角)/g, "零")
      .replace(/(零)+/g, "零")
      .replace(/零(万|亿|元)/g, "$1")
      .replace(/(亿)万|壹(拾)/g, "$1$2")
      .replace(/^元零?|零分/g, "")
      .replace(/元$/g, "元整");
  },

其他常用

  setSS: (key, value) => {
   
    sessionStorage.setItem(key, JSON.stringify(value));
  },
  getSS: key => {
   
    let value = sessionStorage.getItem(key);
    try {
   
      return JSON.parse(value);
    } catch (err) {
   
      return value;
    }
  },
  delSS: key => {
   
    sessionStorage.removeItem(key);
  },
  setLS: (key, value) => {
   
    localStorage.setItem(key, JSON.stringify(value));
  },
  getLS: key => {
   
    let value = localStorage.getItem(key);
    try {
   
      return JSON.parse(value);
    } catch (err) {
   
      return value;
    }
  },
  delLS: key => {
   
    localStorage.removeItem(key);
  },
  setCookie: (name, value) => {
   
    const Days = 0.5;
    const exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie =
      name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  },
  getCookie: name => {
   
    const reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    const arr = document.cookie.match(reg);
    if (arr) {
   
      return unescape(arr[2]);
    } else {
   
      return null;
    }
  },
  delCookie: name => {
   
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    const cval = utils.getCookie(name);
    if (cval != null) {
   
      document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
  },
  downLoad: downLoadUrl => {
   
    let a = document.createElement("a");
    if (a.download) {
   
      a.href = downLoadUrl;
      if (document.createEvent) {
   
        var evObj = document.createEvent("MouseEvents");
        evObj.initEvent(
          "click",
          true,
          true,
          window,
          1,
          12,
          345,
          7,
          220,
          false,
          false,
          true,
          false,
          0,
          null
        );
        a.dispatchEvent(evObj);
      } else if (document.createEventObject) {
   
        a.fireEvent("onmousemove");
      }
    } else {
   
      window.open(downLoadUrl, "_blank");
    }
  },
  postDownLoad: (url, options, fileName) => {
   
    Message({
   
      message: "开始下载",
      type: "success"
    });
    axiosHttp()({
   
      method: options.method || "post",
      url: url,
      responseType: "blob",
      timeout: options.timeout || 10000,
      params: options.params || {
   },
      data: options.data || {
   }
    })
      .then(res => {
   
        const blob = new Blob([res.data]);
        // 兼容不同浏览器的URL对象
        const url = window.URL || window.webkitURL || window.moxURL;

        // 获取后台提供的文件名
        let backendFileName = res.headers["content-disposition"];
        if (/utf-8''/i.test(backendFileName)) {
   
          backendFileName = backendFileName.split("-8''")[1];
        } else if (backendFileName && backendFileName.length >= 2) {
   
          backendFileName = backendFileName.split("=")[1];
        }
        backendFileName = decodeURIComponent(backendFileName);

        //  兼容IE
        if ("msSaveOrOpenBlob" in navigator) {
   
          window.navigator.msSaveOrOpenBlob(blob, fileName || backendFileName);
          return;
        }
        // 创建下载链接
        const downloadHref = url.createObjectURL(blob);
        // 创建a标签并为其添加属性
        let downloadLink = document.createElement("a");
        downloadLink.href = downloadHref;
        downloadLink.download = fileName || backendFileName || "未知文件.txt";
        // 触发点击事件执行下载
        downloadLink.click();
      })
      .catch(_ => {
   
        Message({
   
          message: "下载失败",
          type: "error"
        });
      });
  },
  imgView: downLoadUrl => {
   
    window.open(<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值