third-receipt.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. $.extend({
  2. parseQueryString: function (name) {
  3. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  4. var r = window.location.search.substr(1).match(reg);
  5. if (r != null)
  6. return decodeURI(r[2]);
  7. return null;
  8. },
  9. plusString: function (str, frontLen, endLen) {
  10. var len = str.length - frontLen - endLen;
  11. if (len <= 0) {
  12. return str
  13. }
  14. var startStr = '';
  15. for (var i = 0; i < len; i++) {
  16. startStr += '*';
  17. }
  18. return str.substring(0, frontLen) + startStr + str.substring(str.length - endLen);
  19. },
  20. // get_browser: function () {
  21. // var ua = window.navigator.userAgent.toLowerCase();
  22. // if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  23. // return "wechat";
  24. // } else if (ua.match(/Alipay/i) == "alipay") {
  25. // return "alipay";
  26. // } else {
  27. // return "othter"
  28. // }
  29. // }
  30. isTest: function (no5tao) {
  31. if (no5tao) {
  32. return location.port != '8000' && (location.host.indexOf("127.0.0.1") > -1 || location.host.indexOf("localhost") > -1 || location.host.indexOf("192.168.") > -1);
  33. } else {
  34. 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);
  35. }
  36. }
  37. });
  38. var startKey = null;
  39. var logicalCode = null;
  40. var devNo = null;
  41. var homeLink = null;
  42. var detailLink = null;
  43. var serviceLink = null;
  44. var orderType = null;
  45. var orderPayload = null
  46. var outTradeNo = $.parseQueryString("outTradeNo");
  47. if (!outTradeNo) {
  48. outTradeNo = sessionStorage.getItem("receiptOrderNo");
  49. orderType = sessionStorage.getItem("receiptOrderType");
  50. } else {
  51. orderType = $.parseQueryString("orderType");
  52. sessionStorage.setItem("receiptOrderNo", outTradeNo)
  53. sessionStorage.setItem("receiptOrderType", orderType)
  54. }
  55. var getUserStatusTime = 0;
  56. var orderEvent = {
  57. adInfo: null,
  58. endOrderInterval: function () {
  59. getUserStatusTime = 0;
  60. localStorage.removeItem("RECEIPT_START_TIME")
  61. },
  62. endOrderPolling: function () {
  63. getUserStatusTime = 0;
  64. localStorage.removeItem("RECEIPT_START_TIME")
  65. localStorage.removeItem("START_KEY");
  66. // localStorage.removeItem("DEVICE_START_TIME");
  67. },
  68. showBtn: function() {
  69. var that = this
  70. $("#backHome").removeClass("hide")
  71. if (detailLink != null) {
  72. $("#toOrder").removeClass("hide")
  73. } else {
  74. $("#serviceBtn").removeClass("hide")
  75. }
  76. },
  77. noDeviceOrder: function () {
  78. var that = this
  79. $(".order-icon").removeClass("loading")
  80. $(".order-icon").addClass("icon-success")
  81. $(".order-tips").text('充值成功')
  82. that.showAd()
  83. that.showBtn()
  84. },
  85. success: function () {
  86. var that = this
  87. $(".order-icon").removeClass("loading")
  88. $(".order-icon").addClass("icon-success")
  89. $(".order-tips").text('设备启动成功')
  90. that.showAd()
  91. that.showBtn()
  92. if (orderPayload && orderPayload.record && orderPayload.record.isIns) {
  93. $('.order-ins').show()
  94. }
  95. },
  96. start: function () {
  97. var that = this
  98. $(".order-icon").addClass("loading")
  99. $(".order-tips").text('正在获取订单')
  100. that.getReceipt(2000, new Date().getTime())
  101. },
  102. devInStartTips: function () {
  103. $(".order-icon").addClass("loading")
  104. $(".order-tips").text('正在启动设备')
  105. },
  106. fail: function () {
  107. var that = this
  108. $(".order-icon").removeClass("loading")
  109. $(".order-icon").addClass("icon-close")
  110. if (detailLink != null) {
  111. $(".order-tips").text('设备启动失败,请点击[详情]查看订单详细状态。')
  112. } else {
  113. $(".order-tips").text('设备启动失败')
  114. }
  115. that.showBtn()
  116. },
  117. timeover: function () {
  118. var that = this
  119. $(".order-icon").removeClass("loading")
  120. $(".order-icon").addClass("icon-close")
  121. $(".order-tips").text('获取设备启动状态超时')
  122. that.showBtn()
  123. },
  124. retry: function () {
  125. $("#backHome").removeClass("hide")
  126. $("#restart").removeClass("hide")
  127. },
  128. getUserStatusInterval: function (callback, timeout) {
  129. console.log("call getUserStatusInterval")
  130. var that = this
  131. if (timeout == null) {
  132. timeout = 2000;
  133. }
  134. var deviceStartTime = localStorage.getItem("RECEIPT_START_TIME");// 用户上次成功启动的时间,目前设置5分钟过期,过期后不再轮询
  135. let exp = new Date().getTime() - deviceStartTime
  136. // 判断是否超过3分钟
  137. if (exp > 3 * 60 * 1000) {
  138. that.timeover()
  139. that.endOrderPolling()
  140. return;
  141. } else {
  142. setTimeout(function () {
  143. that.devInStartTips()
  144. let url = '/user/getCurrentOrder'
  145. if ($.isTest(true)) {
  146. url = "/mock" + url;
  147. }
  148. $.ajax({
  149. url: url,
  150. type: "GET",
  151. data: {devNo: devNo, startKey: startKey, exp: exp},
  152. dataType: "json",
  153. contentType: "application/json",
  154. success: function (res, status, xhr) {
  155. var obj = res.payload;
  156. orderPayload = obj
  157. if (obj) {
  158. var orderProcessing = obj.orderProcessing;//是否有订单在处理
  159. if (orderProcessing === false) {
  160. if (obj.succeeded === true) {
  161. that.endOrderPolling()
  162. detailLink = obj.record.detailLink
  163. that.success()
  164. } else if (obj.succeeded === false) {
  165. that.endOrderPolling()
  166. if (obj.record) {
  167. detailLink = obj.record.detailLink
  168. }
  169. that.fail()
  170. mui.alert(obj.desc, '温馨提示', '确定');
  171. } else {
  172. that.endOrderInterval();
  173. that.retry()
  174. }
  175. } else if (orderProcessing === true) {
  176. getUserStatusTime++;
  177. that.getUserStatusInterval(callback, 10000);//下一次2000秒间隔轮询
  178. } else if (orderProcessing === null || orderProcessing === undefined) {
  179. that.endOrderInterval();
  180. that.retry()
  181. }
  182. }
  183. }, error: function (xhr, status, error) {
  184. console.log(JSON.stringify(error))
  185. that.endOrderInterval();
  186. that.retry()
  187. }
  188. })
  189. }, timeout);
  190. }
  191. },
  192. getReceipt: function (timeout, startTime) {
  193. console.log("call getPayOrder")
  194. var that = this
  195. let exp = new Date().getTime() - startTime
  196. if (exp > 15 * 1000) {
  197. orderEvent.retry()
  198. } else {
  199. let url = '/common/getReceipt'
  200. if ($.isTest(true)) {
  201. url = "/mock" + url;
  202. }
  203. $.ajax({
  204. url: url,
  205. type: "GET",
  206. data: {
  207. order_type: orderType,
  208. out_trade_no: outTradeNo
  209. },
  210. timeout: 3000,
  211. dataType: "json",
  212. contentType: "application/json",
  213. success: function (data, status, xhr) {
  214. console.log(data)
  215. if (data.result == 1) {
  216. var info = data.payload;
  217. startKey = info.startKey
  218. logicalCode = info.logicalCode
  219. devNo = info.devNo
  220. homeLink = info.homeLink
  221. serviceLink = info.serviceLink
  222. var adShown = localStorage.getItem("THIRD_AD_SHOW");
  223. if ((adShown == null) || (adShown != outTradeNo)) {
  224. that.initAd(info.adInfo)
  225. }
  226. $("#orderNo").text($.plusString(info.orderNo, 8, 8))
  227. if (orderType == "consume") {
  228. $("#p_no_label").text("消费单号")
  229. $("#p_price_label").text("支付方式")
  230. $("#price").text(info.payment)
  231. } else {
  232. $("#price").text('¥' + info.payment)
  233. }
  234. $("#p_name").text(info.itemName)
  235. if (!startKey) {
  236. detailLink = info.detailLink
  237. $("#p_name_label").text("购买商品")
  238. orderEvent.noDeviceOrder()
  239. } else {
  240. $("#p_name_label").text("启动设备")
  241. localStorage.setItem("RECEIPT_START_TIME", new Date().getTime())
  242. orderEvent.getUserStatusInterval()
  243. }
  244. //注册点击事件(去详情)
  245. $("#toOrder").click(
  246. function () {
  247. location.href = detailLink
  248. })
  249. //注册点击事件(去首页)
  250. $("#backHome").click(function () {
  251. location.href = homeLink
  252. })
  253. //注册点击事件(客服页面)
  254. $("#serviceBtn").click(function () {
  255. location.href = serviceLink
  256. })
  257. //保险
  258. $('.order-ins').click(function () {
  259. location.href = '/user/index.html#/insurance/compensate'
  260. })
  261. } else {
  262. setTimeout(function () {
  263. that.getReceipt(timeout, startTime)
  264. }, timeout)
  265. }
  266. },
  267. error: function (xhr, status, error) {
  268. console.log(JSON.stringify(error))
  269. setTimeout(function () {
  270. that.getReceipt(timeout, startTime)
  271. }, timeout)
  272. }
  273. })
  274. }
  275. },
  276. initAded: false,
  277. initAd: function (adInfo) {
  278. var that = this
  279. if (this.initAded) {
  280. console.warn('initAd已经加载过一次')
  281. return;
  282. } else {
  283. this.initAded = true
  284. }
  285. that.adInfo = adInfo
  286. if ((that.adInfo.adShow != 'show') || (that.adInfo.showType == null) || (that.adInfo.showType == "")) {
  287. _czc.push(["_trackEvent", "ad", "adType", "noshow", 1]);
  288. return
  289. }
  290. if (that.adInfo.showType == "float") {
  291. that.initFloatAd()
  292. } else if (that.adInfo.showType == "floatRedpack") {
  293. that.initFloatRedpack()
  294. } else if (that.adInfo.showType == "jump") {
  295. that.initJumpAd()
  296. } else if (that.adInfo.showType == "banner") {
  297. _czc.push(["_trackEvent", "ad", "adType", "banner", 1]);
  298. that.showBannerAd()
  299. } else if (that.adInfo.showType == "floatJump") {
  300. that.initFloatJumpAd()
  301. }
  302. },
  303. showAded: false,
  304. showAd: function () {
  305. var that = this
  306. if (this.showAded) {
  307. console.warn('showAd已经加载过一次')
  308. return;
  309. } else {
  310. this.showAded = true
  311. }
  312. if (that.adInfo.adShow != "show") {
  313. return
  314. }
  315. if (that.adInfo.showType == "float") {
  316. _czc.push(["_trackEvent", "ad", "adType", "float", 1]);
  317. that.showFloatAd()
  318. } else if (that.adInfo.showType == "floatRedpack") {
  319. $("#floatRedpack").addClass("fade")
  320. } else if (that.adInfo.showType == "jump") {
  321. that.jumpToAd()
  322. } else if (that.adInfo.showType == "banner") {
  323. } else if (that.adInfo.showType == "floatJump") {
  324. that.showFloatJumpAd()
  325. }
  326. },
  327. showBannerAd: function () {
  328. var that = this
  329. $(".banner-ad").attr("href", that.adInfo.url)
  330. $(".banner-ad img").attr("src", that.adInfo.img)
  331. $(".banner-ad").addClass("fade")
  332. },
  333. initJumpAd: function () {
  334. var that = this
  335. $("#jumpAdTips .ad-dialog-desc").html(that.adInfo.title)
  336. },
  337. jumpToAd: function () {
  338. var that = this
  339. if (that.adInfo.url == null || that.adInfo.url == "") {
  340. return
  341. }
  342. $("#jumpAdTips").addClass("fade")
  343. let time = 200
  344. let inter = setInterval(function () {
  345. $("#jumpAdTips .progress-done").addClass("max-w")
  346. time = time - 100
  347. if (time <= 0) {
  348. clearInterval(inter)
  349. inter = null
  350. _czc.push(["_trackEvent", "ad", "adType", "jump", 1]);
  351. localStorage.setItem("THIRD_AD_SHOW", outTradeNo)
  352. window.location.href = that.adInfo.url
  353. }
  354. }, 100)
  355. },
  356. clickClose: function (event) {
  357. var that = this
  358. event.preventDefault()
  359. event.stopPropagation()
  360. that.hideFloatAd()
  361. },
  362. clickCloseRedpack: function (event) {
  363. event.preventDefault()
  364. event.stopPropagation()
  365. $("#floatRedpack").removeClass("fade")
  366. },
  367. initFloatAd: function () {
  368. var that = this
  369. $("#floatAd .ad-dialog-link").attr("href", that.adInfo.url)
  370. if (that.adInfo.title != null && that.adInfo.title != "") {
  371. $("#floatAd .ad-dialog-desc").text(that.adInfo.title)
  372. } else {
  373. $("#floatAd .ad-dialog-desc").addClass("hide")
  374. }
  375. $("#floatAd .ad-dialog-img").attr("src", that.adInfo.img)
  376. $("#floatAd .mask-close").click(function (event) {
  377. that.clickClose(event)
  378. })
  379. },
  380. initFloatRedpack: function () {
  381. var that = this
  382. $("#floatRedpack .ad-dialog-link").attr("href", that.adInfo.url)
  383. if (that.adInfo.title != null && that.adInfo.title != "") {
  384. $("#floatRedpack .ad-dialog-desc").text(that.adInfo.title)
  385. } else {
  386. $("#floatRedpack .ad-dialog-desc").addClass("hide")
  387. }
  388. $("#floatRedpack .ad-dialog-img").attr("src", that.adInfo.img)
  389. $("#floatRedpack .mask-close").click(function (event) {
  390. that.clickCloseRedpack(event)
  391. })
  392. },
  393. showFloatAd: function () {
  394. $("#floatAd").addClass("fade")
  395. },
  396. hideFloatAd: function () {
  397. $("#floatAd").removeClass("fade")
  398. },
  399. initFloatJumpAd: function () {
  400. var that = this
  401. $("#floatJumpAd .ad-dialog-img").attr("src", that.adInfo.img)
  402. },
  403. showFloatJumpAd: function () {
  404. var that = this
  405. if (that.adInfo.url == null || that.adInfo.url == "") {
  406. return
  407. }
  408. $("#floatJumpAd").addClass("fade")
  409. setTimeout(function () {
  410. console.log(that.adInfo.url)
  411. _czc.push(["_trackEvent", "ad", "adType", "floatJump", 1]);
  412. localStorage.setItem("THIRD_AD_SHOW", outTradeNo)
  413. window.location.href = that.adInfo.url
  414. }, 1500)
  415. }
  416. }
  417. orderEvent.start()