js辅助

大概结构

正文

(function ($) {
    var qrbase = function () {
    };
    qrbase.constructor = {};
    qrbase.prototype = {
        baseobj: function () {
            return {
                //接口更目录 
                apiurl: '',
                token: "token"
            };
        },
        pathbuild: function (x, y) {
            var _x = x.replace('http://', '').replace('https://');
            var _y = y.replace('http://', '').replace('https://');
            var h2 = y.indexOf('http://') > -1 || y.indexOf('https://') > -1;
            var xarry = _x.split('/');
            var yarry = _y.split('/');
            for (var i = 0; i < xarry.length; i++) {
                if (xarry[i] == yarry[0])
                    xarry.length = i;
            }
            var _b = xarry.concat(yarry).join('/');
            if (h2) return y.split('//')[0] + '//' + _b;
            return x.split('//')[0] + '//' + _b;
        },
        ajax: function (url, data, fn, err,isload) {
            var owner = this;
            if (isload) owner.load();
            $.ajax({
                url: this.pathbuild(this.baseobj().apiurl, url) + "?t=" + Math.random(),
                data: data,
                type: 'get',
                dataType: "jsonp",
                cache: false,
                success: function (d) {
                    if(isload) owner.clearload();
                    fn(d);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    owner.clearload();
                    if (typeof (err) == 'function') {
                        err();
                    }
                }
            });
        },
        cookie: function (k) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(k + "=")
                if (c_start != -1) {
                    c_start = c_start + k.length + 1
                    c_end = document.cookie.indexOf(";", c_start)
                    if (c_end == -1) c_end = document.cookie.length
                    return unescape(document.cookie.substring(c_start, c_end))
                }
            }
            return ""
        },
        setCookie: function (c_name, value, expiredays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + expiredays);
            document.cookie = c_name + "=" + escape(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + '; path=/';
        },
        setCookieM: function (c_name, value, expireMinutes) {
            var exdate = new Date();
            exdate.setMinutes(exdate.getMinutes() + expireMinutes);
            document.cookie = c_name + "=" + escape(value) +
            ((expireMinutes == null) ? "" : ";expires=" + exdate.toGMTString()) + '; path=/';
        },
        gettoken: function () {
            return this.cookie(this.baseobj().token);
        },
        queryString: function (name) {
            var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
            if (result == null || result.length < 1) { return ""; }
            return result[1];
        }, 
        isphone: function (s) {
            var reg = /^1\d{10}$/;
            //var reg = /^0?1[3|4|5|8][0-9]\d{8}$/;
            return reg.test(s);
        },
        isemail: function (s) {
            var reg = /\w@\w*\.\w/;
            return reg.test(s);
        }, 
        alert: function (msgtitle, content, callback) {
            layer.open({
                content: content
                , btn: ['确定']
                , yes: function (index) {
                    if (callback) callback();
                    layer.close(index);
                }
            });
        },
        confirm: function (msgtitle, content, yes, no) {
            layer.confirm(content, {
                title: msgtitle,
                btn: ['确定', '取消']
            }, function () { yes(); layer.closeAll(); }, function () { no(); layer.closeAll(); });
        },
        load: function () {
            layer.open({ type: 2, shadeClose: false, shade: 'background-color: rgba(0,0,0,.3)' });
        },
        clearload: function () {
            layer.closeAll();
        },
        add: function (a, b) {
            var c, d, e;
            try {
                c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), ($.qr.mul(a, e) + $.qr.mul(b, e)) / e;
        },
        sub: function (a, b) {
            var c, d, e;
            try {
                c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), ($.qr.mul(a, e) - $.qr.mul(b, e)) / e;
        },
        mul: function (a, b) {
            var c = 0,
                d = a.toString(),
                e = b.toString();
            try {
                c += d.split(".")[1].length;
            } catch (f) { }
            try {
                c += e.split(".")[1].length;
            } catch (f) { }
            return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
        },
        div: function (a, b) {
            var c, d, e = 0,
                f = 0;
            try {
                e = a.toString().split(".")[1].length;
            } catch (g) { }
            try {
                f = b.toString().split(".")[1].length;
            } catch (g) { }
            return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), $.qr.mul(c / d, Math.pow(10, f - e));
        }, 
        number_format: function (n,b) {//b=true,不四舍五入
            if (isNaN(n)) return n;
            var _e = false;
            if (n < 0) {
                n = Math.abs(n);
                _e = true;
            }
            if (!b) n = n.toFixed(2); else n = n.toFixed(3);
            var zsw = n.split('.')[0], xsw = n.split('.')[1], str = '', restr = '';
            for (var i = 1; i <= zsw.length; i++) {
                str += zsw.substr(zsw.length - i, 1);
                if (i % 3 == 0) str += ',';
            }
            if (zsw.length % 3 == 0) str = str.substr(0, str.length - 1);
            for (var i = 1; i <= str.length; i++) {
                restr += str.substr(str.length - i, 1);
            }
            if (xsw.length >= 2 && b) {
                xsw = xsw.substr(0, 2);
            }
            if (_e) {
                return '-' + restr + '.' + xsw;
            } else {
                return restr + '.' + xsw;
            }
        }, 
        int_format: function (n) {
            if (isNaN(n)) return n;
            var _e = false;
            if (n < 0) {
                n = Math.abs(n);
                _e = true;
            }
            n = n.toFixed(2);
            var zsw = n.split('.')[0], xsw = n.split('.')[1], str = '', restr = '';
            for (var i = 1; i <= zsw.length; i++) {
                str += zsw.substr(zsw.length - i, 1);
                if (i % 3 == 0) str += ',';
            }
            if (zsw.length % 3 == 0) str = str.substr(0, str.length - 1);
            for (var i = 1; i <= str.length; i++) {
                restr += str.substr(str.length - i, 1);
            }
            if (_e) {
                return '-' + restr;
            } else {
                return restr;
            }
        },
        pageinit: function (pageindex, pageall) {
            var _h = '<a  href="javascript:;" page="1">首页</a>';
            var _n = pageindex - 1; 
            var start = pageindex - 2 >= 1 ? pageindex - 2 : 1;
            var end = pageindex + 2 <= pageall ? pageindex + 2 : pageall;
            if (start > 1)
                _h += '<a href="javascript:;" page="' + (start - 1) + '">..</a>';
            for (var i = start; i <= end; i++) {
                if (i == pageindex) {
                    _h += '<a href="javascript:;" class="current" page="' + i + '">' + i + '</a>';
                } else {
                    _h += '<a href="javascript:;" page="' + i + '">' + i + '</a>';
                }
            }
            if (end < pageall)
                _h += '<a href="javascript:;" page="' + (end + 1) + '">..</a>';
            if (pageall > 1 && pageindex < pageall)
                _h += '<a class="pbtn" href="javascript:;" page="' + ((pageindex + 1) > pageall ? pageall : (pageindex + 1)) + '">下一页</a>'; 
            _h += '<a class="pbtn" href="javascript:;" page="' + pageall + '">尾页</a>';
            return _h;
        } 
    }; 
    $.qr = new qrbase();
})(jQuery);

 

posted @ 2017-06-28 08:35  卖果子的小男孩  阅读(460)  评论(0编辑  收藏  举报