cashier.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // pages/my/cashier/cashier.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. order_no: "",
  9. pageinfo: {},
  10. price: 0,
  11. give_price: 0,
  12. is_online: 1,
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. var that = this;
  19. this.setData({
  20. order_no: options.order_no,
  21. is_online: wx.getStorageSync('is_online')
  22. });
  23. app.api.useApi(app.globalData.baseAppUrl + "api/recharge/order", {
  24. order_no: options.order_no,
  25. }, "get").then(function (res) {
  26. console.log(res)
  27. if (res.code != 200) {
  28. wx.showLoading({
  29. title: res.message.msg,
  30. })
  31. setTimeout(function () {
  32. wx.hideLoading({
  33. success: (res) => { },
  34. })
  35. }, 1000);
  36. } else {
  37. var pageinfo = res.message.data
  38. that.setData({
  39. pageinfo: pageinfo,
  40. })
  41. if (pageinfo.total_money <= 0 || parseFloat(pageinfo.total_money) - parseFloat(pageinfo.price) <= 0) {
  42. var price = parseFloat(pageinfo.price) - parseFloat(pageinfo.total_money)
  43. that.setData({ price: price })
  44. }
  45. }
  46. }).catch(function (err) { console.log(err) })
  47. },
  48. /**
  49. * 生命周期函数--监听页面初次渲染完成
  50. */
  51. onReady: function () {
  52. },
  53. pay() {
  54. let that = this
  55. if (that.data.price > 0) {
  56. this.wxPay();
  57. } else {
  58. this.opay();
  59. }
  60. },
  61. opay(pay_trade_no = "") {
  62. var that = this;
  63. var no = that.data.order_no
  64. app.api.useApi(app.globalData.baseAppUrl + "api/payment", {
  65. order_no: no,
  66. pay_trade_no: pay_trade_no
  67. }, "post").then(function (res) {
  68. console.log(res)
  69. if (res.code != 200) {
  70. wx.showLoading({
  71. title: res.message.msg,
  72. })
  73. setTimeout(function () {
  74. wx.hideLoading({
  75. success: (res) => { },
  76. })
  77. }, 1000);
  78. } else {
  79. wx.reLaunch({
  80. url: '/pages/order/detail/detail?order_no=' + no + '&subscribe=1'
  81. })
  82. }
  83. }).catch(function (err) { console.log(err) })
  84. // wx.switchTab({
  85. // url: '/pages/tabber/order/index',
  86. // })
  87. },
  88. click(e) {
  89. // this.pageinfo.price=e.currentTarget.dataset.price;
  90. this.setData({
  91. price: e.currentTarget.dataset.price,
  92. give_price: e.currentTarget.dataset.gprice,
  93. })
  94. },
  95. wxPay() {
  96. var that = this;
  97. var no = that.data.price
  98. app.api.useApi(app.globalData.baseAppUrl + "api/recharge/charge", {
  99. price: no.toFixed(2),
  100. }, "post").then(function (res) {
  101. console.log(res)
  102. if (res.code != 200) {
  103. wx.showLoading({
  104. title: res.message.msg,
  105. })
  106. setTimeout(function () {
  107. wx.hideLoading({
  108. success: (res) => { },
  109. })
  110. }, 1000);
  111. } else {
  112. var paydata = res.message.data
  113. wx.requestPayment({
  114. timeStamp: paydata.timeStamp,
  115. nonceStr: paydata.nonceStr,
  116. package: paydata.package,
  117. signType: paydata.signType,
  118. paySign: paydata.paySign,
  119. success(res) {
  120. console.log(res);
  121. if (res.errMsg == "requestPayment:ok") {
  122. that.opay(paydata.pay_trade_no);
  123. }
  124. },
  125. fail(res) {
  126. wx.showLoading({
  127. title: "失败,请重试!",
  128. })
  129. setTimeout(function () {
  130. wx.hideLoading({
  131. success: (res) => { },
  132. })
  133. }, 1000);
  134. console.log(res)
  135. }
  136. })
  137. }
  138. }).catch(function (err) { console.log(err) })
  139. },
  140. back: function () {
  141. wx.redirectTo({
  142. url: '/pages/order/detail/detail?order_no=' + this.data.order_no + '&subscribe=1',
  143. })
  144. }
  145. })