payStatus.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const { default: api } = require("../../js/api");
  2. Page({
  3. data: {
  4. id: 4 , // 初始ID值
  5. nozzleId:0,
  6. order:null,
  7. countdown: 120,
  8. progressWidth: 100, // 初始进度条宽度为100%
  9. totalSeconds: 60, // 总秒数
  10. currentSeconds: 60, // 当前剩余秒数
  11. countDownTimer:null, //倒计时定时器
  12. checkNozzleStatuesTimer:null, //检查油枪状态定时器
  13. },
  14. onLoad(options) {
  15. console.log("收到预支付确定页信息",options)
  16. this.setData({
  17. id:options.id,
  18. nozzleId:options.nozzleId
  19. })
  20. const that = this;
  21. const eventChannel = this.getOpenerEventChannel();
  22. eventChannel.on('acceptDataFromAuthorization', function (data) {
  23. console.log(data)
  24. that.setData({
  25. order:data
  26. })
  27. //授权
  28. if(options.id == 1) {
  29. that.toAuthorization();
  30. }
  31. });
  32. },
  33. /** 返回重新支付 */
  34. back(){
  35. setTimeout(() => {
  36. wx.navigateBack()
  37. },500)
  38. },
  39. /** 授权 */
  40. toAuthorization() {
  41. this.setData({
  42. id:1
  43. })
  44. const that = this;
  45. api.request_NozzleAuthorization(this.data.order.orderId).then(res => {
  46. console.log("授权",res)
  47. if(res.data.statusCode != 200) {
  48. that.setData({
  49. id:5
  50. })
  51. return
  52. }
  53. that.setData({
  54. id:3
  55. })
  56. that.countDown()
  57. that.startCheckNozzle()
  58. }).catch(err => {
  59. console.log("授权报错",err)
  60. that.setData({
  61. id:5
  62. })
  63. })
  64. },
  65. /** 取消授权 */
  66. toUnAuthorization(){
  67. const that = this;
  68. api.request_CancelNozzleAuthorization(this.data.order.orderId).then(res => {
  69. console.log("取消授权",res)
  70. if(res.data.statusCode != 200) {
  71. that.countDown()
  72. that.startCheckNozzle()
  73. return
  74. }
  75. that.setData({
  76. id:5
  77. })
  78. })
  79. },
  80. /** 查看历史订单 */
  81. toHistory() {
  82. setTimeout(() => {
  83. wx.reLaunch({
  84. url: '../historyOrder/historyOrder?paymentMode='+1,
  85. })
  86. },500)
  87. },
  88. /** 授权成功倒计时 */
  89. countDown() {
  90. const that = this;
  91. let timer = setInterval(() => {
  92. let { currentSeconds, totalSeconds } = that.data;
  93. currentSeconds--;
  94. if (currentSeconds < 0) {
  95. that.toUnAuthorization()
  96. that.setData({
  97. currentSeconds: 60,
  98. progressWidth: 100
  99. })
  100. that.clearTimer()
  101. return
  102. }
  103. let aprogressWidth = (currentSeconds / totalSeconds) * 100;
  104. that.setData({
  105. currentSeconds: currentSeconds,
  106. progressWidth: aprogressWidth
  107. });
  108. }, 1000);
  109. that.setData({
  110. countDownTimer:timer
  111. }) // 保存定时器,以便在onUnload中清除
  112. },
  113. /** 轮询检查油枪状态 */
  114. startCheckNozzle(){
  115. const that = this;
  116. const timer = setInterval(() => {
  117. that.checkNozzleInfo()
  118. }, 3000);
  119. that.setData({
  120. checkNozzleStatuesTimer:timer
  121. })
  122. },
  123. /** 检查油枪状态 */
  124. checkNozzleInfo(){
  125. const that = this;
  126. api.request_GetNozzleInfo(this.data.nozzleId).then(res => {
  127. if (res.data.content.length != 0) {
  128. var nozzle = res.data.content[0];
  129. //加油态,转加油中页面
  130. if(nozzle.status == 8) {
  131. that.setData({
  132. id:4
  133. })
  134. that.clearTimer()
  135. }
  136. }
  137. })
  138. },
  139. onUnload() {
  140. this.clearTimer()
  141. },
  142. clearTimer(){
  143. if (this.data.countDownTimer != null) {
  144. clearInterval(this.data.countDownTimer);
  145. this.data.countDownTimer = null;
  146. }
  147. if(this.data.checkNozzleStatuesTimer != null) {
  148. clearInterval(this.data.checkNozzleStatuesTimer)
  149. this.data.checkNozzleStatuesTimer = null;
  150. }
  151. }
  152. });