一个小型的类库

/**
 * Created with IntelliJ IDEA.
 * User: zet
 * Date: 13-1-10
 * Time: 下午5:46
 * To change this template use File | Settings | File Templates.
 */
/**
* 字符串和日期工具
*/
(function (window, document) {
    String.prototype.Trim = function () {
        return this.replace(/(^s*)|(s*$)/g, "");
    };
    String.prototype.getBytesCount = function (str) {
        if (str == null) {
            return 0;
        } else {
            return str.replace(/[^x00-xff]/g, "**").length;
        }
    };
    Date.prototype.nextDate = function () {
        return new Date(this.getTime() + 86400000);
    };
    Date.prototype.getMyDay = function () {
        var temp = this.getDay();
        switch (temp) {
            case 0:
                return "周日";
            case 1:
                return "周一";
            case 2:
                return "周二";
            case 3:
                return "周三";
            case 4:
                return "周四";
            case 5:
                return "周五";
            case 6:
                return "周六";
            default:
                return '';
        }
    };
    Date.prototype.getMyMonth = function () {
        return (this.getMonth() + 1) + '月';
    };
    Date.prototype.toMyString = function () {
        var month = this.getMonth() + 1;
        return this.getFullYear() + '-' + (month >= 10 ? month : ('0' + month)) + '-' + this.getDate();
    };

})(window, document);
/**
 * 获取地理位置
 */
(function(window, document) {
    window.Drore = window.Drore || {};
    var nav = navigator;
    Drore.Location = Drore.Location || null;
    Drore.getLocation = function(cb) {
        var pos = null;
        if (nav.geolocation) {
            nav.geolocation.getCurrentPosition(function(position) {
                pos = {
                    lat: position.coords.latitude,
                    lon: position.coords.longitude
                }
                cb(pos);
            }, function() {
                console.log('千岛湖旅游客户端提示您:获取您的位置信息失败!');
                cb(pos);
            }, {
                timeout: 3000
            });
        } else {
            alert('您的浏览器不支持定位!');
            cb(pos);
        }
    };
})(window, document);
/**
 * ajax and html help function
 */
(function(window, document) {
    window.Drore = window.Drore || {};
    var head = document.getElementsByTagName('head')[0],
        i,
        imgCache = document.createElement('img');
    Drore.importJs = function(url, cb, charset) {
        var s = document.createElement("script");
        s.src = url;
        if (charset) {
            s.charset = charset
        }
        s.onload = s.onreadystatechange = function() {
            if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
                s.onload = s.onreadystatechange = null;
                this.parentNode.removeChild(this);
                if (cb) {
                    cb(true)
                }
            };
        };
        s.onerror = function() {
            this.onload = this.onerror = null;
            this.parentNode.removeChild(this);
            if (cb) {
                cb(false)
            }
        };
        head.appendChild(s)
    };
    Drore.importCSS = function(url) {
        var link = document.createElement('link');
        link.setAttribute('rel', 'stylesheet');
        link.setAttribute('type', 'text/css');
        link.setAttribute('href', url);
        head.appendChild(link)
    };

    Drore.importMap = function(cb) {
        this.importCSS(window.Config.Map.cssUrl)
        this.importJs(window.Config.Map.jsUrl, cb);
    };
    
    Drore._paras = (function() {
        var url = window.location.href,
            start = url.indexOf("?") + 1,
            paras = {};
        if (start !== 0) {
            var queryString = url.substring(start);
            var paraNames = queryString.split("&");
            var arr = [],
                i = 0;
            for (; i < paraNames.length; i++) {
                arr = paraNames[i].split("=");
                if (i === paraNames.length - 1) {
                    var sIndex = arr[1].indexOf("#");
                    if (sIndex !== -1) {
                        arr[1] = arr[1].substring(0, sIndex);
                    }
                }
                paras[arr[0]] = arr[1]
            }
        }
        return paras
    })();
    Drore.getParameter = function(pName) {
        return Drore._paras[pName];
    };
    Drore.getReqParams = function(paramObj) {
        var arr = new Array();
        for (i in paramObj) {
            arr.push(i + '=' + paramObj[i]);
        }
        return arr.join('&');
    };
    Drore.loadImg = function(targetDom, url) {
        if (url.indexOf("undefined")!=-1) {
            targetDom.src = Config.ImgLazyLoading.errorUrl;
        }
        else {
            var img = imgCache.cloneNode(true);
            targetDom.src = Config.ImgLazyLoading.loadingUrl;
            img.src = url + "_192X0_70.jpg";
            img.onload = function() {
                targetDom.src = url + "_192X0_70.jpg";
                img.onload = null;
                img.onerror = null;
                img = null;
            }
            img.onerror = function() {
                targetDom.src = url; 
                img.onload = null;
                img.onerror = null;
                img = null;
            }
        }
        
    };
    Drore.loadBigImg = function(targetDom, url) {
        var img = imgCache.cloneNode(true);
        targetDom.src = Config.ImgLazyLoading.loadingUrl;
        img.src = url;
        img.onload = function() {
            targetDom.src = url;
            img.onload = null;
            img.onerror = null;
            img = null;
        }
        img.onerror = function() {
            targetDom.src = Config.ImgLazyLoading.errorUrl;
            img.onload = null;
            img.onerror = null;
            img = null;
        }
    };
})(window, document);
/**
 * 本地缓存
 */
