payment.js 4.4 KB

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