util.js 11.3 KB
function t(t, e) {
    if (!(t instanceof e)) throw new TypeError("Cannot call a class as a function");
}

function e(t, e, n) {
    return e in t ? Object.defineProperty(t, e, {
        value: n,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = n, t;
}

function n(t) {
    return t < 10 ? "0" + t : t;
}

function o(t) {
    var e = new Date(parseInt(t)), n = e.getFullYear(), o = e.getMonth() + 1, r = e.getDate(), a = e.getHours(), i = e.getMinutes(), u = e.getSeconds();
    a || (a = 0), i || (i = 0), u || (u = 0);
    var s = "";
    s = a && a < 10 ? "0" + a : a + "";
    var g = "";
    g = i && i < 10 ? "0" + i : i + "";
    var c = "";
    c = u && u < 10 ? "0" + u : u + "", "0" == s && (s = "00"), "0" == g && (g = "00"), 
    "0" == c && (c = "00");
    var f = new Date();
    Date.parse(f.toDateString());
    return n + "年" + o + "月" + r + "日 ";
}

var r, a = function() {
    function t(t, e) {
        for (var n = 0; n < e.length; n++) {
            var o = e[n];
            o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), 
            Object.defineProperty(t, o.key, o);
        }
    }
    return function(e, n, o) {
        return n && t(e.prototype, n), o && t(e, o), e;
    };
}(), i = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t;
} : function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
}, u = require("./restapi.js"), s = function(t) {
    return (t = t.toString())[1] ? t : "0" + t;
};

String.prototype.format = function() {
    if (0 == arguments.length) return this;
    var t = arguments, e = this;
    "object" === i(arguments[0]) && 1 == arguments.length && (t = arguments[0]);
    for (var n in t) e = e.replace(new RegExp("\\{" + n + "\\}", "g"), t[n]);
    return e;
}, Date.prototype.format = function(t) {
    var e = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds()
    };
    /(y+)/.test(t) && (t = t.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)));
    for (var n in e) new RegExp("(" + n + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? e[n] : ("00" + e[n]).substr(("" + e[n]).length)));
    return t;
};

var g = new (function() {
    function e() {
        t(this, e);
    }
    return a(e, [ {
        key: "get",
        value: function(t, e, n) {
            return this.request("GET", t, e, n);
        }
    }, {
        key: "post",
        value: function(t, e, n) {
            return this.request("POST", t, e, n);
        }
    }, {
        key: "request",
        value: function(t, e, n, o) {
            var r = "POST" == t ? {
                "content-type": "application/json"
            } : {};
            (0, u.req)("/web-dj-weapp-mp/" + e, n, r, t, "json", "text", o);
        }
    } ]), e;
}())();

