首页 > 代码库 > utils.js文件;一些常用方法的备份

utils.js文件;一些常用方法的备份

一些常用方法备份:

技术分享
function _(value) {
    value = ‘0‘ + value;
    return value.substr(value.length - 2);
}
Date.prototype.format = function(split) {
    split = split || "";

    if(split === ‘time‘) {
        return _(this.getHours()) + ‘:‘ + _(this.getMinutes()) + ‘:‘ + _(this.getSeconds()); /*返回  时:分:秒*/
    } else {
        var month = this.getMonth() + 1;
        month = month < 10 ? "0" + month : month;
        var day = this.getDate();
        day = day < 10 ? "0" + day : day;

        return this.getFullYear() + split + month + split + day; /*返回  年月日*/
    }
}

Date.prototype.time = function() {
    var hour = this.getHours();
    var minute = this.getMinutes();
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;

    return hour + ":" + minute; /*返回  时:分*/
}

Date.prototype.addDays = function(n) {
    return new Date(this.setDate(this.getDate() + n));
};

Date.prototype.addMonths = function(n) {
    return new Date(this.setMonth(this.getMonth() + n));
};

Array.prototype.max = function() {
    return Math.max.apply(null, this);
}
Array.prototype.min = function() {
    return Math.min.apply(null, this);
}

Array.prototype.maxVal = function() {
    return this.sort()[this.length - 1];
}
Array.prototype.minVal = function() {
    return this.sort()[0];
}

Array.prototype.update = function(value) {
    var pos = $.inArray(value, this);
    if(pos === -1) {
        this.push(value);
    } else {
        this.splice(pos, 1);
    }
    return this;
}

String.prototype.format = function(split) {
    split = split || ‘‘;
    var value;
    if(this.indexOf(‘/‘) !== -1) {
        value = this.replace(/\//g, split);
    } else if(this.indexOf(‘-‘) !== -1) {
        value = this.replace(/-/g, split);
    } else {
        value = this.substr(0, 4) + split + this.substr(4, 2) + split + this.substr(6);
    }

    if(split === ‘date‘) {
        var arr = value.split(split);
        value = new Date(arr[0], parseInt(arr[1]) - 1, arr[2])
    }

    if(split === ‘timeStamp‘) {
        var arr = value.split(split);
        value = new Date(arr[0], parseInt(arr[1]) - 1, arr[2])
        value = value.getTime();
    }

    return value;
}

String.prototype.decimal = function() {
    var value;
    if(this.indexOf(‘.‘) == -1) {
        value = this + ‘.00‘;
    } else if(this.split(".")[1].length == 1) {
        value = this + ‘0‘;
    } else {
        value = this + ‘‘;
    }
    return value;
}

var A = {
    Service: {
        get: function(url, data, success, error) {
            $.ajax({
                url: url,
                type: "GET",
                dataType: "json",
                data: data || "",
                cache: false,
                success: function(result) {
                    success && success(result);
                },
                error: function(xhr) {
                    var index = layer.alert(xhr.responseJSON[0].content, {
                        title: ‘提示‘
                    }, function() {
                        if(xhr.status == 401) {
                            layer.close(index);
                            //调取父级页面的退出函数
                            self.parent.window.quit();
                        } else {
                            layer.close(index);
                        }
                    });
                    error && error();
                }

            })
        },
        post: function(url, data, success, error) {

            data = JSON.stringify(data);

            $.ajax({
                url: url,
                type: "POST",
                dataType: "json",
                data: data,
                cache: false,
                contentType: "application/json",
                success: function(result) {
                    success && success(result);
                },
                error: function(xhr) {
                    if(xhr.status == 401) {
                        var index = layer.alert(xhr.responseJSON[0].content, {
                            title: ‘提示‘
                        }, function() {
                            layer.close(index);
                            self.parent.window.quit();
                        });
                    } else if(xhr.status == 404) {
                        var index = layer.alert(‘访问错误或内部服务器错误‘, {
                            title: ‘提示‘
                        }, function() {
                            layer.close(index);
                        });
                    } else {
                        layer.msg(xhr.responseJSON[0].content, {
                            time: 2000
                        });
                    }
                    error && error();
                }

            })
        },
        jsonp: function(url, data, callback) {
            $.ajax({
                url: url,
                dataType: "jsonp",
                data: data,
                jsonp: "callback",
                success: function(result) {
                    callback(result);
                },
                timeout: 3000
            });
        }
    },
    params: function() {
        var params = decodeURIComponent(window.location.search).substr(1);
        if(params.indexOf(‘=‘) !== -1) {
            var arr = params.split(‘&‘);
            var json = {};
            for(var i = 0; i < arr.length; i++) {
                var t = arr[i].split(‘=‘);
                json[t[0]] = t[1];
            }
            return json;
        } else {
            return params;
        }
    },
    hash: function() {
        var hash = decodeURIComponent(window.location.hash).substr(1);
        var arr = hash.split(‘-‘);
        return arr[1];
    },
    //字符串//获取//最后一位
    gerStr: function(str) {
        var s = ‘‘;
        s = str.charAt(str.length - 1);
        return s;
    },
    //字符串//去除//最后一位
    cutStr: function(str) {
        var s = ‘‘;
        s = str.substring(0, str.length - 1);
        return s;
    },
    express: {
        mobile: {
            exp: /^[1][3-8]\d{9}$/,
            message: "请输入正确的手机号码"
        },
        password: {
            exp: /^[A-Za-z0-9_@]+$/,
            message: "密码由字母、数字、下划线或@组成"
        },
        enterpriseName: {
            exp: /^[a-zA-Z\u4e00-\u9fa5()()_]+$/,
            message: "企业名称只能输入中英文、下划线或括号"
        },
        license: {
            exp: /^[0-9A-Za-z\-]+$/,
            message: "营业执照号码只允许数字英文中横线"
        },
        address: {
            exp: /^[0-9a-zA-Z\u4e00-\u9fa5#()()_-]+$/,
            message: "公司地理位置只能包含中英文、数字、下划线、中横线、括号和#号"
        },
        userName: {
            exp: /^[a-zA-Z\u4e00-\u9fa5]+$/,
            message: "姓名只能是中英文格式"
        },
        fixedTel: {
            exp: /^[0-9\s,、,\-]*$/,
            message: "办公电话只能包含数字逗号顿号中横线和空格"
        },
        jobName: {
            exp: /^[0-9a-zA-Z\u4e00-\u9fa5()()_]+$/,
            message: "职位名称只能包含中英文数字下划线或括号"
        },
        recruitNum: {
            exp: /^\+?[1-9][0-9]*$/,
            message: "招聘人数只能是非0正整数~"
        },
        salary: {
            exp: /^([1-9]\d*|0)(\.\d{1,2})?$/,
            message: "薪酬可以是正整数或包含两位小数"
        },
        height: {
            exp: /^\d{2,3}$/,
            message: "身高只能是2-3位的整数"
        },
        email: {
            exp: /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/,
            message: "请检查邮箱格式"
        },
        money: {
            exp: /^(([1-9]\d*)|\d)(\.\d{1,2})?$/,
            message: "请输入正确的支付金额"
        },
        regCode: {
            exp: /^\d{4}$/,
            message: "请输入正确的验证码"
        }
    }
}
View Code

 

utils.js文件;一些常用方法的备份