AuthorizationTransactionPage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. var that = this;
  69. api.request_wechatPay(this.data.order.orderId).then(res => {
  70. console.log("支付结果", res)
  71. that.subMessage()
  72. // this.pay()
  73. }).catch(err => {
  74. console.log("支付失败", err)
  75. })
  76. },
  77. /** 订阅消息模板 */
  78. subMessage() {
  79. const that = this;
  80. wx.requestSubscribeMessage({
  81. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  82. success(res) {
  83. that.pay()
  84. // 'accept'表示用户同意订阅该条id对应的模板消息
  85. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  86. // 用户同意订阅,调用云函数或服务器接口发送订阅消息
  87. // wx.cloud.callFunction({
  88. // name: 'sendSubscribeMessage',
  89. // data: {
  90. // templateId: '配置好的模板ID',
  91. // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA',
  92. // data: {
  93. // thing1: {
  94. // value: '活动名称'
  95. // },
  96. // // 其他参数...
  97. // }
  98. // },
  99. // success(res) {
  100. // console.log('订阅消息发送成功', res)
  101. // },
  102. // fail(err) {
  103. // console.error('订阅消息发送失败', err)
  104. // }
  105. // })
  106. }
  107. },
  108. fail(err) {
  109. that.pay()
  110. }
  111. })
  112. },
  113. pay() {
  114. const that = this;
  115. api.request_wechatPay(this.data.order.orderId)
  116. .then(res => {
  117. wx.requestPayment({
  118. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  119. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  120. package: res.data.data.unifiedOrderResult.package,
  121. signType: res.data.data.unifiedOrderResult.signType,
  122. paySign: res.data.data.unifiedOrderResult.paySign,
  123. success: res => {
  124. wx.hideLoading()
  125. that.sendMessage()
  126. that.toPayResult(1)
  127. },
  128. fail: res => {
  129. wx.hideLoading()
  130. that.toPayResult(2)
  131. }
  132. })
  133. });
  134. },
  135. /** 发送消息模板 */
  136. sendMessage() {
  137. const message = {
  138. trxid: this.data.order.orderId,
  139. orderType: '支付'
  140. }
  141. api.request_sendMessage(message).then(res => {
  142. console.log("发送消息模板结果", res)
  143. }).catch(err => {
  144. console.log("发送消息模板失败", err)
  145. })
  146. },
  147. /** 跳到支付结果页面 */
  148. toPayResult(type){
  149. const that = this;
  150. setTimeout(() => {
  151. wx.navigateTo({
  152. url: '../payStatus/payStatus?id='+type+'&nozzleId='+that.data.nozzleId,
  153. success:function(res){
  154. res.eventChannel.emit('acceptDataFromAuthorization',that.data.order)
  155. }
  156. })
  157. },500)
  158. }
  159. })