vue项目常用方法封装

vue 项目中可以直接使用

1、常用工具类 utils.js 中

/**
 * 表单常用正则验证
 */
checkStr (str, type) { 
    switch (type) { 
        case 'phone': //手机号码 
            return /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(str); 
        case 'tel':     //座机 
            return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str); 
        case 'card':    //身份证 
            return /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(str); 
        case 'carNumber': //车牌 
            let xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
            let creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
            if (vehicleNumber.length == 7) {
                return creg.test(vehicleNumber);
            } else if (vehicleNumber.length == 8) {
                return xreg.test(vehicleNumber);
            } else {
                return false;
            }
        case 'pwd':     //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线 
            return /^[a-zA-Z]\w{5,17}$/.test(str) 
        case 'postal':  //邮政编码 
            return /[1-9]\d{5}(?!\d)/.test(str); 
        case 'QQ':      //QQ号 
            return /^[1-9][0-9]{4,9}$/.test(str); 
        case 'email':   //邮箱 
            return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str); 
        case 'money':   //金额(小数点2位) 
            return /^\d*(?:\.\d{0,2})?$/.test(str); 
        case 'URL':     //网址 
            return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str) 
        case 'IP':      //IP 
            return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str); 
        case 'date':    //日期时间 
            return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/.test(str) 
        case 'number':  //数字 
            return /^[0-9]$/.test(str); 
        case 'english': //英文 
            return /^[a-zA-Z]+$/.test(str); 
        case 'chinese': //中文 
            return /^[\u4E00-\u9FA5]+$/.test(str); 
        case 'lower':   //小写 
            return /^[a-z]+$/.test(str); 
        case 'upper':   //大写 
            return /^[A-Z]+$/.test(str); 
        default: 
            return true; 
    }
}

// 严格的身份证校验 
export function isCardID(sId) { 
    if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) { 
        alert('你输入的身份证长度或格式错误') 
        return false 
    } 
    //身份证城市 
    var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}; 
    if(!aCity[parseInt(sId.substr(0,2))]) {  
        alert('你的身份证地区非法') 
        return false 
    } 

    // 出生日期验证 
    var sBirthday=(sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2))).replace(/-/g,"/"), 
        d = new Date(sBirthday) 
    if(sBirthday != (d.getFullYear()+"/"+ (d.getMonth()+1) + "/" + d.getDate())) { 
        alert('身份证上的出生日期非法') 
        return false 
    } 

    // 身份证号码校验 
    var sum = 0, 
        weights =  [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], 
        codes = "10X98765432" 
    for (var i = 0; i < sId.length - 1; i++) { 
        sum += sId[i] * weights[i]; 
    } 
    var last = codes[sum % 11]; //计算出来的最后一位身份证号码 
    if (sId[sId.length-1] != last) {  
        alert('你输入的身份证号非法') 
        return false 
    } 

    return true 
} 

/**  
 * 去除空格  
 * @param  {str}  
 * @param  {type}   
 *       type:  1-所有空格  2-前后空格  3-前空格 4-后空格  
 * @return {String}  
 */ 

export default trim (str, type) {  
    type = type || 1  
    switch (type) {  
        case 1:  
            return str.replace(/\s+/g, "");  
        case 2:  
            return str.replace(/(^\s*)|(\s*$)/g, "");  
        case 3: 
             return str.replace(/(^\s*)/g, "");  
        case 4: 
             return str.replace(/(\s*$)/g, "");  
        default:  
            return str;  
    }  
}  

/*
 * 验证字符串是否为空(也不能为纯空格)
 * true--说明为空, false--说明不为空
 */
export function isEmptyString(string) {
  if (
    string == undefined ||
    typeof string == 'undefined' ||
    !string ||
    string == null ||
    string == '' ||
    /^\s+$/gi.test(string)
  ) {
    return true;
  } else {
    return false;
  }
}

/*
 * 生日转为年龄(精确到月份)
 */
export function birsdayToAge(birsday) {
  let aDate = new Date();
  let thisYear = aDate.getFullYear();
  let bDate = new Date(birsday);
  let brith = bDate.getFullYear();
  let age = thisYear - brith;
  if (aDate.getMonth() == bDate.getMonth()) {
    if (aDate.getDate() < bDate.getDate()) {
      age = age - 1;
    }
  } else {
    if (aDate.getMonth() < bDate.getMonth()) {
      age = age - 1;
    }
  }
  return age;
}

/**
 * 判断数据类型
 * @param {any} val - 基本类型数据或者引用类型数据
 * @return {string} - 可能返回的结果有,均为小写字符串
 * number、boolean、string、null、undefined、array、object、function等
 */
export function getType(val){
  //判断数据是 null 和 undefined 的情况
  if (val == null) {
    return val + "";
  }
  return typeof(val) === "object" ?
      Object.prototype.toString.call(val).slice(8, -1).toLowerCase() :
      typeof(val);
}


/*
 * 验证是否字符串
 */
export function isString(n) {
  return Object.prototype.toString.call(o).slice(8, -1) === 'String' 
}

/*
 * 验证是否为数字
 */
