index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const WXAPI = require('apifm-wxapi')
  2. const wxpay = require('../../utils/pay.js')
  3. const AUTH = require('../../utils/auth')
  4. Page({
  5. data: {
  6. payType: 'wxpay'
  7. },
  8. onLoad: function (options) {
  9. this.payBillDiscounts()
  10. this.setData({
  11. balance_pay_pwd: wx.getStorageSync('balance_pay_pwd')
  12. })
  13. },
  14. onShow () {
  15. AUTH.checkHasLogined().then(isLogined => {
  16. if (!isLogined) {
  17. AUTH.openLoginDialog()
  18. } else {
  19. this.userAmount()
  20. }
  21. })
  22. },
  23. async payBillDiscounts() {
  24. const res = await WXAPI.payBillDiscounts()
  25. if (res.code === 0) {
  26. this.setData({
  27. rechargeSendRules: res.data
  28. })
  29. }
  30. },
  31. async userAmount() {
  32. const res = await WXAPI.userAmount(wx.getStorageSync('token'))
  33. if (res.code === 0) {
  34. this.setData({
  35. balance: res.data.balance
  36. })
  37. }
  38. },
  39. async bindSave() {
  40. const amount = this.data.amount;
  41. if (!amount) {
  42. wx.showToast({
  43. title: '请填写正确的消费金额',
  44. icon: 'none'
  45. })
  46. return
  47. }
  48. if (this.data.payType == 'balance') {
  49. if (this.data.balance_pay_pwd && !this.data.pwd) {
  50. wx.showToast({
  51. title: '请输入交易密码',
  52. icon: 'none'
  53. })
  54. return
  55. }
  56. }
  57. const res = await WXAPI.payBillV2({
  58. token: wx.getStorageSync('token'),
  59. money: amount,
  60. calculate: true
  61. })
  62. if (res.code != 0) {
  63. wx.showToast({
  64. title: res.msg,
  65. icon: 'none'
  66. })
  67. return
  68. }
  69. const amountReal = res.data.realMoney
  70. const _msg = `您本次消费${amount},优惠后只需支付${amountReal}`
  71. wx.showModal({
  72. title: '请确认消费金额',
  73. content: _msg,
  74. confirmText: "确认支付",
  75. cancelText: "取消支付",
  76. success: res => {
  77. if (res.confirm) {
  78. this.goPay(amount, amountReal)
  79. }
  80. }
  81. });
  82. },
  83. async goPay(amount, wxpayAmount){
  84. if (this.data.payType == 'balance') {
  85. const res = await WXAPI.payBillV2({
  86. token: wx.getStorageSync('token'),
  87. money: amount,
  88. pwd: this.data.pwd
  89. })
  90. if (res.code != 0) {
  91. wx.showToast({
  92. title: res.msg,
  93. icon: 'none'
  94. })
  95. return
  96. }
  97. wx.showModal({
  98. title: '成功',
  99. content: '买单成功,欢迎下次光临!',
  100. showCancel: false
  101. })
  102. } else {
  103. wxpay.wxpay('paybill', wxpayAmount, 0, "/pages/asset/index", { money: amount});
  104. }
  105. },
  106. processLogin(e) {
  107. if (!e.detail.userInfo) {
  108. wx.showToast({
  109. title: '已取消',
  110. icon: 'none',
  111. })
  112. return;
  113. }
  114. AUTH.register(this);
  115. },
  116. payTypeChange(event) {
  117. this.setData({
  118. payType: event.detail,
  119. });
  120. },
  121. payTypeClick(event) {
  122. const { name } = event.currentTarget.dataset;
  123. this.setData({
  124. payType: name,
  125. });
  126. },
  127. })