123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- $.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()
|