export function isNumber(n) {
  return Object.prototype.toString.call(o).slice(8, -1) === 'Number' 
}

/*
 * 是否为数组
 */
export function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}

/*
 * 是否空数组
 */
export function isArrayEmpty(val) {
  if (val && val instanceof Array && val.length > 0) {
    return false;
  } else {
    return true;
  }
}

/*
 * 获取url参数字符串
 * 没有返回null
 */
export function getQueryString(name) {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  let r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

/** 
 * @param  {arr} 数组排序
 * @param  {type} 1:从小到大   2:从大到小   3:随机 
 * @return {Array} 
 */ 
export function arrSort (arr, type = 1) { 
    return arr.sort( (a, b) => { 
        switch(type) { 
            case 1: 
                return a - b; 
            case 2: 
                return b - a; 
            case 3: 
                return Math.random() - 0.5; 
            default: 
                return arr; 
        } 
    }) 
}

/*
 * 原数组去重
 */
export function arrUnique(arr){   
    return arr.filter((item,index, arr) => {  
        return index === arr.indexOf(item);
    })    
} 

/*
 * 递归深拷贝
 */
export function deepCopy(obj) {
  let result = Array.isArray(obj) ? [] : {};
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === 'object' && obj[key] !== null) {
        result[key] = deepCopy(obj[key]);
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
}

/**
 * 去除参数空数据(用于向后台传递参数的时候)
 * @param {Object} obj [参数对象]
 */
export function filterEmptyData(obj) {
  for (let prop in obj) {
    obj[prop] === '' ? delete obj[prop] : obj[prop];
  }
  return obj;
}

/**
 * @desc  函数防抖,用于将多次执行变为最后一次执行
 * @param {function} func - 需要使用函数防抖的被执行的函数。必传
 * @param {Number} wait - 多少毫秒之内触发,只执行第一次,默认1000ms。可以不传
 */
export function debounce(fn, delay) {
  delay = delay || 1000; //默认1s后执行
  let timer = null;
  return function () {
    let context = this;
    let arg = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(context, arg);
    }, delay);
  };
}

/**
 * 节流函数, 用于将多次执行变为每隔一段时间执行
 * @param fn 事件触发的操作
 * @param delay 间隔多少毫秒需要触发一次事件
 */
export function throttle2(fn, delay) {
  let timer = null;
  return function () {
    let context = this;
    let args = arguments;
    if (!timer) {
      timer = setTimeout(function () {
        fn.apply(context, args);
        clearTimeout(timer);
      }, delay);
    }
  };
}

/**
 * 格式化时间
 */
export function dateFormater(formater, t){  
    let date = t ? new Date(t) : new Date(),  
        Y = date.getFullYear() + '',  
        M = date.getMonth() + 1,  
        D = date.getDate(),  
        H = date.getHours(),  
        m = date.getMinutes(),  
        s = date.getSeconds();  
    return formater.replace(/YYYY|yyyy/g,Y)  
        .replace(/YY|yy/g,Y.substr(2,2))  
        .replace(/MM/g,(M<10?'0':'') + M)  
        .replace(/DD/g,(D<10?'0':'') + D)  
        .replace(/HH|hh/g,(H<10?'0':'') + H)  
        .replace(/mm/g,(m<10?'0':'') + m)  
        .replace(/ss/g,(s<10?'0':'') + s)  
}  

//判断是否苹果手机设备
isIos () {  
    var u = navigator.userAgent;  
    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机  
        // return "Android";  
        return false  
    } else if (u.indexOf('iPhone') > -1) {//苹果手机  
        // return "iPhone";  
        return true  
    } else if (u.indexOf('iPad') > -1) {//iPad  
        // return "iPad";  
        return false  
    } else if (u.indexOf('Windows Phone') > -1) {//winphone手机  
        // return "Windows Phone";  
        return false  
    }else{  
        return false  
    }  
} 
 
//判断是否为PC端  
isPC () { 
    var userAgentInfo = navigator.userAgent;  
    var Agents = ["Android", "iPhone",  
                "SymbianOS", "Windows Phone",  
                "iPad", "iPod"];  
    var flag = true;  
    for (var v = 0; v < Agents.length; v++) {  
        if (userAgentInfo.indexOf(Agents[v]) > 0) {  
            flag = false;  
            break;  
        }  
    }  
    return flag;  
}  

//判断浏览器类型
browserType(){  
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器  
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器  
    var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器  
    var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器  
    var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器  
    var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器  
    if (isIE) {  
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");  
        reIE.test(userAgent);  
        var fIEVersion = parseFloat(RegExp["$1"]);  
        if(fIEVersion == 7) return "IE7"  
        else if(fIEVersion == 8) return "IE8";  
        else if(fIEVersion == 9) return "IE9";  
        else if(fIEVersion == 10) return "IE10";  
        else if(fIEVersion == 11) return "IE11";  
        else return "IE7以下"//IE版本过低  
    } 
 
    if (isFF) return "FF";  
    if (isOpera) return "Opera";  
    if (isEdge) return "Edge";  
    if (isSafari) return "Safari";  
    if (isChrome) return "Chrome";  
} 

2、number

