AuthorizationTransactionPage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const {
  2. default: api
  3. } = require("../../js/api");
  4. const util = require('../../utils/util.js')
  5. // pages/orderConfirm/orderConfirm.js
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. nozzleId: 0,
  12. type: '预支付',
  13. order: null
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad(options) {
  19. this.setData({
  20. nozzleId:options.nozzleId
  21. })
  22. const that = this;
  23. const eventChannel = this.getOpenerEventChannel();
  24. eventChannel.on('acceptDataFromQuantify', function (data) {
  25. console.log("获取到下单页的订单",data)
  26. that.setData({
  27. order:data
  28. })
  29. });
  30. },
  31. /**
  32. * 生命周期函数--监听页面初次渲染完成
  33. */
  34. onReady() {
  35. },
  36. /**
  37. * 生命周期函数--监听页面显示
  38. */
  39. onShow() {
  40. },
  41. /**
  42. * 生命周期函数--监听页面隐藏
  43. */
  44. onHide() {
  45. },
  46. /**
  47. * 生命周期函数--监听页面卸载
  48. */
  49. onUnload() {
  50. },
  51. /**
  52. * 页面相关事件处理函数--监听用户下拉动作
  53. */
  54. onPullDownRefresh() {
  55. },
  56. /**
  57. * 页面上拉触底事件的处理函数
  58. */
  59. onReachBottom() {
  60. },
  61. /**
  62. * 用户点击右上角分享
  63. */
  64. onShareAppMessage() {
  65. },
  66. /** 支付按钮点击事件 */
  67. toPay() {
  68. this.subMessage()
  69. // var that = this;
  70. // api.request_wechatPay(this.data.order.orderId).then(res => {
  71. // console.log("支付结果", res)
  72. // that.subMessage()
  73. // // this.pay()
  74. // }).catch(err => {
  75. // console.log("支付失败", err)
  76. // })
  77. },
  78. /** 订阅消息模板 */
  79. subMessage() {
  80. const that = this;
  81. wx.requestSubscribeMessage({
  82. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  83. success(res) {
  84. that.pay()
  85. // 'accept'表示用户同意订阅该条id对应的模板消息
  86. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  87. // 用户同意订阅,调用云函数或服务器接口发送订阅消息
  88. // wx.cloud.callFunction({
  89. // name: 'sendSubscribeMessage',
  90. // data: {
  91. // templateId: '配置好的模板ID',
  92. // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA',
  93. // data: {
  94. // thing1: {
  95. // value: '活动名称'
  96. // },
  97. // // 其他参数...
  98. // }
  99. // },
  100. // success(res) {
  101. // console.log('订阅消息发送成功', res)
  102. // },
  103. // fail(err) {
  104. // console.error('订阅消息发送失败', err)
  105. // }
  106. // })
  107. }
  108. },
  109. fail(err) {
  110. that.pay()
  111. }
  112. })
  113. },
  114. pay() {
  115. const that = this;
  116. api.request_wechatPay(this.data.order.orderId)
  117. .then(res => {
  118. wx.requestPayment({
  119. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  120. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  121. package: res.data.data.unifiedOrderResult.package,
  122. signType: res.data.data.unifiedOrderResult.signType,
  123. paySign: res.data.data.unifiedOrderResult.paySign,
  124. success: res => {
  125. wx.hideLoading()
  126. that.sendMessage()
  127. that.toPayResult(1)
  128. },
  129. fail: res => {
  130. wx.hideLoading()
  131. that.toPayResult(2)
  132. }
  133. })
  134. });
  135. },
  136. /** 发送消息模板 */
  137. sendMessage() {
  138. const message = {
  139. trxid: this.data.order.orderId,
  140. orderType: '支付'
  141. }
  142. api.request_sendMessage(message).then(res => {
  143. console.log("发送消息模板结果", res)
  144. }).catch(err => {
  145. console.log("发送消息模板失败", err)
  146. })
  147. },
  148. /** 跳到支付结果页面 */
  149. toPayResult(type){
  150. const that = this;
  151. setTimeout(() => {
  152. wx.navigateTo({
  153. url: '../payStatus/payStatus?id='+type+'&nozzleId='+that.data.nozzleId,
  154. success:function(res){
  155. res.eventChannel.emit('acceptDataFromAuthorization',that.data.order)
  156. }
  157. })
  158. },500)
  159. }
  160. })