module.exports = (r = {
    formatTime: function(t) {
        var e = t.getFullYear(), n = t.getMonth() + 1, o = t.getDate(), r = t.getHours(), a = t.getMinutes(), i = t.getSeconds();
        return [ e, n, o ].map(s).join("/") + " " + [ r, a, i ].map(s).join(":");
    },
    timeago: function(t) {
        var e = new Date().getTime() - t, n = new Date();
        n.setHours(0), n.setMinutes(0), n.setSeconds(0), n.setMilliseconds(0);
        var o = n.getTime();
        if (e < 0) return "刚刚";
        var r = o - t, a = e / 6e4, i = e / 36e5, u = r / 864e5;
        return r > 0 ? u >= 2 ? new Date(t).format("MM月dd日 hh:mm") : u >= 1 && u < 2 ? "前天" + new Date(t).format(" hh:mm") : "昨天" + new Date(t).format(" hh:mm") : i >= 1 && i <= 23 ? parseInt(i) + "小时前" : a >= 1 && a <= 59 ? parseInt(a) + "分钟前" : e >= 0 && e <= 6e4 ? "刚刚" : new Date(t).format("MM月dd日 hh:mm");
    },
    showLoading: function(t) {
        wx.showLoading ? wx.showLoading({
            title: t,
            mask: !0
        }) : wx.showToast({
            title: t,
            icon: "loading",
            mask: !0,
            duration: 2e4
        });
    },
    hideLoading: function() {
        wx.hideLoading ? wx.hideLoading() : wx.hideToast();
    },
    date_format: function(t) {
        var e = n(parseInt(t / 60 / 60 / 24, 10)), o = n(parseInt(t / 60 / 60 % 24, 10)), r = n(parseInt(t / 60 % 60, 10)), a = n(parseInt(t % 60, 10));
        return 0 == e ? o + ":" + r + ":" + a : e + "天 " + o + ":" + r + ":" + a;
    },
    formatDateTime: function(t) {
        var e = new Date(t), n = (e.getFullYear(), e.getMonth() + 1);
        n = n < 10 ? "0" + n : n;
        var o = e.getDate();
        o = o < 10 ? "0" + o : o;
        var r = e.getHours();
        r = r < 10 ? "0" + r : r;
        var a = e.getMinutes(), i = e.getSeconds();
        a = a < 10 ? "0" + a : a, i = i < 10 ? "0" + i : i;
        var u = new Date(), s = u.getMonth() + 1;
        s = s < 10 ? "0" + s : s;
        var g = u.getDate();
        return g = g < 10 ? "0" + g : g, s == n && g == o ? "今日" + r + ":" + a + ":" + i : n + "月" + o + "日 " + r + ":" + a + ":" + i;
    },
    standardDateTime: function(t) {
        var e = new Date(parseInt(t)), n = e.getFullYear(), o = e.getMonth() + 1, r = e.getDate(), a = e.getHours(), i = e.getMinutes(), u = e.getSeconds();
        a || (a = 0), i || (i = 0), u || (u = 0);
        var s = "";
        s = a && a < 10 ? "0" + a : a + "";
        var g = "";
        g = i && i < 10 ? "0" + i : i + "";
        var c = "";
        c = u && u < 10 ? "0" + u : u + "", "0" == s && (s = "00"), "0" == g && (g = "00"), 
        "0" == c && (c = "00");
        var f = new Date();
        Date.parse(f.toDateString());
        return n + "年" + o + "月" + r + "日 " + s + ":" + g + ":" + c;
    },
    standardDateTimeWithoutHMS: o
}, e(r, "standardDateTimeWithoutHMS", o), e(r, "getCountDownFormat", function(t) {
    var e = parseInt(t / 1e3), n = parseInt(t % 1e3), o = 0, r = 0;
    return e > 60 && (o = parseInt(e / 60), e = parseInt(e % 60), o > 60 && (r = parseInt(o / 60), 
    o = parseInt(o % 60))), e = e > 9 ? e : "0" + e, o = o > 9 ? o : "0" + o, r = r > 9 ? r : "0" + r, 
    {
        ms: n,
        ss: e,
        mm: o,
        hh: r
    };
}), e(r, "day_format", function(t) {
    return n(parseInt(t / 60 / 60 / 24, 10));
}), e(r, "hour_format", function(t) {
    return n(parseInt(t / 60 / 60 % 24, 10));
}), e(r, "minute_format", function(t) {
    return n(parseInt(t / 60 % 60, 10));
}), e(r, "second_format", function(t) {
    return n(parseInt(t % 60, 10));
}), e(r, "MsToDuration", function(t) {
    var e = 0, n = 0, o = 0, r = 0, a = 0;
    return t > 0 && ((e = (t / 1e3).toFixed(2)) >= 60 && (n = Math.floor(e / 60), e = (e % 60).toFixed(2)), 
    n >= 60 && (o = Math.floor(n / 60), n %= 60), o >= 24 && (r = Math.floor(o / 24), 
    o %= 24), r >= 365 && (a = Math.floor(r / 365), r %= 365)), a > 0 ? a + "年" + r + "天" + o + "小时" + n + "分" : r > 0 ? r + "天" + o + "小时" + n + "分" : o > 0 ? o + "小时" + n + "分" : n + "分";
}), e(r, "MsToYear", function(t) {
    var e = 0, n = 0, o = 0, r = 0, a = 0;
    return t > 0 && ((e = (t / 1e3).toFixed(2)) >= 60 && (n = Math.floor(e / 60), e = (e % 60).toFixed(2)), 
    n >= 60 && (o = Math.floor(n / 60), n %= 60), o >= 24 && (r = Math.floor(o / 24), 
    o %= 24), r >= 365 && (a = Math.floor(r / 365), r %= 365)), a > 0 && r > 0 ? a + "年" + r + "天" : a > 0 && 0 == r ? a + "年" : r > 0 || 0 == r ? r + "天" : "0天";
}), e(r, "dateToStr", function(t, e) {
    return t.format(e);
}), e(r, "futureDate", function(t) {
    null == t && (t = 0), null == t && (t = 0);
    var e = new Date(), n = e.setMinutes(e.getMinutes() + t);
    e = new Date(n);
    var o, r = new Date(), a = r.getMonth() + 1, i = e.getMonth() + 1, u = r.getDate(), s = e.getDate(), g = e.getHours(), c = e.getMinutes();
    return i > a ? (i < 10 && (i = "0" + i), s < 10 && (s = "0" + s), o = i + "月" + s + "日") : (s - u == 0 ? s = "今天" : s - u == 1 ? s = "明天" : s - u == 2 && (s = "后天"), 
    o = s), g < 10 && (g = "0" + g), c < 10 && (c = "0" + c), o += g + ":" + c;
}), e(r, "throttle", function(t, e) {
    null != e && void 0 != e || (e = 1500);
    var n = null;
    return function() {
        var o = +new Date();
        (o - n > e || !n) && (t.apply(this, arguments), n = o);
    };
}), e(r, "promise", function(t) {
    return new Promise(function(e, n) {
        t(e, n);
    });
}), e(r, "navigateTo", function(t, e) {
    wx.navigateTo({
        url: t,
        success: function(t) {
            t.eventChannel.emit("getData", e);
        }
    });
}), e(r, "getData", function(t, e) {
    t ? 0 !== Object.keys(t.getOpenerEventChannel()).length && t.getOpenerEventChannel().on("getData", function(t) {
        e(t);
    }) : console.log("缺少this");
}), e(r, "navigateBack", function(t, n) {
    var o = getCurrentPages(), r = o[o.length - 2];
    return t && "object" === (void 0 === t ? "undefined" : i(t)) && Object.keys(t).map(function(n) {
        r.setData(e({}, n, t[n]));
    }), wx.navigateBack(), n && "string" == typeof n && r[n](), r;
}), e(r, "getViewInfo", function(t, e, n) {
    (n ? wx.createSelectorQuery().in(n) : wx.createSelectorQuery()).select(t).boundingClientRect(function(t) {
        e(t);
    }).exec();
}), e(r, "onReachBottom", function(t, n, o) {
    n.pageSize || (n.pageSize = 10), n.nothing || (n.nothing = "nothing"), Math.ceil(n.total / n.pageSize) > n.pageNum ? (t.setData({
        pageNum: n.pageNum + 1
    }), o()) : t.setData(e({}, n.nothing, !0));
}), e(r, "api", g), e(r, "showToast", function(t) {
    var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "none", n = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 1500;
    console.log(t), wx.showToast({
        title: t || "出错了,请联系管理员",
        icon: e,
        duration: n
    });
}), e(r, "resolveCouponDiscountType", function(t) {
    var e = "";
    return 1 == t.discountType ? e = "满" + t.full + "减" + t.denomination : 2 == t.discountType ? e = t.denomination + "折" : 3 == t.discountType ? e = t.denomination + "元代金券" : 4 == t.discountType && (e = "实物券"), 
    e;
}), e(r, "resolveCouponType", function(t) {
    var e = "";
    if ("number" == typeof t) 1 == t ? e = "通用" : 2 == t ? e = "保养" : 3 == t ? e = "维修" : 4 == t ? e = "钣喷" : 5 == t ? e = "清洗" : 6 == t && (e = "工时"); else if (-1 != t.indexOf(";")) {
        var n = t.split(";");
        if (n) if (1 == n.length) "1" == t[0] ? e = "通用" : "2" == t[0] ? e = "保养" : "3" == t[0] ? e = "维修" : "4" == t[0] ? e = "钣喷" : "5" == t[0] ? e = "清洗" : "6" == t[0] && (e = "工时"); else {
            for (var o = 0; o < n.length; o++) "1" == t[o] ? e += "通用," : "2" == t[o] ? e += "保养," : "3" == t[o] ? e += "维修," : "4" == t[o] ? e += "钣喷," : "5" == t[o] ? e += "清洗," : "6" == t[o] && (e += "工时,");
            var r = /,$/gi;
            e = e.replace(r, "");
        }
    } else "1" == t ? e = "通用" : "2" == t ? e = "保养" : "3" == t ? e = "维修" : "4" == t ? e = "钣喷" : "5" == t ? e = "清洗" : "6" == t && (e = "工时");
    return e;
}), e(r, "validationMobile", function(t) {
    return /^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|14[0-9])[0-9]{8}$/.test(t);
}), r);