TransactionPage.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. oprationBtn1: '',
  15. oprationBtn2: '对此订单有疑问?',
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad(options) {
  21. const link = decodeURIComponent(options.q) // 获取到二维码原始链接
  22. var id = link.split('wxapp?id=')[1];
  23. if (id == undefined) {
  24. id = options.id;
  25. }
  26. this.setData({
  27. nozzleId: id
  28. })
  29. this.getOrder()
  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. getOrder() {
  68. api.request_GetMiniProgramTransactionsUnpaidNozzle(Number(this.data.nozzleId))
  69. .then(res => {
  70. if (res.data.statusCode == 203) {
  71. //若为203,证明还未登录,跳转到登录页,这里可能刚从主页跳转过来,频繁的跳转可能会跳转页面超时,故而加上延时
  72. setTimeout(() => {
  73. wx.navigateTo({
  74. url: '../login/login'
  75. })
  76. }, 500)
  77. return
  78. }
  79. console.log("获取到未支付订单", res)
  80. var order = res.data.data[0];
  81. var time = util.formatDateNotSecond(order.fuelItemTransactionEndTime);
  82. this.setData({
  83. order: {
  84. oilName: order.productName,
  85. nozzle: order.nozzleId,
  86. volume: order.originalQty,
  87. amount: order.originalAmount,
  88. payAmount: order.actualPaymentAmount,
  89. discount: order.originalAmount - order.actualPaymentAmount,
  90. orderId: order.id,
  91. time: time
  92. }
  93. })
  94. }).catch(err => {
  95. console.log("未获取到未支付订单")
  96. })
  97. },
  98. /** 后支付查看更多交易 */
  99. toMordOrderPage() {
  100. var that = this;
  101. wx.navigateTo({
  102. url: '../moreOrder/moreOrder?nozzle=' + this.data.nozzleId,
  103. events: {
  104. acceptOrderFromMoreOrder: function (data) {
  105. console.log("获取到传送过来的订单", data)
  106. that.setData({
  107. order: data
  108. })
  109. }
  110. }
  111. })
  112. },
  113. /** 支付按钮点击事件 */
  114. toPay() {
  115. var that = this;
  116. api.request_wechatPay(this.data.order.orderId).then(res => {
  117. console.log("支付结果", res)
  118. this.subMessage()
  119. // this.pay()
  120. }).catch(err => {
  121. console.log("支付失败", err)
  122. })
  123. },
  124. /** 订阅消息模板 */
  125. subMessage() {
  126. const that = this;
  127. wx.requestSubscribeMessage({
  128. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  129. success(res) {
  130. that.pay()
  131. // 'accept'表示用户同意订阅该条id对应的模板消息
  132. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  133. // 用户同意订阅,调用云函数或服务器接口发送订阅消息
  134. // wx.cloud.callFunction({
  135. // name: 'sendSubscribeMessage',
  136. // data: {
  137. // templateId: '配置好的模板ID',
  138. // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA',
  139. // data: {
  140. // thing1: {
  141. // value: '活动名称'
  142. // },
  143. // // 其他参数...
  144. // }
  145. // },
  146. // success(res) {
  147. // console.log('订阅消息发送成功', res)
  148. // },
  149. // fail(err) {
  150. // console.error('订阅消息发送失败', err)
  151. // }
  152. // })
  153. }
  154. },
  155. fail(err) {
  156. that.pay()
  157. }
  158. })
  159. },
  160. pay() {
  161. const that = this;
  162. api.request_wechatPay(this.data.order.orderId)
  163. .then(res => {
  164. wx.requestPayment({
  165. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  166. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  167. package: res.data.data.unifiedOrderResult.package,
  168. signType: res.data.data.unifiedOrderResult.signType,
  169. paySign: res.data.data.unifiedOrderResult.paySign,
  170. success: res => {
  171. wx.hideLoading()
  172. that.sendMessage()
  173. that.toPayResult()
  174. },
  175. fail: res => {
  176. wx.hideLoading()
  177. wx.showModal({
  178. title: '支付失败',
  179. content: res.errMsg + '请稍后再试',
  180. showCancel: false,
  181. success(res) {
  182. }
  183. })
  184. }
  185. })
  186. });
  187. },
  188. /** 发送消息模板 */
  189. sendMessage() {
  190. const message = {
  191. trxid:this.data.order.orderId,
  192. orderType:'支付'
  193. }
  194. api.request_sendMessage(message).then(res => {
  195. console.log("发送消息模板结果",res)
  196. }).catch(err => {
  197. console.log("发送消息模板失败",err)
  198. })
  199. },
  200. /** 跳转到支付结果页 */
  201. toPayResult(){
  202. wx.redirectTo({
  203. url: '../payResult/payResult',
  204. })
  205. },
  206. /** 跳转到支付结果页(不用这个) */
  207. toPayResult_back(){
  208. const that = this;
  209. setTimeout(function () {
  210. wx.navigateTo({
  211. url: '../payResult/payResult?nozzle=' + that.data.nozzleId,
  212. events: {
  213. acceptOrderFromPayResult: function (data) {
  214. console.log("获取到来自result页面传送过来的订单", data)
  215. that.setData({
  216. order: data
  217. })
  218. }
  219. }
  220. })
  221. }, 500)
  222. }
  223. })