payStatus.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const { default: api } = require("../../js/api");
  2. Page({
  3. data: {
  4. id: 1 , // 初始ID值
  5. nozzleId:0,
  6. order:null,
  7. // countdown: 120,
  8. progressWidth: 100, // 初始进度条宽度为100%
  9. totalSeconds: 120,// 总秒数
  10. currentSeconds: 120, // 当前剩余秒数
  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. wx.showLoading({
  69. title: '正在取消授权',
  70. })
  71. api.request_CancelNozzleAuthorization(this.data.order.orderId).then(res => {
  72. console.log("取消授权",res)
  73. wx.hideLoading()
  74. if(res.data.statusCode != 200) {
  75. wx.showToast({
  76. title: '取消授权失败,重新取消授权',
  77. })
  78. that.countDown()
  79. that.startCheckNozzle()
  80. return
  81. }
  82. that.setData({
  83. id:5
  84. })
  85. }).catch(err => {
  86. console.log("取消授权报错",err)
  87. wx.hideLoading()
  88. })
  89. },
  90. /** 查看历史订单 */
  91. toHistory() {
  92. setTimeout(() => {
  93. wx.reLaunch({
  94. url: '../historyOrder/historyOrder?paymentMode='+1,
  95. })
  96. },500)
  97. },
  98. /** 授权成功倒计时 */
  99. countDown() {
  100. const that = this;
  101. let timer = setInterval(() => {
  102. let { currentSeconds, totalSeconds } = that.data;
  103. currentSeconds--;
  104. if (currentSeconds < 0) {
  105. that.toUnAuthorization()
  106. that.setData({
  107. currentSeconds: 120,
  108. progressWidth: 100
  109. })
  110. that.clearTimer()
  111. return
  112. }
  113. let aprogressWidth = (currentSeconds / totalSeconds) * 100;
  114. that.setData({
  115. currentSeconds: currentSeconds,
  116. progressWidth: aprogressWidth
  117. });
  118. }, 1000);
  119. that.setData({
  120. countDownTimer:timer
  121. }) // 保存定时器,以便在onUnload中清除
  122. },
  123. /** 轮询检查油枪状态 */
  124. startCheckNozzle(){
  125. const that = this;
  126. const timer = setInterval(() => {
  127. that.checkNozzleInfo()
  128. }, 3000);
  129. that.setData({
  130. checkNozzleStatuesTimer:timer
  131. })
  132. },
  133. /** 检查油枪状态 */
  134. checkNozzleInfo(){
  135. const that = this;
  136. api.request_GetNozzleInfo(this.data.nozzleId).then(res => {
  137. if (res.data.content.length != 0) {
  138. var nozzle = res.data.content[0];
  139. //加油态,转加油中页面
  140. if(nozzle.status == 8) {
  141. that.setData({
  142. id:4
  143. })
  144. that.clearTimer()
  145. }
  146. }
  147. })
  148. },
  149. onUnload() {
  150. this.clearTimer()
  151. },
  152. clearTimer(){
  153. if (this.data.countDownTimer != null) {
  154. clearInterval(this.data.countDownTimer);
  155. this.data.countDownTimer = null;
  156. }
  157. if(this.data.checkNozzleStatuesTimer != null) {
  158. clearInterval(this.data.checkNozzleStatuesTimer)
  159. this.data.checkNozzleStatuesTimer = null;
  160. }
  161. }
  162. });