receipt.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var getUserStatusTime = 0;
  2. var orderPayload = null;
  3. var orderEvent = {
  4. endOrderInterval: function () {
  5. getUserStatusTime = 0;
  6. localStorage.removeItem("RECEIPT_START_TIME")
  7. localStorage.removeItem("START_KEY");
  8. // localStorage.removeItem("DEVICE_START_TIME");
  9. },
  10. noDeviceOrder: function () {
  11. // 非启动设备的订单,无需等待。显示回到主页
  12. $('.order-status').remove()
  13. if (detailLink != null) {
  14. $('#toOrder').removeClass('hide')
  15. }
  16. },
  17. success: function () {
  18. $('.order-icon').removeClass('loading').addClass('iconfont icon-success')
  19. $('.order-tips').text('设备启动成功')
  20. if (detailLink != null) {
  21. $('#toOrder').removeClass('hide')
  22. }
  23. if (orderPayload && orderPayload.record && orderPayload.record.isIns) {
  24. $('.order-ins').show()
  25. }
  26. },
  27. orderLoading: function () {
  28. $('.order-icon').addClass('loading')
  29. $('.order-tips').text('正在获取订单')
  30. },
  31. devInStartTips: function () {
  32. $('.order-icon').addClass('loading')
  33. $('.order-tips').text('正在启动设备')
  34. },
  35. fail: function () {
  36. $('.order-icon').removeClass('loading').addClass('iconfont icon-close-dark')
  37. if (detailLink != null) {
  38. $('.order-tips').text('设备启动失败,请点击[详情]查看订单详细状态。')
  39. $('#toOrder').removeClass('hide')
  40. } else {
  41. $('.order-tips').text('设备启动失败')
  42. $('#serviceBtn').removeClass('hide')
  43. }
  44. },
  45. timeover: function () {
  46. $('.order-icon').removeClass('loading').addClass('iconfont icon-close-dark')
  47. $('.order-tips').text('获取设备启动状态超时')
  48. $('#serviceBtn').removeClass('hide')
  49. },
  50. retry: function () {
  51. $('#restart').removeClass('hide')
  52. },
  53. getUserStatusInterval: function (callback, timeout) {
  54. console.log("call getUserStatusInterval")
  55. var that = this
  56. if (timeout == null) {
  57. timeout = 2000;
  58. }
  59. var deviceStartTime = localStorage.getItem("RECEIPT_START_TIME");// 用户上次成功启动的时间,目前设置5分钟过期,过期后不再轮询
  60. let exp = new Date().getTime() - deviceStartTime
  61. // 判断是否超过3分钟
  62. if (exp > 3 * 60 * 1000) {
  63. that.timeover()
  64. that.endOrderInterval()
  65. return;
  66. } else {
  67. setTimeout(function () {
  68. that.devInStartTips()
  69. sendRequest({
  70. url: '/user/getCurrentOrder',
  71. type: "GET",
  72. data: {devNo: devNo, startKey: startKey, exp: exp},
  73. contentType: "application/json",
  74. success: function (res) {
  75. var obj = res.payload;
  76. orderPayload = obj
  77. if (obj) {
  78. var orderProcessing = obj.orderProcessing;//是否有订单在处理
  79. var desc = obj.desc;
  80. if (orderProcessing === false) {
  81. that.endOrderInterval()
  82. // 如果订单成功,则提示用户:对订单是否还有疑问,失败则直接提示desc即可
  83. if (obj.succeeded === true) {
  84. detailLink = obj.record.detailLink
  85. that.success()
  86. } else if (obj.succeeded === false) {
  87. if(obj.record) {
  88. detailLink = obj.record.detailLink
  89. }
  90. that.fail()
  91. mui.alert(desc, '温馨提示', '确定');
  92. } else {
  93. that.fail()
  94. mui.alert('订单的succeeded状态为:' + obj.succeeded, '温馨提示', '确定');
  95. }
  96. } else if (orderProcessing === true) {
  97. getUserStatusTime++;
  98. that.getUserStatusInterval(callback, 10000);//下一次2000秒间隔轮询
  99. } else if (orderProcessing === null || orderProcessing === undefined) {
  100. mui.alert('未获取到上次订单信息')
  101. that.endOrderInterval();
  102. that.fail()
  103. }
  104. }
  105. }
  106. })
  107. }, timeout);
  108. }
  109. }
  110. }