vue 封装公用函数

Vue 函数封装

  1.  格式化浏览器时间
    /**
     * 格式化时间
     * @param params
     * @param blo 默认为true
     * @returns {string}
     * @constructor 冯刚 2019年6月12日11点01分
     */
    function TimeConversion(params,blo=true){
        var stamp = Date.parse(params);
        var newDate= new Date(stamp);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var date = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if(blo)
            return year + '-' + getNow(month) + "-" + getNow(date);
        return year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
    
    }

     

  2. 校验字符串最后是否存在斜杠
    /**
     * 验证最后是否有反斜杠
     * @param value
     * @returns {*}
     * @constructor 冯刚 2019年6月12日
     */
    function Verification(value) {
        if (value.length > 0 && value !== '') {
            var str = value.substr(value.length - 1, 1);
            if (str !== '/' && str !== '') {
                value += '/';
            }
        }
        return value;
    }

     

  3. 字符串加密
    /**
     * 加密
     * @param code 要加密的字符串
     * @returns {string}
     */
    function compileStr(code) {
        var c = String.fromCharCode(code.charCodeAt(0) + code.length);
        for (var i = 1; i < code.length; i++) {
            c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
        }
        return escape(c);
    }

     

  4. 字符串解密
    /**
     * 解密
     * @param code 要解密的字符串
     * @returns {string}
     */
    function uncompileStr(code) {
        code = unescape(code);
        var c = String.fromCharCode(code.charCodeAt(0) - code.length);
        for (var i = 1; i < code.length; i++)
            c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
        return c;
    }

     

  5. 根据key获取浏览器地址后参数
    /**
     * js获取url传递指定参数,解决url中带中文乱码的问题(根据key获取value)
     * @param key
     * @returns {string|null}
     */
    function getQueryString(key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r !== null) return decodeURI(r[2]); return null;
    }

     

  6. 文本框非空校验,文本框添加红色样式
    /**
     * 文本框非空验证
     * @param type 自定义类型(.或#)
     * @param name 页面中自定义类型名称
     * @author 冯刚 2019年6月12日
     */
    function isNotNull(type, name) {
        var temp, select;
        var isNull = false;
        if (checkValue(type, name))
            isNull = true;
        temp = type + name;
        if (!isNull)
            $($(temp)).each(function () {
                var _this = $(this);
                _this = reductionStyle(_this);
                temp = _this.children('input').val();
                select = _this.children('div').children('input').val();
                if (temp === '' || temp === null) {
                    isNull = true;
                    _this.children('input').css('border', 'solid red 1px');
                }
                if (select === '' || select === null) {
                    isNull = true;
                    _this.children('div').children('input').css('border', 'solid red 1px');
                }
            });
        return isNull;
    }

     

  7. 重置初始化样式
    /**
     * 重置初始化样式
     * @param type
     * @param name
     * @returns {boolean}
     */
    function resetStyle(type, name) {
        var temp;
        var isBool = false;
        if (checkValue(type, name))
            isBool = true;
        temp = type + name;
        if (!isBool)
            $($(temp)).each(function () {
                var _this = $(this);
                _this = reductionStyle(_this);
                isBool = true;
            });
        return isBool;
    }

     

  8. 数据封装成 data 对象,并且去除空格
    /**
     * 封装数据
     * @param data 数据对象(用于添加修改)部分可用
     */
    function packData(data){
        var vmData={};
        for (var o in data) {
            if (data[o] !== null && data[o] instanceof Array)
                vmData[o]=null;
            else{
                if(typeof data[o] === 'string' && data[o].length > 0)
                    vmData[o]=data[o].trim();
                else
                    vmData[o]=data[o];
            }
        }
        return vmData;
    }

       

  9. 动态绑定数据
    /**
     * 动态赋值(用于查看编辑)
     * @param orgObj
     * @param newObj
     * @returns {*}
     * @constructor fg 2019年6月12日
     */
    function AssignmentObject(orgObj, newObj){
        for (var o in orgObj) {
            if (!(orgObj[o] !== null && orgObj[o] instanceof Array))
            {
                for (var n in newObj){
                    if(o==n){
                        orgObj[o]=newObj[n];
                        break;
                    }
                }
            }
        }
        return orgObj;
    }

     

  10. 清空文本框内容,重置内容
    /**
     * 按钮重置内容
     * @param data
     */
    function clearContent(data) {
        var v_data = {};
        for (var o in data) {
            if (data[o] !== null && data[o] instanceof Array)
                v_data[o] = data[o];
            else {
                v_data[o] = null;
            }
        }
        return v_data;
    }

     

  11. 部分函数校验
    /**
     * 内部引用 还原样式
     * @param obj
     * @returns {*}
     */
    function reductionStyle(obj) {
        obj.children('input').css('border', '1px solid #dcdfe6');
        obj.children('div').children('input').css('border', '1px solid #dcdfe6');
        return obj;
    }
    
    /**
     * 内部引用 检测选择器以及类型名称是否输入全
     * @param key
     * @param value
     * @returns {boolean}
     */
    function checkValue(key, value) {
        var isBool = false;
        if (isCheck(key) && isCheck(value)) {
            isBool = true;
            alert('请检查选择器类型及名称');
        }
        if (isCheck(key)) {
            isBool = true;
            alert('选择器类型不能为空:(./#)');
        }
        if (isCheck(value)) {
            isBool = true;
            alert('选择器名称不能为空');
        }
        return isBool;
    }
    
    /**
     * 内部引用 校验值是否为空
     * @param value
     * @returns {boolean}
     */
    function isCheck(value) {
        value = value.trim();
        if (value === null || value === '' || value === 'undefined')
            return true;
        return false;
    }

     

  12. 获取当前时间
    /**
     * 校验时间
     * @param s
     * @returns {string}
     */
    function getNow(s) {
        return s < 10 ? '0' + s : s;
    }
    
    /**
     * 获取当前时间
     * @returns {string}
     */
    function getDate() {
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth() + 1;
        var date = myDate.getDate();
        var h = myDate.getHours();
        var m = myDate.getMinutes();
        var s = myDate.getSeconds();
        return year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
    }

     作者地址:https://www.cnblogs.com/FGang/p/11124070.html

posted @ 2019-07-03 00:45  Hi,Sky  阅读(4923)  评论(0编辑  收藏  举报