cashier.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(){
  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. }, "post").then(function (res) {
  67. console.log(res)
  68. if (res.code != 200) {
  69. wx.showLoading({
  70. title: res.message.msg,
  71. })
  72. setTimeout(function () {
  73. wx.hideLoading({
  74. success: (res) => { },
  75. })
  76. }, 1000);
  77. } else {
  78. wx.navigateTo({
  79. url: '/pages/order/detail/detail?order_no='+no
  80. })
  81. }
  82. }).catch(function (err) {console.log(err)})
  83. // wx.switchTab({
  84. // url: '/pages/tabber/order/index',
  85. // })
  86. },
  87. click(e){
  88. // this.pageinfo.price=e.currentTarget.dataset.price;
  89. this.setData({
  90. price:e.currentTarget.dataset.price,
  91. give_price:e.currentTarget.dataset.gprice,
  92. })
  93. },
  94. wxPay(){
  95. var that=this;
  96. var no=that.data.price
  97. app.api.useApi(app.globalData.baseAppUrl + "api/recharge/charge", {
  98. price: no,
  99. }, "post").then(function (res) {
  100. console.log(res)
  101. if (res.code != 200) {
  102. wx.showLoading({
  103. title: res.message.msg,
  104. })
  105. setTimeout(function () {
  106. wx.hideLoading({
  107. success: (res) => { },
  108. })
  109. }, 1000);
  110. } else {
  111. var paydata=res.message.data
  112. wx.requestPayment({
  113. timeStamp: paydata.timeStamp,
  114. nonceStr: paydata.nonceStr,
  115. package: paydata.package,
  116. signType: paydata.signType,
  117. paySign: paydata.paySign,
  118. success (res) {
  119. console.log(res);
  120. if(res.errMsg=="requestPayment:ok"){
  121. that.opay();
  122. }
  123. },
  124. fail (res) {
  125. wx.showLoading({
  126. title: "失败,请重试!",
  127. })
  128. setTimeout(function () {
  129. wx.hideLoading({
  130. success: (res) => { },
  131. })
  132. }, 1000);
  133. console.log(res)}
  134. })
  135. }
  136. }).catch(function (err) {console.log(err)})
  137. },
  138. back:function(){
  139. wx.redirectTo({
  140. url: '/pages/order/detail/detail?order_no='+this.data.order_no,
  141. })
  142. }
  143. })