$.extend({ parseQueryString: function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }, plusString: function (str, frontLen, endLen) { var len = str.length - frontLen - endLen; if (len <= 0) { return str } var startStr = ''; for (var i = 0; i < len; i++) { startStr += '*'; } return str.substring(0, frontLen) + startStr + str.substring(str.length - endLen); }, // get_browser: function () { // var ua = window.navigator.userAgent.toLowerCase(); // if (ua.match(/MicroMessenger/i) == 'micromessenger') { // return "wechat"; // } else if (ua.match(/Alipay/i) == "alipay") { // return "alipay"; // } else { // return "othter" // } // } isTest: function (no5tao) { if (no5tao) { return location.port != '8000' && (location.host.indexOf("127.0.0.1") > -1 || location.host.indexOf("localhost") > -1 || location.host.indexOf("192.168.") > -1); } else { return location.port != '8000' && (location.host.indexOf("127.0.0.1") > -1 || location.host.indexOf("localhost") > -1 || location.host.indexOf("192.168.") > -1 || location.host.indexOf("5tao5ai.com") > -1); } } }); var startKey = null; var logicalCode = null; var devNo = null; var homeLink = null; var detailLink = null; var serviceLink = null; var orderType = null; var orderPayload = null var outTradeNo = $.parseQueryString("outTradeNo"); if (!outTradeNo) { outTradeNo = sessionStorage.getItem("receiptOrderNo"); orderType = sessionStorage.getItem("receiptOrderType"); } else { orderType = $.parseQueryString("orderType"); sessionStorage.setItem("receiptOrderNo", outTradeNo) sessionStorage.setItem("receiptOrderType", orderType) } var getUserStatusTime = 0; var orderEvent = { adInfo: null, endOrderInterval: function () { getUserStatusTime = 0; localStorage.removeItem("RECEIPT_START_TIME") }, endOrderPolling: function () { getUserStatusTime = 0; localStorage.removeItem("RECEIPT_START_TIME") localStorage.removeItem("START_KEY"); // localStorage.removeItem("DEVICE_START_TIME"); }, showBtn: function() { var that = this $("#backHome").removeClass("hide") if (detailLink != null) { $("#toOrder").removeClass("hide") } else { $("#serviceBtn").removeClass("hide") } }, noDeviceOrder: function () { var that = this $(".order-icon").removeClass("loading") $(".order-icon").addClass("icon-success") $(".order-tips").text('充值成功') that.showAd() that.showBtn() }, success: function () { var that = this $(".order-icon").removeClass("loading") $(".order-icon").addClass("icon-success") $(".order-tips").text('设备启动成功') that.showAd() that.showBtn() if (orderPayload && orderPayload.record && orderPayload.record.isIns) { $('.order-ins').show() } }, start: function () { var that = this $(".order-icon").addClass("loading") $(".order-tips").text('正在获取订单') that.getReceipt(2000, new Date().getTime()) }, devInStartTips: function () { $(".order-icon").addClass("loading") $(".order-tips").text('正在启动设备') }, fail: function () { var that = this $(".order-icon").removeClass("loading") $(".order-icon").addClass("icon-close") if (detailLink != null) { $(".order-tips").text('设备启动失败,请点击[详情]查看订单详细状态。') } else { $(".order-tips").text('设备启动失败') } that.showBtn() }, timeover: function () { var that = this $(".order-icon").removeClass("loading") $(".order-icon").addClass("icon-close") $(".order-tips").text('获取设备启动状态超时') that.showBtn() }, retry: function () { $("#backHome").removeClass("hide") $("#restart").removeClass("hide") }, getUserStatusInterval: function (callback, timeout) { console.log("call getUserStatusInterval") var that = this if (timeout == null) { timeout = 2000; } var deviceStartTime = localStorage.getItem("RECEIPT_START_TIME");// 用户上次成功启动的时间,目前设置5分钟过期,过期后不再轮询 let exp = new Date().getTime() - deviceStartTime // 判断是否超过3分钟 if (exp > 3 * 60 * 1000) { that.timeover() that.endOrderPolling() return; } else { setTimeout(function () { that.devInStartTips() let url = '/user/getCurrentOrder' if ($.isTest(true)) { url = "/mock" + url; } $.ajax({ url: url, type: "GET", data: {devNo: devNo, startKey: startKey, exp: exp}, dataType: "json", contentType: "application/json", success: function (res, status, xhr) { var obj = res.payload; orderPayload = obj if (obj) { var orderProcessing = obj.orderProcessing;//是否有订单在处理 if (orderProcessing === false) { if (obj.succeeded === true) { that.endOrderPolling() detailLink = obj.record.detailLink that.success() } else if (obj.succeeded === false) { that.endOrderPolling() if (obj.record) { detailLink = obj.record.detailLink } that.fail() mui.alert(obj.desc, '温馨提示', '确定'); } else { that.endOrderInterval(); that.retry() } } else if (orderProcessing === true) { getUserStatusTime++; that.getUserStatusInterval(callback, 10000);//下一次2000秒间隔轮询 } else if (orderProcessing === null || orderProcessing === undefined) { that.endOrderInterval(); that.retry() } } }, error: function (xhr, status, error) { console.log(JSON.stringify(error)) that.endOrderInterval(); that.retry() } }) }, timeout); } }, getReceipt: function (timeout, startTime) { console.log("call getPayOrder") var that = this let exp = new Date().getTime() - startTime if (exp > 15 * 1000) { orderEvent.retry() } else { let url = '/common/getReceipt' if ($.isTest(true)) { url = "/mock" + url; } $.ajax({ url: url, type: "GET", data: { order_type: orderType, out_trade_no: outTradeNo }, timeout: 3000, dataType: "json", contentType: "application/json", success: function (data, status, xhr) { console.log(data) if (data.result == 1) { var info = data.payload; startKey = info.startKey logicalCode = info.logicalCode devNo = info.devNo homeLink = info.homeLink serviceLink = info.serviceLink var adShown = localStorage.getItem("THIRD_AD_SHOW"); if ((adShown == null) || (adShown != outTradeNo)) { that.initAd(info.adInfo) } $("#orderNo").text($.plusString(info.orderNo, 8, 8)) if (orderType == "consume") { $("#p_no_label").text("消费单号") $("#p_price_label").text("支付方式") $("#price").text(info.payment) } else { $("#price").text('¥' + info.payment) } $("#p_name").text(info.itemName) if (!startKey) { detailLink = info.detailLink $("#p_name_label").text("购买商品") orderEvent.noDeviceOrder() } else { $("#p_name_label").text("启动设备") localStorage.setItem("RECEIPT_START_TIME", new Date().getTime()) orderEvent.getUserStatusInterval() } //注册点击事件(去详情) $("#toOrder").click( function () { location.href = detailLink }) //注册点击事件(去首页) $("#backHome").click(function () { location.href = homeLink }) //注册点击事件(客服页面) $("#serviceBtn").click(function () { location.href = serviceLink }) //保险 $('.order-ins').click(function () { location.href = '/user/index.html#/insurance/compensate' }) } else { setTimeout(function () { that.getReceipt(timeout, startTime) }, timeout) } }, error: function (xhr, status, error) { console.log(JSON.stringify(error)) setTimeout(function () { that.getReceipt(timeout, startTime) }, timeout) } }) } }, initAded: false, initAd: function (adInfo) { var that = this if (this.initAded) { console.warn('initAd已经加载过一次') return; } else { this.initAded = true } that.adInfo = adInfo if ((that.adInfo.adShow != 'show') || (that.adInfo.showType == null) || (that.adInfo.showType == "")) { _czc.push(["_trackEvent", "ad", "adType", "noshow", 1]); return } if (that.adInfo.showType == "float") { that.initFloatAd() } else if (that.adInfo.showType == "floatRedpack") { that.initFloatRedpack() } else if (that.adInfo.showType == "jump") { that.initJumpAd() } else if (that.adInfo.showType == "banner") { _czc.push(["_trackEvent", "ad", "adType", "banner", 1]); that.showBannerAd() } else if (that.adInfo.showType == "floatJump") { that.initFloatJumpAd() } }, showAded: false, showAd: function () { var that = this if (this.showAded) { console.warn('showAd已经加载过一次') return; } else { this.showAded = true } if (that.adInfo.adShow != "show") { return } if (that.adInfo.showType == "float") { _czc.push(["_trackEvent", "ad", "adType", "float", 1]); that.showFloatAd() } else if (that.adInfo.showType == "floatRedpack") { $("#floatRedpack").addClass("fade") } else if (that.adInfo.showType == "jump") { that.jumpToAd() } else if (that.adInfo.showType == "banner") { } else if (that.adInfo.showType == "floatJump") { that.showFloatJumpAd() } }, showBannerAd: function () { var that = this $(".banner-ad").attr("href", that.adInfo.url) $(".banner-ad img").attr("src", that.adInfo.img) $(".banner-ad").addClass("fade") }, initJumpAd: function () { var that = this $("#jumpAdTips .ad-dialog-desc").html(that.adInfo.title) }, jumpToAd: function () { var that = this if (that.adInfo.url == null || that.adInfo.url == "") { return } $("#jumpAdTips").addClass("fade") let time = 200 let inter = setInterval(function () { $("#jumpAdTips .progress-done").addClass("max-w") time = time - 100 if (time <= 0) { clearInterval(inter) inter = null _czc.push(["_trackEvent", "ad", "adType", "jump", 1]); localStorage.setItem("THIRD_AD_SHOW", outTradeNo) window.location.href = that.adInfo.url } }, 100) }, clickClose: function (event) { var that = this event.preventDefault() event.stopPropagation() that.hideFloatAd() }, clickCloseRedpack: function (event) { event.preventDefault() event.stopPropagation() $("#floatRedpack").removeClass("fade") }, initFloatAd: function () { var that = this $("#floatAd .ad-dialog-link").attr("href", that.adInfo.url) if (that.adInfo.title != null && that.adInfo.title != "") { $("#floatAd .ad-dialog-desc").text(that.adInfo.title) } else { $("#floatAd .ad-dialog-desc").addClass("hide") } $("#floatAd .ad-dialog-img").attr("src", that.adInfo.img) $("#floatAd .mask-close").click(function (event) { that.clickClose(event) }) }, initFloatRedpack: function () { var that = this $("#floatRedpack .ad-dialog-link").attr("href", that.adInfo.url) if (that.adInfo.title != null && that.adInfo.title != "") { $("#floatRedpack .ad-dialog-desc").text(that.adInfo.title) } else { $("#floatRedpack .ad-dialog-desc").addClass("hide") } $("#floatRedpack .ad-dialog-img").attr("src", that.adInfo.img) $("#floatRedpack .mask-close").click(function (event) { that.clickCloseRedpack(event) }) }, showFloatAd: function () { $("#floatAd").addClass("fade") }, hideFloatAd: function () { $("#floatAd").removeClass("fade") }, initFloatJumpAd: function () { var that = this $("#floatJumpAd .ad-dialog-img").attr("src", that.adInfo.img) }, showFloatJumpAd: function () { var that = this if (that.adInfo.url == null || that.adInfo.url == "") { return } $("#floatJumpAd").addClass("fade") setTimeout(function () { console.log(that.adInfo.url) _czc.push(["_trackEvent", "ad", "adType", "floatJump", 1]); localStorage.setItem("THIRD_AD_SHOW", outTradeNo) window.location.href = that.adInfo.url }, 1500) } } orderEvent.start()