payment.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // pages/payment/payment.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. paymentInfo: [{
  9. id: ''
  10. }],
  11. amount: '',
  12. amountTags: [50, 100, 150, 200],
  13. debtInfo: {},
  14. activeAmount: '',
  15. amountTip: '请输入金额',
  16. numberFormat: app.$util.numberFormat
  17. },
  18. setAmount(e) {
  19. const {
  20. value
  21. } = e.target.dataset;
  22. this.setData({
  23. amount: value || e.detail.value,
  24. activeAmount: value || ''
  25. })
  26. },
  27. toOtherPage(e) {
  28. const {
  29. url
  30. } = e.target.dataset;
  31. wx.navigateTo({
  32. url: `/pages/payment/${url}/${url}`,
  33. })
  34. },
  35. submit() {
  36. const {amount} = this.data;
  37. if(!amount) {
  38. wx.showToast({
  39. title: '请输入金额',
  40. icon:'none',
  41. duration: 2000
  42. })
  43. return;
  44. }
  45. if(!(/^\d{1,8}$|^\d{1,6}[.]\d{1,2}$/.test(amount))) {
  46. wx.showToast({
  47. title: '输入金额格式有误',
  48. icon:'none',
  49. duration: 2000
  50. })
  51. return;
  52. }
  53. if(amount < this.data.debtInfo.totalDebt) {
  54. wx.showToast({
  55. title: '缴费金额不能低于累计欠费金额,请重新输入',
  56. icon:'none',
  57. duration: 2000
  58. })
  59. return;
  60. }
  61. const self = this;
  62. app.$http.get('/user/getOrderInfo', {
  63. payfee: amount
  64. }).then(({status, data = {}}) => {
  65. if(status == 0) {
  66. const {nonceStr, outTradeNo, packages, paySign, signType, timeStamp} = data
  67. wx.requestPayment({
  68. timeStamp, // 时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
  69. nonceStr, // 随机字符串,长度为32个字符以下。
  70. package: packages, // 统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
  71. signType, // 签名类型,默认为MD5,支持HMAC-SHA256和MD5。注意此处需与统一下单的签名类型一致
  72. paySign, // 签名,具体签名方案参见微信公众号支付帮助文档;
  73. success: function (res) {
  74. app.$http.get('/user/payFee', {
  75. fee: amount,
  76. customerCode: self.data.debtInfo.accountNumber,
  77. outTradeNo
  78. }).then(({status, data = {}, msg}) => {
  79. if(status == 0) {
  80. wx.showToast({
  81. title: msg,
  82. icon: 'success',
  83. duration: 2000
  84. })
  85. } else {
  86. wx.showToast({
  87. title: msg,
  88. icon:'none',
  89. duration: 2000
  90. })
  91. }
  92. self.getDebtInfo();
  93. }).catch(() => {
  94. wx.showToast({
  95. title: '缴费失败',
  96. icon:'none',
  97. duration: 2000
  98. })
  99. })
  100. },
  101. fail: function (res) {
  102. if(res.errMsg == "requestPayment:fail cancel") return;
  103. wx.showToast({
  104. title: '缴费失败',
  105. icon:'none',
  106. duration: 2000
  107. })
  108. }
  109. })
  110. }
  111. })
  112. },
  113. getLastPayRecord() {
  114. app.$http.get('/user/getLastPayRecord').then(({status, data = {}}) => {
  115. if(status == 0) {
  116. data.fee = app.$util.numberFormat(data.fee, 2);
  117. this.setData({
  118. amountTip: `上次缴费金额${data.fee || 0}元`
  119. })
  120. }
  121. })
  122. },
  123. // GET /user/getDebtInfo
  124. getDebtInfo() {
  125. app.showLoading();
  126. app.$http.get('/user/getDebtInfo').then(({status, data = {}}) => {
  127. if(status == 0) {
  128. const totalDebt = JSON.parse(JSON.stringify(data.totalDebt));
  129. data.receFee = app.$util.numberFormat(data.receFee, 2);
  130. data.totalDebt = app.$util.numberFormat(data.totalDebt, 2);
  131. this.setData({
  132. debtInfo: data,
  133. amount: totalDebt || ''
  134. })
  135. if(totalDebt == 0) {
  136. this.getLastPayRecord();
  137. }
  138. }
  139. app.hideLoading();
  140. }).catch(() => {
  141. app.hideLoading();
  142. })
  143. },
  144. /**
  145. * 生命周期函数--监听页面加载
  146. */
  147. onLoad: function (options) {
  148. this.getDebtInfo();
  149. },
  150. })