javascriptcommon js

Posted 酱油帝君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascriptcommon js相关的知识,希望对你有一定的参考价值。

function Common() { };
Common.prototype = function () {
    return {
        alert: function (msg) {
            layer.msg(msg, { time: 1000 });
            //layer.open({
            //    type: 1,
            //    shadeClose: false,
            //    content: ‘\<\div style="padding:20px;">‘ + msg + ‘\<\/div>‘
            //});
        },
        confirm: function (msg, succ, cancel) {
            layer.confirm(msg, {
                btn: [‘确定‘, ‘取消‘] //按钮
            }, succ, cancel);
        },
        loading: function (msg) {
            if (window.layer) {
                layer.load()
            }
        },
        closeloading: function () {
            layer.closeAll();
        },
        ajax: function (setting) {
            if (!setting.dataType) setting.dataType = "json";
            if (!setting.type) setting.type = "post";
            $.ajax({
                url: setting.url,
                type: setting.type,
                dataType: setting.dataType,
                data: setting.data,
                success: function (data) {
                    if (setting.success)
                        setting.success(data);
                },
                error: setting.error
            })
        },
        query: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        ready: function (a) {
            document.onreadystatechange = function () {
                if (document.readyState == "complete") {
                    if (a) a();
                }
            }
        },
    }
}();
//基于jq的通用部分
Common.prototype.jquery = function () {
    return {
        //调用示例
        exp: function () {
            com.jquery.initFileUpload({
                a: $file,
                b: function (pic, pic_2, pic_4, pic_6) {//上传成功后返回 4中格式的图片
                    setTimeout(function () {
                        $file.prev().prop("src", pic);
                        com.closeloading();
                    }, 1000)
                },
                c: function (a, b) {//图片选择完毕之后,b.submit()就是把图片上传的意思
                    com.loading();
                    b.submit();
                }
            });
        },
        initFileUpload: function (setting) {
            if (!setting) com.alert("缺少参数setting");
            if (!setting.a) com.alert("缺少参数file input");
            var a = setting.a;
            var b = setting.b;//success
            var c = setting.c;
            var url = "http://114.112.101.70:91/api/uploadimage/eggworld_product_0_0_0_true";
            var fp = a.fileupload({
                url: url,
                dataType: ‘json‘,
                forceIframeTransport: true,
                autoUpload: setting.autoUpload ? setting.autoUpload : true,
                redirect: setting.redirect ? setting.redirect : "http://" + window.location.host + "/Scripts/result.html",
                add: function (e, data) {
                    if (c) c(e, data);
                    else {
                        if (e.isDefaultPrevented()) {
                            return false;
                        }
                        if (data.autoUpload || (data.autoUpload !== false &&
                                $(this).fileupload(‘option‘, ‘autoUpload‘))) {
                            data.process().done(function () {
                                data.submit();
                            });
                        }
                    }
                },
                done: function (e, res) {
                    console.log(res)
                    if (res.result.Code == 200) {
                        var suffix = res.result.Datas[0].ImgUrl.substring(res.result.Datas[0].ImgUrl.lastIndexOf(‘.‘));
                        var picUrl = res.result.Datas[0].ImgUrl.replace(suffix, suffix);
                        var picUrl_6 = res.result.Datas[0].ImgUrl.replace(suffix, ‘_6‘ + suffix);
                        var picUrl_4 = res.result.Datas[0].ImgUrl.replace(suffix, ‘_4‘ + suffix);
                        var picUrl_2 = res.result.Datas[0].ImgUrl.replace(suffix, ‘_2‘ + suffix);
                        if (b) b(picUrl, picUrl_2, picUrl_4, picUrl_6, setting.tag);
                    }
                },
                fail: function () {
                    if (setting.d) setting.d();
                },
            });//end fileupload
        },
    };
}();
//数据绑定模板
Common.prototype.tmpl = function () {
    return {
        //普通参数直接绑定{createtime}
        //表达式支持函数调用{model.getDate($createtime,$updatetiem)}
        //a 模板的html
        //b list.item
        getTmpl: function (a, b) {
            //console.log(b)
            var regKK = new RegExp("{(.*?)}", "g");
            var c = a.match(regKK);

            for (var i = 0; i < c.length; i++) {
                c[i] = c[i].replace(/\{/g, "").replace(/\}/g, "");
            }
            for (var i = 0; i < c.length; i++) {
                var tempRegItem = c[i];
                //var regFuc = new RegExp(".+?\((\d+?)\)", "g");
                var regFuc = new RegExp("\\((.| )+?\\)", "g");
                if (regFuc.test(tempRegItem)) {
                    //function 
                    var ar1 = tempRegItem.split("(")[0];//function name
                    //console.log(tempRegItem);
                    var parastr = tempRegItem.split("(")[1].split(")")[0];
                    var ar2 = parastr.split(",");//参数数组

                    for (var aritem in ar2) {
                        aritem = ar2[aritem];
                        var treg = new RegExp("\\" + aritem, "g");
                        if (b[aritem.replace(/\$/g, "")]) {
                            parastr = parastr.replace(treg, "‘" + b[aritem.replace(/\$/g, "")] + "‘");
                        } else {
                            parastr = parastr.replace(treg, "‘‘");
                        }
                    }
                    var fucstr = ar1 + "(" + parastr + ")";
                    //console.log(fucstr);
                    var r = eval(fucstr);
                    a = a.replace("{" + tempRegItem + "}", r);
                }
                else if (b[tempRegItem]) {
                    var reg = new RegExp("{" + tempRegItem + "}", "g");
                    a = a.replace(reg, b[tempRegItem]);
                } else {
                    var reg = new RegExp("{" + tempRegItem + "}", "g");
                    a = a.replace(reg, " ");
                }
            }
            return a;
        },
        getTmpls: function (a, b) {
            var str = "";
            for (var i = 0; i < b.length; i++) {
                str += this.getTmpl(a, b[i]);
            }
            return str;
        }
    }
}();