/*
*随机数范围(取整)
*/
export function random (min, max) {  
    if (arguments.length === 2) { 
        return Math.floor(min + Math.random() * ( (max+1) - min ))  
    }else{  
        return null;  
    }  
}
/*将阿拉伯数字翻译成中文的大写数字*/  
export function numberToChinese (num) {  
    var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");  
    var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");  
    var a = ("" + num).replace(/(^0*)/g, "").split("."),  
        k = 0,  
        re = "";  
    for(var i = a[0].length - 1; i >= 0; i--) {  
        switch(k) {  
            case 0:  
                re = BB[7] + re;  
                break;  
            case 4:  
                if(!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")  
                    .test(a[0]))  
                    re = BB[4] + re;  
                break;  
            case 8:  
                re = BB[5] + re;  
                BB[7] = BB[5];  
                k = 0;  
                break;  
        } 
 
        if(k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)  
            re = AA[0] + re;  
        if(a[0].charAt(i) != 0)  
            re = AA[a[0].charAt(i)] + BB[k % 4] + re;  
        k++; 
     }  
 
    if(a.length > 1) // 加上小数部分(如果有小数部分)  
    {  
        re += BB[6];  
        for(var i = 0; i < a[1].length; i++)  
            re += AA[a[1].charAt(i)];  
    }  
    if(re == '一十')  
        re = "十";  
    if(re.match(/^一/) && re.length == 3)  
        re = re.replace("一", "");  
    return re;  
}  
 
/*将数字转换为大写金额*/  
export function changeToChinese (Num) {  
    //判断如果传递进来的不是字符的话转换为字符  
    if(typeof Num == "number") {  
        Num = new String(Num);  
    };  
    Num = Num.replace(/,/g, "") //替换tomoney()中的“,”  
    Num = Num.replace(/ /g, "") //替换tomoney()中的空格  
    Num = Num.replace(/¥/g, "") //替换掉可能出现的¥字符  
    if(isNaN(Num)) { //验证输入的字符是否为数字  
        //alert("请检查小写金额是否正确");  
        return "";  
    };  
    //字符处理完毕后开始转换,采用前后两部分分别转换  
    var part = String(Num).split(".");  
    var newchar = "";  
    //小数点前进行转化  
    for(var i = part[0].length - 1; i >= 0; i--) {  
        if(part[0].length > 10) {  
            return "";  
            //若数量超过拾亿单位,提示  
        }  
        var tmpnewchar = ""  
        var perchar = part[0].charAt(i);  
        switch(perchar) {  
            case "0":  
                tmpnewchar = "零" + tmpnewchar;  
                break;  
            case "1":  
                tmpnewchar = "壹" + tmpnewchar;  
                break;  
            case "2":  
                tmpnewchar = "贰" + tmpnewchar;  
                break;  
            case "3":  
                tmpnewchar = "叁" + tmpnewchar;  
                break;  
            case "4":  
                tmpnewchar = "肆" + tmpnewchar;  
                break;  
            case "5":  
                tmpnewchar = "伍" + tmpnewchar;  
                break;  
            case "6":  
                tmpnewchar = "陆" + tmpnewchar;  
                break;  
            case "7":  
                tmpnewchar = "柒" + tmpnewchar;  
                break;  
            case "8":  
                tmpnewchar = "捌" + tmpnewchar;  
                break;  
            case "9":  
                tmpnewchar = "玖" + tmpnewchar;  
                break;  
        }  
        switch(part[0].length - i - 1) {  
            case 0:  
                tmpnewchar = tmpnewchar + "元"; 
                    break;  
            case 1:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "拾";  
                break;  
            case 2:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "佰";  
                break;  
            case 3:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "仟";  
                break; 
                case 4:  
                tmpnewchar = tmpnewchar + "万";  
                break; 
                case 5:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "拾";  
                break; 
                case 6:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "佰";  
                break; 
                case 7:  
                if(perchar != 0) tmpnewchar = tmpnewchar + "仟";  
                break;  
            case 8:  
                tmpnewchar = tmpnewchar + "亿";  
                break;  
            case 9:  
                tmpnewchar = tmpnewchar + "拾";  
                break;  
        }  
        var newchar = tmpnewchar + newchar;  
    } 
        //小数点之后进行转化  
    if(Num.indexOf(".") != -1) {  
        if(part[1].length > 2) {  
            // alert("小数点之后只能保留两位,系统将自动截断");  
            part[1] = part[1].substr(0, 2)  
        }  
        for(i = 0; i < part[1].length; i++) {  
            tmpnewchar = ""  
            perchar = part[1].charAt(i)  
            switch(perchar) {  
                case "0":  
                    tmpnewchar = "零" + tmpnewchar;  
                    break;  
                case "1":  
                    tmpnewchar = "壹" + tmpnewchar;  
                    break;  
                case "2":  
                    tmpnewchar = "贰" + tmpnewchar;  
                    break;  
                case "3":  
                    tmpnewchar = "叁" + tmpnewchar; 
                    break;  
                case "4":  
                    tmpnewchar = "肆" + tmpnewchar; 
                    break;  
                case "5":  
                    tmpnewchar = "伍" + tmpnewchar;  
                    break;  
                case "6":  
                    tmpnewchar = "陆" + tmpnewchar;  
                    break;  
                case "7":  
                    tmpnewchar = "柒" + tmpnewchar;  
                    break;  
                case "8":  
                    tmpnewchar = "捌" + tmpnewchar;  
                    break;  
                case "9":  
                    tmpnewchar = "玖" + tmpnewchar;  
                    break;  
            }  
            if(i == 0) tmpnewchar = tmpnewchar + "角";  
            if(i == 1) tmpnewchar = tmpnewchar + "分";  
            newchar = newchar + tmpnewchar;  
        }  
    }  
    //替换所有无用汉字  
    while(newchar.search("零零") != -1)  
        newchar = newchar.replace("零零", "零");  
    newchar = newchar.replace("零亿", "亿");  
    newchar = newchar.replace("亿万", "亿");  
    newchar = newchar.replace("零万", "万");  
    newchar = newchar.replace("零元", "元");  
    newchar = newchar.replace("零角", "");  
    newchar = newchar.replace("零分", "");  
    if(newchar.charAt(newchar.length - 1) == "元") {  
        newchar = newchar + "整"  
    }  
    return newchar;  
}

