yyb
20 小时以前 e5e79769db31b3f64eb7df5eec9543a5241b31f9
src/utils/util.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,121 @@
//防抖
import dayjs from "dayjs";
export  function debounce(fn) {
    console.log(1)
    let t = null //只会执行一次
    debugger
    return function (){
        if(t){
            clearTimeout(t)
        }
        t = setTimeout(()=>{
            console.log(temp);  //可以获取
            // console.log(arguments[0]) //undefined
            fn.apply(this,arguments)
            //在这个回调函数里面的argument是这个回调函数的参数,因为没有参数所以undefined,可以通过外面的函数赋值来进行访问
            //也可以改变成箭头函数,箭头函数的this是指向定义函数的那一层的,所以访问到的arguments是上一层函数的arguments
        },1000)
    }
}
//节流
export function throttle(fn, delay = 200) {
    let timer = null
    console.log(fn);
    debugger
    return function () {
        if(timer) return
        timer = setTimeout(() => {
            debugger
          fn.apply(this,arguments)
          timer = null
        })
    }
 }
//下拉动画
 export function animation(obj, target, fn1) {
    // console.log(fn1);
    // fn是一个回调函数,在定时器结束的时候添加
    // æ¯æ¬¡å¼€å®šæ—¶å™¨ä¹‹å‰å…ˆæ¸…除掉定时器
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
      // æ­¥é•¿è®¡ç®—公式  è¶Šæ¥è¶Šå°
      // æ­¥é•¿å–æ•´
      var step = (target - obj.scrollTop) / 10;
      step = step > 0 ? Math.ceil(step) : Math.floor(step);
      if (obj.scrollTop >= target) {
        clearInterval(obj.timer);
        // å¦‚æžœfn1存在,调用fn
        if (fn1) {
          fn1();
        }
      } else {
        // æ¯30毫秒就将新的值给obj.left
        obj.scrollTop = obj.scrollTop + step;
      }
    }, 10);
  }
  //判断文件类型
  export function judgeFileType(file) {
    if (file == null||file == ""){
         alert("请选择要上传的图片!");
         return false;
    }
    if (file.lastIndexOf('.')==-1){    //如果不存在"."
        alert("路径不正确!");
        return false;
    }
    var AllImgExt=".jpg|.jpeg|.gif|.bmp|.png|";
    var extName = file.substring(file.lastIndexOf(".")).toLowerCase();//(把路径中的所有字母全部转换为小写)
    if(AllImgExt.indexOf(extName+"|")==-1)
    {
        ErrMsg="该文件类型不允许上传。请上传 "+AllImgExt+" ç±»åž‹çš„æ–‡ä»¶ï¼Œå½“前文件类型为"+extName;
        alert(ErrMsg);
        return false;
    }
  }
  //文件类型
  export function fileType() {
    return {
      'application/msword': 'word',
      'application/pdf': 'pdf',
      'application/vnd.ms-powerpoint': 'ppt',
      'application/vnd.ms-excel': 'excel',
      'aplication/zip': 'zpi',
    }
  }
 export const deepCopySameProperties = (source, target) =>{
    for (const key in source) {
        if (target.hasOwnProperty(key)) {
            if (typeof source[key] === 'object' && source[key] !== null &&
                typeof target[key] === 'object' && target[key] !== null) {
                // é€’归处理对象
                deepCopySameProperties(source[key], target[key]);
            } else {
                // åŸºæœ¬ç±»åž‹ç›´æŽ¥èµ‹å€¼
                target[key] = source[key];
            }
        }
    }
    return target;
}
  export function filterArr(arr) {
      return arr.filter(item => item.flag !== false);
  }
 export function getCurrentMonth () {
    let month = dayjs().month() + 1
    if (month <= 3) {
        return '1';
    } else if (month <= 6) {
        return '2';
    } else if (month <= 9) {
        return '3';
    } else if (month <= 12) {
        return '4';
    }
}