跳至主要內容

常用方法

周浚豪...大约 1 分钟常用方法

常用方法

uniapp base64

//base64加密
export const encrypt = (str) => {
  return (str = Buffer.from(str).toString("base64"));
};
//base64解密
export const decode = async (str) => {
  return (str = await Buffer.from(str, "base64").toString());
};

获取导航栏等信息

export const getEquipmentInformation = () => {
  let statusBar = 0; //状态栏高度
  let customBar = 0; // 状态栏高度 + 导航栏高度
  let navbar = 0; // 自定义标题与胶囊对齐高度
  uni.getSystemInfo({
    success: function (e) {
      // #ifdef MP
      statusBar = e.statusBarHeight;
      customBar = e.statusBarHeight + 45;
      if (e.platform === "android") {
        this.$store.commit("SET_SYSTEM_IOSANDROID", false);
        customBar = e.statusBarHeight + 50;
      }
      // #endif
      // #ifdef MP-WEIXIN
      statusBar = e.statusBarHeight;
      const custom = wx.getMenuButtonBoundingClientRect();
      customBar = custom.bottom + custom.top - e.statusBarHeight;
      navbar = (custom.top - e.statusBarHeight) * 2 + custom.height;
      // #endif
      // #ifdef MP-ALIPAY
      statusBar = e.statusBarHeight;
      customBar = e.statusBarHeight + e.titleBarHeight;
      // #endif
      // #ifdef APP-PLUS
      console.log("app-plus", e);
      statusBar = e.statusBarHeight;
      customBar = e.statusBarHeight + 45;
      // #endif
      // #ifdef H5
      statusBar = 0;
      customBar = e.statusBarHeight + 45;
      // #endif
    },
  });
  return {
    statusBar,
    customBar,
    navbar,
  };
};

数字转成汉字

/**
 * @params num === 要转换的数字
 * @return 汉字
 * */
export const toChinesNum = (num) => {
  let changeNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp) => {
    let strArr = temp.toString().split("").reverse();
    let newNum = "";
    let newArr = [];
    strArr.forEach((item, index) => {
      newArr.unshift(
        item === "0" ? changeNum[item] : changeNum[item] + unit[index]
      );
    });
    let numArr = [];
    newArr.forEach((m, n) => {
      if (m !== "零") numArr.push(n);
    });
    if (newArr.length > 1) {
      newArr.forEach((m, n) => {
        if (newArr[newArr.length - 1] === "零") {
          if (n <= numArr[numArr.length - 1]) {
            newNum += m;
          }
        } else {
          newNum += m;
        }
      });
    } else {
      newNum = newArr[0];
    }

    return newNum;
  };
  let overWan = Math.floor(num / 10000);
  let noWan = num % 10000;
  if (noWan.toString().length < 4) {
    noWan = "0" + noWan;
  }
  return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
};

获取年月日

export const transitionTime = (timestamp) => {
  let date = new Date(parseInt(timestamp));
  let Year = date.getFullYear();
  let Moth = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1): date.getMonth() + 1;
  let Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  let Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let Minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let Sechond = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  let GMT = Year + "-" + Moth + "-" + Day + "   " + Hour + ":" + Minute + ":" + Sechond;
  return GMT; // 2022-09-07 15:56:07
};

字符串转时间戳

/**
   * 字符串转时间戳
   * @param {Object} expiresTime
   */
export const strTotime(expiresTime){
    let expires_time = expiresTime.substring(0, 19);
    expires_time = expires_time.replace(/-/g, '/');
    return Math.round(new Date(expires_time).getTime() / 1000);
  }

获取当前时间戳

export const time() {
   return Math.round(new Date() / 1000);
  }
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8