3、其他

/*获取十六进制随机颜色*/  
export function getRandomColor () {  
    return '#' + (function(h) {  
        return new Array(7 - h.length).join("0") + h;  
    })((Math.random() * 0x1000000 << 0).toString(16)); 
}

/*获取网址参数*/  
export function getURL(name){ 
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");  
    var r = window.location.search.substr(1).match(reg);  
    if(r!=null) return  r[2]; return null; 
} 
 
/*获取全部url参数,并转换成json对象*/ 
export function getUrlAllParams (url) { 
    var url = url ? url : window.location.href;  
    var _pa = url.substring(url.indexOf('?') + 1),  
        _arrS = _pa.split('&'),  
        _rs = {}; 

    for (var i = 0, _len = _arrS.length; i < _len; i++) {  
        var pos = _arrS[i].indexOf('=');  
        if (pos == -1) {  
            continue;  
        } 
 
        var name = _arrS[i].substring(0, pos),  
            value = window.decodeURIComponent(_arrS[i].substring(pos + 1));  
        _rs[name] = value; 
 
    }  
    return _rs;  
}

/** 
 * ajax请求封装 
 * @param  {setting}  
 */  
export function ajax(setting){  
    //设置参数的初始值  
    var opts={  
        method: (setting.method || "GET").toUpperCase(), //请求方式  
        url: setting.url || "", // 请求地址  
        async: setting.async || true, // 是否异步  
        dataType: setting.dataType || "json", // 解析方式  
        data: setting.data || "", // 参数  
        success: setting.success || function(){}, // 请求成功回调  
        error: setting.error || function(){} // 请求失败回调  
    }   
    // 参数格式化 
    function params_format (obj) {  
        var str = ''  
        for (var i in obj) {  
            str += i + '=' + obj[i] + '&'  
        }  
        return str.split('').slice(0, -1).join('')  
    }   
    // 创建ajax对象  
    var xhr=new XMLHttpRequest();   
 
    // 连接服务器open(方法GET/POST,请求地址, 异步传输)  
    if(opts.method == 'GET'){  
        xhr.open(opts.method, opts.url + "?" + params_format(opts.data), opts.async);  
        xhr.send();  
    }else{  
        xhr.open(opts.method, opts.url, opts.async);  
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
        xhr.send(opts.data);  
    }       
 
    /* 
 
    ** 每当readyState改变时,就会触发onreadystatechange事件 
 
    ** readyState属性存储有XMLHttpRequest的状态信息 
 
    ** 0 :请求未初始化 
 
    ** 1 :服务器连接已建立 
 
    ** 2 :请求已接受 
 
    ** 3 : 请求处理中 
 
    ** 4 :请求已完成,且相应就绪 
 
    */ 
 
    xhr.onreadystatechange = function() {  
        if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {  
            switch(opts.dataType){ 
                 case "json":  
                    var json = JSON.parse(xhr.responseText);  
                    opts.success(json);  
                    break;  
                case "xml":  
                    opts.success(xhr.responseXML);  
                    break;  
                default: 
                    opts.success(xhr.responseText);  
                    break;  
            } 
        }  
    }  
 
    xhr.onerror = function(err) {  
        opts.error(err);  
    }  
}  

4、本地存储工具类 storageUntils.js

  • 结合 vuex 使用
class StorageFn {  
    constructor () {  
        this.ls = window.localStorage;  
        this.ss = window.sessionStorage;  
    }   
    /*-----------------cookie---------------------*/  
    /*设置cookie*/ 
    setCookie (name, value, day) {  
        var oDate = new Date();  
        oDate.setDate(oDate.getDate() + day);  
        document.cookie = name + '=' + value + ';expires=' + oDate;        
    }   
 
    /*获取cookie*/  
    getCookie (name) {  
        var arr = document.cookie.split('; ');  
        for (var i = 0; i < arr.length; i++) {  
            var arr2 = arr[i].split('='); 
            if (arr2[0] == name) {  
                return arr2[1];  
            }          }  
        return '';  
    } 
 
