1. 程式人生 > >JS 常用封裝類[日期格式化、js互動]

JS 常用封裝類[日期格式化、js互動]

    //轉換響應資料中的日期

    ChangeDateFormat: function (new Date()) {
        var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    },


    //格式化日期物件為yyyy-MM-dd格式

    formatTime: function (new Date()) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return [year, month, day].map(function (n) { n = n.toString(); return n[1] ? n : '0' + n }).join('-');

    },

//JS  封裝前後臺互動

var Temp{

 //封裝ajax請求[給予常規預設值,並判斷呼叫可行性]
    query: function (options) {//url,para,callback
        var defaultPara = {
            type: "POST",
            showLoading: true,
            async: true
        };
        var opt = $.extend(defaultPara, options);
        $.ajax({
            url: opt.url,
            data: opt.data,
            type: opt.type,
            async: opt.async,
            beforeSend: function (xhr) {
                if (opt.showLoading) {
                    var index = layer.load(0, {
                        shade: [0.01, '#000']
                    });
                };
            },
            success: function (res) {
                layer.closeAll('loading');
                if (res.errcode == 0) {
                    if (typeof opt.callback == "function") {
                        opt.callback(res);
                    } else if (opt.callback != undefined) {
                        throw new Error(opt.callback + "不是一個函式");
                    };
                } else if (res.errcode == 3) {
                    if (typeof opt.callback3 == "function") {
                        opt.callback3(res);
                    } else if (opt.callback3 != undefined) {
                        throw new Error(opt.callback3 + "不是一個函式");
                    };
                } else {
                    var idx = layer.alert(res.errmsg, {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    }, function () {
                        layer.close(idx);


                        if (typeof opt.callback2 == "function") {
                            opt.callback2();
                        } else if (opt.callback2 != undefined) {
                            throw new Error(opt.callback2 + "不是一個函式");
                        };
                    });
                };
            },
            dataType: "json"
        });

    },’

//呼叫後臺資料並返回方法

    getInitData: function (options, callback) {
        this.query({
            url: "/Area/Controller/Action",
            data: options,
            callback: callback

        });
    },
}

//直接呼叫封裝的方法

Temp.getInitData({
    "storeid": CWBookOrder.currStoreID

}, function (res) {

console.log(res)

});