Common.prototype.word = function () {
    return {
        errorMsg: "网络错误,请稍后再试"
    };
}();

Common.prototype.modelPage = function () {
    return {//分页控件
        //调用示例
        codeExp: function () {
            common.modelPage.initPage({
                pagerowcount: 215,//数据总共多少条
                pageindex: 1,//当前第几页
                pagesize: 10,//一页多少条
                domid: ‘pageid‘,
                pageclick: function (pageindex) { }//分页点击事件
            })
        },
        //分页 配合梅珍写的样式使用
        initPage: function (setting) {
            if (setting.pagerowcount <= setting.pageindex * setting.pagesize) {
                return;
            }
            this.pagecontent(setting);
        },
        pagecontent: function (setting) {
            if (!setting.pageindex) setting.pageindex = 1;
            if (!setting.pagesize) setting.pagesize = 10;
            if (!setting.showcount) setting.showcount = 5;
            if (!setting.pagerowcount) alert("缺少参数pagerowcount");
            if (!setting.domid) alert("缺少参数domid");
            if (!setting.pageclick) alert("缺少回调函数pageclick");

            if (setting.pagerowcount) {
                setting.pagecount = parseInt(setting.pagerowcount % setting.pagesize > 0 ? (setting.pagerowcount / setting.pagesize) + 1 : setting.pagerowcount / setting.pagesize);
            }
            var itemthis = this;
            var div_page = document.createElement("div");
            div_page.className = "page";
            div_page.id = setting.domid;
            var div_pageIn = document.createElement("div");
            div_pageIn.className = "pageIn";

            this.pageset(setting, div_pageIn, itemthis);

            div_page.appendChild(div_pageIn);
            var dom = document.getElementById(setting.domid);
            dom.parentElement.replaceChild(div_page, dom);
        },
        pageset: function (setting, div_pageIn, itemthis) {
            /*处理分页参数*/
            if (setting.pageindex > 1) {
                var link_a = document.createElement("a");
                link_a.innerText = "首页";
                link_a.onclick = function () { itemthis.pageclick.call(itemthis, setting, 1); };
                div_pageIn.appendChild(link_a);
                var link_a = document.createElement("a");
                link_a.innerText = "上一页";
                if (setting.pageindex <= 1) link_a.disabled = "disabled";
                else
                    link_a.onclick = function () { itemthis.pageclick.call(itemthis, setting, setting.pageindex - 1); };
                div_pageIn.appendChild(link_a);
            }

            //var tempshowcount = setting.pagecount < setting.showcount ? setting.pagecount : setting.showcount;
            for (var i = 1; i <= setting.showcount; i++) {
                var link_a = document.createElement("a");
                link_a.innerText = parseInt((setting.pageindex - 1) / setting.showcount) * setting.showcount + i;
                if (link_a.innerText > setting.pagecount) break;
                if (link_a.innerText == setting.pageindex) {
                    link_a.className = "now";
                }

                var pageitem = this;

                link_a.onclick = function (pagei) {
                    return function () {
                        itemthis.pageclick.call(itemthis, setting, pagei);
                    }
                }(link_a.innerText);
                div_pageIn.appendChild(link_a);
            }

            if (setting.pageindex < setting.pagecount) {
                var link_a = document.createElement("a");
                link_a.innerText = "下一页";
                if (setting.pageindex >= setting.pagecount) link_a.disabled = "disabled";
                else link_a.onclick = function () { itemthis.pageclick.call(itemthis, setting, setting.pageindex + 1); };
                div_pageIn.appendChild(link_a);

                var link_a = document.createElement("a");
                link_a.innerText = "最后一页";
                link_a.onclick = function () { itemthis.pageclick.call(itemthis, setting, setting.pagecount); };
                div_pageIn.appendChild(link_a);
            }
            /*处理分页参数END*/
        },
        pageclick: function (setting, page) {
            page = parseInt(page);
            setting.pageindex = page;
            this.pagecontent(setting);
            setting.pageclick(page);
        },
        loadCss: function () {
            var dom = document.createElement("style");
            dom.type = "text/css";
            dom.innerText = ".page{width:100%;height:auto;margin-top:20px}.page .pageIn{width:620px;margin:0 auto}.page .pageIn a{display:inline-block;padding:0 15px;height:40px;border:1px solid #ccc;margin-right:3px;line-height:40px;color:#555;font-size:14px}.page .pageIn .now{background:#2dc3e8;border:1px solid #2dc3e8;color:#fff}";
            window.document.body.appendChild(dom);
        },
    }
}();

//返回
Common.prototype.rtModel = function () {
    return {
        rtuname: "ReturnUrl",
        rtuvalue: function () {
            return com.query(this.rtuname);
        },
        back: function (url) {
            if (this.rtuvalue()) {
                window.location = this.rtuvalue();
            } else if (url) {
                window.location = url;
            } else {
                window.history.back();
            }
        }
    };
}();
var Com = new Common();
var com = Com;
var common = Com;
com.queryString = com.query;

 

以上是关于javascriptcommon js的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

js代码片段: utils/lcoalStorage/cookie

JS代码片段:一个日期离现在多久了

js常用代码片段(更新中)

js常用代码片段