    /*删除cookie*/  
    removeCookie (name) {  
        this.setCookie(name, 1, -1);  
    }  
    /*-----------------localStorage---------------------*/  
    /*设置localStorage*/  
    setLocal(key, val) {  
        this.ls.setItem(key, JSON.stringify(val))          
    }   
 
    /*获取localStorage*/  
    getLocal(key) {  
        if (key) return JSON.parse(this.ls.getItem(key))  
        return null;  
    }  
    /*移除localStorage*/  
    removeLocal(key) {  
        this.ls.removeItem(key)  
    }  
    /*移除所有localStorage*/ 
 
    clearLocal() {  
        this.ls.clear()  
    }  
 
    /*-----------------sessionStorage---------------------*/ 
 
    /*设置sessionStorage*/  
    setSession(key, val) {   
        this.ss.setItem(key, JSON.stringify(val))  
    } 
 
    /*获取sessionStorage*/ 
    getSession(key) {  
        if (key) return JSON.parse(this.ss.getItem(key))  
        return null; 
    } 
  
    /*移除sessionStorage*/  
    removeSession(key) {  
        this.ss.removeItem(key)  
    } 
 
    /*移除所有sessionStorage*/ 
 
    clearSession() { 
 
        this.ss.clear()  
    }  
} 

//storageUtils.js
//所有key
const key_base="Cloud_ManagerSystem_"; //基础前缀
const key_userInfo=key_base+"userInfo"; //用户信息
const key_token=key_base+"token"; //token信息

const storageFn = new StorageFn(); //初始化

//用户信息
export function saveUserInfo(data){
    storageFn.setLocal(key_userInfo, data);
}
export function getUserInfo(){
    return storageFn.getLocal(key_userInfo);
}
//token
//....

css篇

  • pc端样式初始化
html { 
 
  line-height: 1.15; 
 
  /* 1 */ 
 
  -ms-text-size-adjust: 100%; 
 
  /* 2 */ 
 
  -webkit-text-size-adjust: 100%; 
 
  /* 2 */ 
 
}   
 
body { 
 
  margin: 0; 
 
}  
 
article, 
 
aside, 
 
footer, 
 
header, 
 
nav, 
 
section { 
 
  display: block; 
 
}   
 
h1 { 
 
  font-size: 2em; 
 
  margin: 0.67em 0; 
 
} 
 
figcaption, 
 
figure, 
 
main { 
 
  /* 1 */  
  display: block; 
 
}  
 
figure {  
  margin: 1em 40px; 
 
}  
 
hr { 
 
  box-sizing: content-box; 
 
  /* 1 */ 
 
  height: 0; 
 
  /* 1 */ 
 
  overflow: visible; 
 
  /* 2 */ 
 
}   
 
pre { 
 
  font-family: monospace, monospace; 
 
  /* 1 */ 
 
  font-size: 1em; 
 
  /* 2 */ 
 
}   
 
a { 
 
  background-color: transparent; 
 
  /* 1 */ 
 
  -webkit-text-decoration-skip: objects; 
 
  /* 2 */ 
 
}  
 
abbr[title] { 
 
  border-bottom: none; 
 
  /* 1 */ 
 
  text-decoration: underline; 
 
  /* 2 */ 
 
  text-decoration: underline dotted; 
 
  /* 2 */ 
 
}  
 
b, 
 
strong { 
 
  font-weight: inherit; 
 
}  
 
b, 
 
strong { 
 
  font-weight: bolder; 
 
} 
 
code, 
 
kbd, 
 
samp { 
 
  font-family: monospace, monospace; 
 
  /* 1 */ 
 
  font-size: 1em; 
 
  /* 2 */ 
 
} 
 
dfn { 
 
  font-style: italic; 
 
} 
 
mark {  
  background-color: #ff0;  
  color: #000;  
} 
 
small {  
  font-size: 80%;  
}  
 
sub, 
 
sup { 
 
  font-size: 75%;  
  line-height: 0;  
  position: relative;  
  vertical-align: baseline;  
}  
 
 
sub {  
  bottom: -0.25em;  
} 
 
sup {  
  top: -0.5em;  
} 
 
audio,  
video {  
  display: inline-block;  
} 
 
audio:not([controls]) {  
  display: none;  
  height: 0;  
}  
 
img {  
  border-style: none;  
}  
svg:not(:root) {  
  overflow: hidden;  
} 
 
button, 
 
input, 
 
optgroup, 
 
select, 
 
textarea { 
 
  font-family: sans-serif; 
 
  /* 1 */ 
 
  font-size: 100%; 
 
  /* 1 */ 
 
  line-height: 1.15; 
 
  /* 1 */ 
 
  margin: 0; 
 
  /* 2 */ 
 
} 
 
button,  
input { 
 
  /* 1 */ 
 
  overflow: visible; 
 
} 
 
button,  
select { 
 
  /* 1 */ 
 
  text-transform: none; 
 
} 
 
button, 
 
html [type="button"],   
 
/* 1 */  
 
[type="reset"], 
 
