美文网首页
vue开发 通用工具包 轮子

vue开发 通用工具包 轮子

作者: 南土酱 | 来源:发表于2023-09-19 17:01 被阅读0次

    import { parseTime } from "./ruoyi";
    import { useWebNotification } from "@vueuse/core";

    /**

    • @param {number} time
    • @param {string} option
    • @returns {string}
      */
      export function formatTime(time, option) {
      if (("" + time).length === 10) {
      time = parseInt(time) * 1000;
      } else {
      time = +time;
      }
      const d = new Date(time);
      const now = Date.now();

    const diff = (now - d) / 1000;

    if (diff < 30) {
    return "刚刚";
    } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + "分钟前";
    } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + "小时前";
    } else if (diff < 3600 * 24 * 2) {
    return "1天前";
    }
    if (option) {
    return parseTime(time, option);
    } else {
    return (
    d.getMonth() +
    1 +
    "月" +
    d.getDate() +
    "日" +
    d.getHours() +
    "时" +
    d.getMinutes() +
    "分"
    );
    }
    }
    // 时间戳:1637244864707
    /* 时间戳转换为时间 /
    export function timestampToTime(timestamp) {
    timestamp = timestamp ? timestamp : null;
    let date = new Date(timestamp); //时间戳为10位需
    1000,时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + "-";
    let M =
    (date.getMonth() + 1 < 10
    ? "0" + (date.getMonth() + 1)
    : date.getMonth() + 1) + "-";
    let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    let h =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    let m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
    ":";
    let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
    }

    /**

    • @param {Object} json
    • @returns {Array}
      */
      export function param(json) {
      if (!json) return "";
      return cleanArray(
      Object.keys(json).map((key) => {
      if (json[key] === undefined) return "";
      return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
      })
      ).join("&");
      }

    /**

    • @param {string} url
    • @returns {Object}
      */
      export function param2Obj(url) {
      const search = decodeURIComponent(url.split("?")[1]).replace(/+/g, " ");
      if (!search) {
      return {};
      }
      const obj = {};
      const searchArr = search.split("&");
      searchArr.forEach((v) => {
      const index = v.indexOf("=");
      if (index !== -1) {
      const name = v.substring(0, index);
      const val = v.substring(index + 1, v.length);
      obj[name] = val;
      }
      });
      return obj;
      }

    /**

    • Merges two objects, giving the last one precedence
    • @param {Object} target
    • @param {(Object|Array)} source
    • @returns {Object}
      */
      export function objectMerge(target, source) {
      if (typeof target !== "object") {
      target = {};
      }
      if (Array.isArray(source)) {
      return source.slice();
      }
      Object.keys(source).forEach((property) => {
      const sourceProperty = source[property];
      if (typeof sourceProperty === "object") {
      target[property] = objectMerge(target[property], sourceProperty);
      } else {
      target[property] = sourceProperty;
      }
      });
      return target;
      }

    /**

    • @param {Function} func

    • @param {number} wait

    • @param {boolean} immediate

    • @return {*}
      */
      export function debounce(func, wait) {
      var timeout;
      return function () {
      var context = this;
      var args = arguments;

      clearTimeout(timeout);
      timeout = setTimeout(function () {
      func.apply(context, args);
      }, wait);
      };
      }

    /**

    • This is just a simple version of deep copy
    • Has a lot of edge cases bug
    • If you want to use a perfect deep copy, use lodash's _.cloneDeep
    • @param {Object} source
    • @returns {Object}
      */
      export function deepClone(source) {
      if (!source && typeof source !== "object") {
      throw new Error("error arguments", "deepClone");
      }
      const targetObj = source.constructor === Array ? [] : {};
      Object.keys(source).forEach((keys) => {
      if (source[keys] && typeof source[keys] === "object") {
      targetObj[keys] = deepClone(source[keys]);
      } else {
      targetObj[keys] = source[keys];
      }
      });
      return targetObj;
      }

    // 首字母大小
    export function titleCase(str) {
    return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
    }

    // 下划转驼峰
    export function camelCase(str) {
    return str.replace(/_[a-z]/g, (str1) => str1.substr(-1).toUpperCase());
    }

    // 是否数字
    export function isNumberStr(str) {
    return /^[+-]?(0|([1-9]\d*))(.\d+)?$/g.test(str);
    }

    /**

    • 变浅颜色值
    • @param color 颜色值字符串
    • @param level 加深的程度,限0-1之间
    • @returns 返回处理后的颜色值
      */
      export function getLightColor(color, level) {
      let reg = /^#?[0-9A-Fa-f]{6}$/;
      if (!reg.test(color)) return color;
      let rgb = hexToRgb(color);
      for (let i = 0; i < 3; i++)
      rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]);
      return rgbToHex(rgb[0], rgb[1], rgb[2]);
      }

    /**

    • hex颜色转rgb颜色
    • @param str 颜色值字符串
    • @returns 返回处理后的颜色值
      */
      export function hexToRgb(str) {
      let hexs = "";
      let reg = /^#?[0-9A-Fa-f]{6}$/;
      if (!reg.test(str)) return str;
      str = str.replace("#", "");
      hexs = str.match(/../g);
      for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16);
      return hexs;
      }

    /**

    • rgb颜色转Hex颜色
    • @param r 代表红色
    • @param g 代表绿色
    • @param b 代表蓝色
    • @returns 返回处理后的颜色值
      */
      export function rgbToHex(r, g, b) {
      let reg = /^\d{1,3}/; if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ""; let hexs = [r.toString(16), g.toString(16), b.toString(16)]; for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0{hexs[i]}; return#${hexs.join("")}`;
      }

    /**

    • 浏览器Web通知
    • @param { title: 'title' } optinos
      */
      export function webNotify(optinos) {
      const { show, isSupported } = useWebNotification({
      title: optinos.title,
      dir: "auto",
      lang: "en",
      renotify: true,
      tag: "tag",
      body: optinos.body,
      });
      if (isSupported) {
      show();
      }
      }

    export function testPsw(rule, value, callBack) {
    // /^(?![0-9]+)(?![a-zA-Z]+)[0-9A-Za-z]*/.test(value) if (true) { callBack(); } else { callBack(new Error("密码只能数字+英文组合,且长度不能超过20")); } } export function wifiPwdMatch(value){ if(/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^\da-zA-Z\s]).{8,16}/.test(value)){
    return true
    }
    return false
    }

    相关文章

      网友评论

          本文标题:vue开发 通用工具包 轮子

          本文链接:https://www.haomeiwen.com/subject/vlkjtdtx.html