(function(window) {
    window.Drore = window.Drore || {};
    window.Drore.storage = {
        set: function(key, value) {
            window.localStorage.setItem(key, value);
        },
        get: function(key) {
            return window.localStorage.getItem(key);
        }
    };
})(window);
/**
 * dom 操作
 */
(function(document, window) {
    window.Drore = window.Drore || {};
    Drore.$ = function (id) {
        return "string" == typeof id ? document.getElementByI(id) : id;
    };
})(document, window);
/**
 * Config
 *
 * !!!!
 *  如果前端页面和服务器部署在不同的域名下,那么Safari浏览器登录和注册就会出错!!
 */
(function() {
    window.Config = {
        Map: {
            cssUrl: 'http://api.map.baidu.com/res/14/bmap.css',
            jsUrl: 'http://api.map.baidu.com/getscript?v=1.4&key=&services=&t=20130219081854'
        },
        APIUrl: {
            host: 'http://api.weyoo.cn',
            // host: 'http://192.168.16.239',
            // port: '8080',
            port: "80",
            imageHost: 'http://img.weyoo.cn',

            //首页广告
            indexAd: function(paramObj) {
                return this.host + ':' + this.port + '/advert/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店列表
            hotelUrl: function(paramObj) {
                return this.host + ':' + this.port + '/hotel/list.htm?' + Drore.getReqParams(paramObj);
            },

            //没房型的酒店信息
            hotelInfo: function(paramObj) {
                return this.host + ':' + this.port + '/hotel/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店预订
            hotelBook: function(paramObj) {
                return this.host + ':' + this.port + '/hotel/book.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店预订支付
            hotelBookPay: function(paramObj) {
                return this.host + ':' + this.port + '/hotel/book_pay.htm?' + Drore.getReqParams(paramObj);
            },

            //订单支付
            pay: function(paramObj) {
                return this.host + ':' + this.port + '/pay/pay_order.htm?' + Drore.getReqParams(paramObj);
            },

            //有房型的酒店信息
            hotelDetail: function(paramObj) {
                return this.host + ':' + this.port + '/hotel/room.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店评价提交
            hotelCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_hotel.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店房型评价提交
            RoomCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_hotel_room.htm?' + Drore.getReqParams(paramObj);
            },

            //景区列表
            viewUrl: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/list.htm?' + Drore.getReqParams(paramObj);
            },

            //景点详情
            pointDetail: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/pointdetail.htm?' + Drore.getReqParams(paramObj);
            },

            // 景点列表
            pointList: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/pointlist.htm?' + Drore.getReqParams(paramObj);
            },

            //景区详情
            viewDetail: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //景点评价提交
            pointCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_scenic_point.htm?' + Drore.getReqParams(paramObj);
            },

            //景点评论列表
            pointCommentList: function(paramObj) {
                return this.host + ':' + this.port + '/comment/scenic_point_list.htm?' + Drore.getReqParams(paramObj);
            },

            // 景区评论提交
            scenicCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_scenic.htm?' + Drore.getReqParams(paramObj);
            },

            //美食列表
            cateringUrl: function(paramObj) {
                return this.host + ':' + this.port + '/provider/list.htm?' + Drore.getReqParams(paramObj);
            },


            //娱乐列表
            recreatUrl: function(paramObj) {
                return this.host + ':' + this.port + '/provider/list.htm?' + Drore.getReqParams(paramObj);
            },

            //商家详情
            providerDetail: function(paramObj) {
                return this.host + ':' + this.port + '/provider/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //特产店列表
            specialtyUrl: function(paramObj) {
                return this.host + ':' + this.port + '/provider/list.htm?' + Drore.getReqParams(paramObj);
            },

            //商品类别列表
            goodsTypeList: function(paramObj) {
                return this.host + ':' + this.port + '/goods_type/list.htm?' + Drore.getReqParams(paramObj);
            },

            //商品类别详情
            goodsTypeDetail: function(paramObj) {
                return this.host + ':' + this.port + '/goods_type/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //商品列表
            goodsUrl: function(paramObj) {
                return this.host + ':' + this.port + '/goods/list.htm?' + Drore.getReqParams(paramObj);
            },

            //商品预订
            goodsBook: function(paramObj) {
                return this.host + ':' + this.port + '/goods/book.htm?' + Drore.getReqParams(paramObj);
            },

            // 门票详情
            ticketsDetail: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/ticketdetail.htm?' + Drore.getReqParams(paramObj);
            },

            // 门票预订
            ticketsBook: function(paramObj) {
                return this.host + ':' + this.port + '/scenic/book.htm?' + Drore.getReqParams(paramObj);
            },

            // 订单详情
            bookDetail: function(paramObj) {
                return this.host + ':' + this.port + '/order/view.htm?' + Drore.getReqParams(paramObj);
            },

            // 取消订单
            cancelOrder: function(paramObj){
                return this.host + ':' + this.port + '/order/cancel.htm?' + Drore.getReqParams(paramObj);
            },

            // 删除订单
            delOrder: function(paramObj){
                return this.host + ':' + this.port + '/order/delete.htm?' + Drore.getReqParams(paramObj);
            },

            //商品详情
            goodsDetail: function(paramObj) {
                return this.host + ':' + this.port + '/goods/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //商品评价提交
            goodsCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_goods.htm?' + Drore.getReqParams(paramObj);
            },

            //商家评价提交
            shopCommentSubmit: function(paramObj) {
                return this.host + ':' + this.port + '/comment/save_provider.htm?' + Drore.getReqParams(paramObj);
            },
            //商家类别
            shopType: function(paramObj) {
                return this.host + ':' + this.port + '/provider/type.htm?' + Drore.getReqParams(paramObj);
            },

            //注册
            registerUrl: function(paramObj) {
                return this.host + ':' + this.port + '/mem/reg.htm?' + Drore.getReqParams(paramObj);
            },
            // 注册请求验证码
            registerCodeRequest: function(paramObj) {
                return this.host + ':' + this.port + '/mem/reg_get_code.htm?' + Drore.getReqParams(paramObj);
            },
            //验证验证码
            codeValidate: function(paramObj) {
                return this.host + ':' + this.port + '/mem/code_validate.htm?' + Drore.getReqParams(paramObj);
            },
            //登陆
            loginUrl: function(paramObj) {
                return this.host + ':' + this.port + '/mem/login.htm?' + Drore.getReqParams(paramObj);
            },
            // 重置密码请求验证码
            resetCodeRequest: function(paramObj) {
                return this.host + ':' + this.port + '/mem/password_reset_code.htm?' + Drore.getReqParams(paramObj);
            },
            // 重置密码校验验证码
            resetCodeValidate: function(paramObj) {
                return this.host + ':' + this.port + '/mem/password_code_validate.htm?' + Drore.getReqParams(paramObj);
            },
            // 重置密码
            resetPassword: function(paramObj) {
                return this.host + ':' + this.port + '/mem/password_reset.htm?' + Drore.getReqParams(paramObj);
            },
            //交通
            trafficUrl: function(paramObj) {
                return this.host + ':' + this.port + '/serv/traffic_type.htm?' + Drore.getReqParams(paramObj);
            },
            //交通列表
            trafficList: function(paramObj) {
                return this.host + ':' + this.port + '/serv/traffic_list.htm?' + Drore.getReqParams(paramObj);
            },
            //交通详情
            trafficDetail: function(paramObj) {
                return this.host + ':' + this.port + '/serv/traffic_detail.htm?' + Drore.getReqParams(paramObj);
            },
            //便民信息类别
            bianminIndex: function(paramObj) {
                return this.host + ':' + this.port + '/serv/conv_type.htm?' + Drore.getReqParams(paramObj);
            },
            //便民信息列表
            bianminList: function(paramObj) {
                return this.host + ':' + this.port + '/serv/conv_list.htm?' + Drore.getReqParams(paramObj);
            },
            // 便民信息详情
            bianminDetail: function(paramObj) {
                return this.host + ':' + this.port + '/serv/conv_detail.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-个人资料查询
            getUserInfo: function(paramObj) {
                return this.host + ':' + this.port + '/mem/self_info.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-修改密码
            pwdUpdate: function(paramObj) {
                return this.host + ':' + this.port + '/mem/password_update.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-头像修改
            headImgUpdate: function(paramObj) {
                return this.host + ':' + this.port + '/mem/update_head_img.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-背景墙设置
            setBackground: function(paramObj) {
                return this.host + ':' + this.port + '/mem/set_bground_wall.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-注销登录
            logout: function(paramObj) {
                return this.host + ':' + this.port + '/mem/logout.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-个人资料更新
            setUserInfo: function(paramObj) {
                return this.host + ':' + this.port + '/mem/update_self_info.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-评论列表
            getCommentList: function(paramObj) {
                return this.host + ':' + this.port + '/comment/self_list.htm?' + Drore.getReqParams(paramObj);
            },
            //用户中心-删除评论
            deleteComment: function(paramObj) {
                return this.host + ':' + this.port + '/comment/delete.htm?' + Drore.getReqParams(paramObj);
            },

            //用户中心-我的咨询列表
            getMyHelpList: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/my_help_list.htm?' + Drore.getReqParams(paramObj);
            },

            //用户中心-我的咨询列表
            getMyComplaintList: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/my_complaint_list.htm?' + Drore.getReqParams(paramObj);
            },

            //用户中心-咨询投诉删除
            deleteService: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/delete.htm?' + Drore.getReqParams(paramObj);
            },

            //用户中心-订单列表
            getOrderList: function(paramObj) {
                return this.host + ':' + this.port + '/order/my_order.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-他人相册列表
            getAlbumList: function(paramObj) {
                return this.host + ':' + this.port + '/mem/album/list.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-我的相册列表
            getMyAlbumList: function(paramObj) {
                return this.host + ':' + this.port + '/mem/album/self.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-相册图片列表
            getPhotoList: function(paramObj) {
                return this.host + ':' + this.port + '/mem/photo/list.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-相册图片上传
            photoUpload: function(paramObj) {
                return this.host + ':' + this.port + '/mem/photo/upload_img.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-更新图片信息
            photoUpdate: function(paramObj) {
                return this.host + ':' + this.port + '/mem/photo/update.htm?' + Drore.getReqParams(paramObj);
            },

            // 用户中心-相册图片删除
            photoDelete: function(paramObj) {
                return this.host + ':' + this.port + '/mem/photo/delete.htm?' + Drore.getReqParams(paramObj);
            },

            //卖家评论列表(景区,酒店,商家)
            commentList: function(paramObj) {
                return this.host + ':' + this.port + '/comment/seller_list.htm?' + Drore.getReqParams(paramObj);
            },

            //酒店房型评论列表
            roomCommentList: function(paramObj) {
                return this.host + ':' + this.port + '/comment/room_list.htm?' + Drore.getReqParams(paramObj);
            },

            //商品评论列表
            goodsCommentList: function(paramObj) {
                return this.host + ':' + this.port + '/comment/goods_list.htm?' + Drore.getReqParams(paramObj);
            },

            //评分项配置查询
            getScoreSetting: function(paramObj) {
                return this.host + ':' + this.port + '/comment/get_score_setting.htm?' + Drore.getReqParams(paramObj);
            },

            //攻略列表
            getGuideList: function(paramObj) {
                return this.host + ':' + this.port + '/guide/list.htm?' + Drore.getReqParams(paramObj);
            },

            //攻略详情
            getGuideDetail: function(paramObj) {
                return this.host + ':' + this.port + '/guide/detail.htm?' + Drore.getReqParams(paramObj);
            },

            //咨询列表
            getHelpList: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/help_list.htm?' + Drore.getReqParams(paramObj);
            },

            //投诉列表
            getComplaintList: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/complaint_list.htm?' + Drore.getReqParams(paramObj);
            },

            //咨询投诉保存
            serviceSave: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/save.htm?' + Drore.getReqParams(paramObj);
            },

            //咨询投诉详情
            getServiceDetail: function(paramObj) {
                return this.host + ':' + this.port + '/serviceform/detail.htm?' + Drore.getReqParams(paramObj);
            },
            //会员资料查询
            getUserInfoById: function(paramObj) {
                return this.host + ':' + this.port + '/mem/info_detail.htm?' + Drore.getReqParams(paramObj);
            },
            //便民信息
            getConvenienceList: function(paramObj) {
                return this.host + ':' + this.port + '/serv/conv_list.htm?' + Drore.getReqParams(paramObj);
            },
            // 新闻资讯列表
            newsList: function(paramObj) {
                return this.host + ':' + '/article/arti_list.htm?' + Drore.getReqParams(paramObj);
            },
            // 新闻资讯详情
            newsDetail: function(paramObj){
                return this.host + ':' + '/article/detail.htm?' + Drore.getReqParams(paramObj);
            },
            // 特色菜列表
            specialDish: function(paramObj){
                return this.host + ':' + '/provider/dish_list.htm?' + Drore.getReqParams(paramObj);
            },
            // 特色菜详情
            specialDishDetail: function(paramObj){
                return this.host + ':' + '/provider/dish_detail.htm?' + Drore.getReqParams(paramObj);
            },
            // 休闲点赞
            xiuxianZan: function(paramObj){
                return this.host + ':' + '/scenic/view.htm?' + Drore.getReqParams(paramObj);
            },
            // 特产点赞
            specialZan: function(paramObj){
                return this.host + ':' + '/goods_type/view.htm?' + Drore.getReqParams(paramObj);
            }
        },
        ImgLazyLoading: {
            loadingUrl: '', //'/img/loading.gif',
            errorUrl: '/img/default.png'
        }
    }
})();
/**
* 登陆及异常处理
*/
(function(window, document) {
    window.Drore = window.Drore || {};
    window.Drore.Validate = {
        phone: /^1[3|4|5|8][0-9]{9}$/,
        password: /^[a-z0-9A-Z_]{6,12}$/,
        name: /^[u4e00-u9fa5w]{2,10}$/
    };
    //异常处理
    Drore.exception = function(type) {
        switch (type) {
            case 2:
                alert("网络问题,请稍候再试!");
                break;
            case 3:
                alert("网络问题,请稍候再试!");
                break;
            case 4:
                if (confirm("您还未登录,是否先登录!")) {
                    Drore.pointLogin();
                }
                break;
        }
    }
    //记录当前url并指向登录
    Drore.pointLogin = function() {
        var url = location.href,
            requestURINo = "requestURINo" + Math.random();
        Drore.storage.set(requestURINo, url);
        location.assign("/user/login.html?requestURINo=" + requestURINo);
    }
})(window, document);
原文地址:https://www.cnblogs.com/huangjianhuakarl/p/3591764.html