[type="submit"] { 
 
  -webkit-appearance: button; 
 
  /* 2 */ 
 
} 
 
button::-moz-focus-inner, 
 
[type="button"]::-moz-focus-inner, 
 
[type="reset"]::-moz-focus-inner, 
 
[type="submit"]::-moz-focus-inner {  
  border-style: none;  
  padding: 0; 
 
}  
 
 
button:-moz-focusring, 
 
[type="button"]:-moz-focusring, 
 
[type="reset"]:-moz-focusring, 
 
[type="submit"]:-moz-focusring { 
 
  outline: 1px dotted ButtonText; 
 
} 
 
fieldset { 
 
  padding: 0.35em 0.75em 0.625em; 
 
} 
 
legend { 
 
  box-sizing: border-box; 
 
  /* 1 */ 
 
  color: inherit; 
 
  /* 2 */ 
 
  display: table; 
 
  /* 1 */ 
 
  max-width: 100%; 
 
  /* 1 */ 
 
  padding: 0; 
 
  /* 3 */ 
 
  white-space: normal; 
 
  /* 1 */ 
 
} 
 
progress { 
 
  display: inline-block; 
 
  /* 1 */ 
 
  vertical-align: baseline; 
 
  /* 2 */ 
 
} 
 
textarea { 
 
  overflow: auto; 
 
} 
 
[type="checkbox"], 
 
[type="radio"] { 
 
  box-sizing: border-box; 
 
  /* 1 */ 
 
  padding: 0; 
 
  /* 2 */ 
 
} 
 
[type="number"]::-webkit-inner-spin-button, 
 
[type="number"]::-webkit-outer-spin-button { 
 
  height: auto; 
 
} 
 
[type="search"] { 
 
  -webkit-appearance: textfield; 
 
  /* 1 */ 
 
  outline-offset: -2px; 
 
  /* 2 */ 
 
} 
 
[type="search"]::-webkit-search-cancel-button, 
 
[type="search"]::-webkit-search-decoration { 
 
  -webkit-appearance: none; 
 
} 
 
 ::-webkit-file-upload-button { 
 
  -webkit-appearance: button; 
 
  /* 1 */ 
 
  font: inherit; 
 
  /* 2 */ 
 
} 
 
details,  
 
/* 1 */  
 
menu { 
 
  display: block; 
 
} 
 
summary { 
 
  display: list-item; 
 
} 
 
canvas { 
 
  display: inline-block; 
 
} 
 
template { 
 
  display: none; 
 
} 
 
[hidden] { 
 
  display: none; 
 
}  
 
/* reset */  
 
html, 
 
body, 
 
h1, 
 
h2, 
 
h3, 
 
h4, 
 
h5, 
 
h6, 
 
div, 
 
dl, 
 
dt, 
 
dd, 
 
ul, 
 
ol, 
 
li, 
 
p, 
 
blockquote, 
 
pre, 
 
hr, 
 
figure, 
 
table, 
 
caption, 
 
th, 
 
td, 
 
form, 
 
fieldset, 
 
legend, 
 
input, 
 
button, 
 
textarea, 
 
menu { 
 
  margin: 0; 
 
  padding: 0; 
 
  box-sizing: border-box; 
 
} 
  • H5样式初始化
html { 
 
  line-height: 1.15; 
 
  /* 1 */ 
 
  -ms-text-size-adjust: 100%; 
 
  /* 2 */ 
 
  -webkit-text-size-adjust: 100%; 
 
  /* 2 */ 
 
} 
 
body { 
 
  margin: 0; 
 
} 
 
article, 
 
aside, 
 
footer, 
 
header, 
 
nav, 
 
section { 
 
  display: block; 
 
} 
 
h1 { 
 
  font-size: 2em; 
 
  margin: 0.67em 0; 
 
} 
 
figcaption, 
 
figure, 
 
main { 
 
  /* 1 */ 
 
  display: block; 
 
} 
 
figure { 
 
  margin: 1em 40px; 
 
} 
 
hr { 
 
  box-sizing: content-box; 
 
  /* 1 */ 
 
  height: 0; 
 
  /* 1 */ 
 
  overflow: visible; 
 
  /* 2 */ 
 
} 
 
pre { 
 
  font-family: monospace, monospace; 
 
  /* 1 */ 
 
  font-size: 1em; 
 
  /* 2 */ 
 
} 
 
a { 
 
  background-color: transparent; 
 
  /* 1 */ 
 
  -webkit-text-decoration-skip: objects; 
 
  /* 2 */ 
 
} 
abbr[title] { 
 
  border-bottom: none; 
 
  /* 1 */ 
 
  text-decoration: underline; 
 
  /* 2 */ 
 
  text-decoration: underline dotted; 
 
  /* 2 */ 
 
} 
 
b, 
 
strong { 
 
  font-weight: inherit; 
 
} 
 
b, 
 
strong { 
 
  font-weight: bolder; 
 
} 
 
code, 
 
kbd, 
 
samp { 
 
  font-family: monospace, monospace; 
 
  /* 1 */ 
 
  font-size: 1em; 
 
  /* 2 */ 
 
} 
 
dfn { 
 
  font-style: italic; 
 
} 
 
mark { 
 
  background-color: #ff0; 
 
  color: #000; 
 
} 
 
small { 
 
  font-size: 80%; 
 
} 
 
sub, 
 
sup { 
 
  font-size: 75%; 
 
  line-height: 0; 
 
  position: relative; 
 
  vertical-align: baseline; 
 
} 
 
sub { 
 
  bottom: -0.25em; 
 
} 
 
sup { 
 
  top: -0.5em; 
 
} 
 
audio, 
 
video { 
 
  display: inline-block; 
 
} 
audio:not([controls]) { 
 
  display: none; 
 
  height: 0; 
 
} 
 
img { 
 
  border-style: none; 
 
} 
 
svg:not(:root) { 
 
  overflow: hidden; 
 
}  
 
button, 
 
input, 
 
optgroup, 
 
select, 
 
textarea { 
 
  font-family: sans-serif; 
 
  /* 1 */ 
 
  font-size: 100%; 
 
  /* 1 */ 
 
  line-height: 1.15; 
 
  /* 1 */ 
 
  margin: 0; 
 
  /* 2 */ 
 
} 
 
button, 
 
input { 
 
  /* 1 */ 
 
  overflow: visible; 
 
} 
  
button, 
 
select { 
 
  /* 1 */ 
 
  text-transform: none; 
 
} 
 
button, 
 
html [type="button"],  
 
/* 1 */  
 
[type="reset"], 
 
[type="submit"] { 
 
  -webkit-appearance: button; 
 
  /* 2 */ 
 
} 
button::-moz-focus-inner, 
 
[type="button"]::-moz-focus-inner, 
 
[type="reset"]::-moz-focus-inner, 
 
[type="submit"]::-moz-focus-inner { 
 
  border-style: none; 
 
  padding: 0; 
 
} 
 
button:-moz-focusring, 
 
[type="button"]:-moz-focusring, 
 
[type="reset"]:-moz-focusring, 
 
[type="submit"]:-moz-focusring { 
 
  outline: 1px dotted ButtonText; 
 
} 
 
fieldset { 
 
  padding: 0.35em 0.75em 0.625em; 
 
} 
 
legend { 
 
  box-sizing: border-box; 
 
  /* 1 */ 
 
  color: inherit; 
 
  /* 2 */ 
 
  display: table; 
 
  /* 1 */ 
 
  max-width: 100%; 
 
  /* 1 */ 
 
  padding: 0; 
 
  /* 3 */ 
 
  white-space: normal; 
 
  /* 1 */ 
 
} 
 
progress { 
 
  display: inline-block; 
 
  /* 1 */ 
 
  vertical-align: baseline; 
 
  /* 2 */ 
 
} 
 
textarea { 
 
  overflow: auto; 
 
} 
 
[type="checkbox"], 
 
[type="radio"] { 
 
  box-sizing: border-box; 
 
  /* 1 */ 
 
  padding: 0; 
 
  /* 2 */ 
 
} 
 
[type="number"]::-webkit-inner-spin-button, 
 
[type="number"]::-webkit-outer-spin-button { 
 
  height: auto; 
 
} 
 
[type="search"] { 
 
  -webkit-appearance: textfield; 
 
  /* 1 */ 
 
  outline-offset: -2px; 
 
  /* 2 */ 
 
} 
 
[type="search"]::-webkit-search-cancel-button, 
 
[type="search"]::-webkit-search-decoration { 
 
  -webkit-appearance: none; 
 
} 
 
 ::-webkit-file-upload-button { 
 
  -webkit-appearance: button; 
 
  /* 1 */ 
 
  font: inherit; 
 
  /* 2 */ 
 
} 
 
details,  
 
/* 1 */  
 
menu { 
 
  display: block; 
 
} 
 
summary { 
 
  display: list-item; 
 
} 
 
canvas { 
 
  display: inline-block; 
 
} 
 
template { 
 
  display: none; 
 
} 
 
[hidden] { 
 
  display: none; 
 
} 
 
/* reset */ 
 
html, 
 
body, 
 
h1, 
 
h2, 
 
h3, 
 
h4, 
 
h5, 
 
h6, 
 
div, 
 
dl, 
 
dt, 
 
dd, 
 
ul, 
 
ol, 
 
li, 
 
p, 
 
blockquote, 
 
pre, 
 
hr, 
 
figure, 
 
table, 
 
caption, 
 
th, 
 
td, 
 
form, 
 
fieldset, 
 
legend, 
 
input, 
 
button, 
 
textarea, 
 
menu { 
 
  margin: 0; 
 
  padding: 0; 
 
  box-sizing: border-box; 
 
} 
html, 
 
body { 
 
  /* 禁止选中文本 */ 
 
  -webkit-user-select: none; 
 
  user-select: none; 
 
  font: Oswald, 'Open Sans', Helvetica, Arial, sans-serif 
 
} 
 
/* 禁止长按链接与图片弹出菜单 */ 
a, 
 
img { 
 
  -webkit-touch-callout: none; 
 
} 
 
/*ios android去除自带阴影的样式*/ 
 
a, 
 
input { 
 
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0); 
 
} 
 
input[type="text"] { 
 
  -webkit-appearance: none; 
 
} 
  • 公共样式提取
/* 禁止选中文本 */ 
 
.usn{ 
 
    -webkit-user-select:none;  
    -moz-user-select:none;  
    -ms-user-select:none;  
    -o-user-select:none;  
    user-select:none;  
} 

/* 浮动 */ 
 
.fl { float: left; } 
 
.fr { float: right; } 
 
.cf { zoom: 1; } 
 
.cf:after { 
 
    content:"."; 
 
    display:block; 
 
    clear:both; 
 
    visibility:hidden; 
 
    height:0; 
 
    overflow:hidden; 
 
} 

/* 元素类型 */ 
 
.db { display: block; } 
 
.dn { display: none; } 
 
.di { display: inline } 
 
.dib {display: inline-block;} 
 
.transparent { opacity: 0 }

/*文字排版、颜色*/ 
 
.f12 { font-size:12px } 
 
.f14 { font-size:14px } 
 
.f16 { font-size:16px } 
 
.f18 { font-size:18px } 
 
.f20 { font-size:20px } 
 
.fb { font-weight:bold } 
 
.fn { font-weight:normal } 
 
.t2 { text-indent:2em } 
 
.red,a.red { color:#cc0031 } 
 
.darkblue,a.darkblue { color:#039 } 
 
.gray,a.gray { color:#878787 } 
 
.lh150 { line-height:150% } 
 
.lh180 { line-height:180% } 
 
.lh200 { line-height:200% } 
 
.unl { text-decoration:underline; } 
 
.no_unl { text-decoration:none; } 
 
.tl { text-align: left; } 
 
.tc { text-align: center; } 
 
.tr { text-align: right; } 
 
.tj { text-align: justify; text-justify: inter-ideograph; } 
 
.wn { /* 强制不换行 */  
    word-wrap:normal;  
    white-space:nowrap;  
} 
 
.wb { /* 强制换行 */  
    white-space:normal;  
    word-wrap:break-word;  
    word-break:break-all;  
} 
 
.wp { /* 保持空白序列*/  
    overflow:hidden;text-align:left;white-space:pre-wrap;word-wrap:break-word;word-break:break-all;  
} 
 
.wes { /* 多出部分用省略号表示 , 用于一行 */  
    overflow:hidden;  
    word-wrap:normal;  
    white-space:nowrap;  
    text-overflow:ellipsis;  
} 
 
.wes-2 { /* 适用于webkit内核和移动端 */  
    display: -webkit-box;  
    -webkit-box-orient: vertical;  
    -webkit-line-clamp: 2;  
    overflow: hidden;  
}  
 
.wes-3 {  
    display: -webkit-box;  
    -webkit-box-orient: vertical;  
    -webkit-line-clamp: 3;  
    overflow: hidden;  
} 
 
.wes-4 {  
    display: -webkit-box;  
    -webkit-box-orient: vertical;  
    -webkit-line-clamp: 4;  
    overflow: hidden;  
} 
 
 
 
/* 溢出样式 */  
.ofh { overflow: hidden; }  
.ofs {overflow: scroll; }  
.ofa {overflow: auto; }  
.ofv {overflow: visible; } 
  
 
/* 定位方式 */  
.ps {position: static; }  
.pr {position: relative;zoom:1; }  
.pa {position: absolute; }  
.pf {position: fixed; }  
 
 
/* 垂直对齐方式 */  
.vt {vertical-align: top; }  
.vm {vertical-align: middle; }  
.vb {vertical-align: bottom; }  
 
/* 鼠标样式 */  
.csd {cursor: default; }  
.csp {cursor: pointer; }  
.csh {cursor: help; }  
.csm {cursor: move; }  
 
 
/* flex布局 */  
.df-sb {  
    display:flex;  
    align-items: center;  
    justify-content: space-between;  
} 
 
.df-sa {  
    display:flex;  
    align-items: center;  
    justify-content: space-around;  
} 
 
 
/* 垂直居中 */  
.df-c {  
    display: flex;  
    align-items: center;  
    justify-content: center; 
 } 
 
.tb-c {  
    text-align:center;  
    display:table-cell;  
    vertical-align:middle;  
} 
 
.ts-c {  
    position: absolute;  
    left: 50%; top: 50%;  
    transform: translate(-50%, -50%);  
} 
 
.ts-mc {  
    position: absolute;  
    left: 0;right: 0;  
    bottom: 0; top: 0;  
    margin: auto;  
} 
  
 
/* 辅助 */  
.mask-fixed-wrapper {  
    width: 100%;  
    height: 100%;  
    position: fixed;  
    left:0;top:0;  
    background: rgba(0, 0, 0, 0.65);  
    z-index: 999;  
} 
 
.bg-cover {  
    background-size: cover;  
    background-repeat: no-repeat;  
    background-position: center center;  
} 
 
.bg-cover-all {  
    background-size: 100% 100%;  
    background-repeat: no-repeat;  
    background-position: center center;  
} 
posted @ 2020-01-16 11:43  smile_or  阅读(4969)  评论(0编辑  收藏  举报