pay.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // myComponents/pay/pay.js
  2. const pay = require('../../utils/pay')
  3. const api = require('../../api/index')
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. },
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. show:false,
  15. data:{},
  16. voucherShow:'暂无可用优惠券',
  17. voucherTotal:0
  18. },
  19. /**
  20. * 组件的方法列表
  21. */
  22. methods: {
  23. //打开弹窗
  24. show(data){
  25. if(data.discount){
  26. data.finalPrice = Number(data.price) * Number(data.discount)
  27. data.discountPrice = Number(data.price) - data.finalPrice
  28. }else{
  29. data.finalPrice = Number(data.price)
  30. data.discountPrice = Number(data.price) - data.finalPrice
  31. }
  32. this.setData(
  33. {
  34. data:data,
  35. show:true
  36. }
  37. )
  38. this.setData(
  39. {
  40. ['data.discountText']:this.decimalToDiscount(this.data.data.discount)
  41. }
  42. )
  43. if(this.data.data.type == -1 || this.data.data.type == 0 || this.data.data.type){
  44. this.queryPersonVoucher()
  45. }
  46. },
  47. decimalToDiscount(decimal){
  48. // 确保输入是有效的数字,并且范围在0到1之间(包括0但不包括1)
  49. if (typeof decimal !== 'number' || isNaN(decimal) || decimal <= 0 || decimal >= 1) {
  50. return ''
  51. }
  52. // 将小数乘以10,四舍五入到最近的整数,并转换为字符串
  53. var discount = Math.round(decimal * 10);
  54. // 添加"折"字作为单位
  55. return discount + '折';
  56. },
  57. //获取优惠券总数
  58. queryPersonVoucher(){
  59. var params = {
  60. useScopes:[this.data.data.type]
  61. }
  62. api.queryPersonVoucher(params).then(res=>{
  63. if(res.code == 200){
  64. if(res.data.total>0){
  65. var data = res.data.data.filter(item=>{
  66. return item.threshold <= Number(this.data.data.price)
  67. })
  68. this.setData(
  69. {
  70. voucherShow:`${data.length}张待使用券`,
  71. voucherTotal:data.length
  72. }
  73. )
  74. }else{
  75. this.setData(
  76. {
  77. voucherShow:'暂无可用优惠券'
  78. }
  79. )
  80. }
  81. }
  82. })
  83. },
  84. //关闭弹窗
  85. onClose(){
  86. this.setData(
  87. {
  88. show:false
  89. }
  90. )
  91. wx.showToast({
  92. title: '取消支付',
  93. icon: 'none'
  94. })
  95. },
  96. //查看优惠券
  97. checkCoupons(){
  98. var that = this
  99. wx.navigateTo({
  100. url: '/pages/voucher/voucher',
  101. events: {
  102. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  103. acceptDataFromVoucher: function(data) {
  104. console.log(data)
  105. var discountPrice = Number(data.discountPrice)
  106. var totalPrice = Number(that.data.data.price)
  107. var discount = Number(that.data.data.discount)
  108. var finalPrice = 0
  109. finalPrice =( totalPrice - discountPrice<=0)?0:totalPrice - discountPrice
  110. if(discount){
  111. finalPrice = finalPrice * discount
  112. }
  113. var voucherShow = ''
  114. if(discountPrice >0){
  115. voucherShow = `- ¥${discountPrice}`
  116. }else if(that.data.voucherTotal > 0){
  117. voucherShow = `${that.data.voucherTotal}张待使用券`
  118. }else{
  119. voucherShow='暂无可用优惠券'
  120. }
  121. that.setData(
  122. {
  123. ["data.personVoucherIds"]:data.chooseVoucher,
  124. ["data.finalPrice"]:finalPrice,
  125. ["data.discountPrice"]:totalPrice - finalPrice,
  126. voucherShow:voucherShow
  127. }
  128. )
  129. },
  130. },
  131. success: function(res) {
  132. // 通过eventChannel向被打开页面传送数据
  133. res.eventChannel.emit('acceptDataFromPay', { useScopes:that.data.data.type,chooseVoucher:that.data.data.personVoucherIds||[],totalPrice:that.data.data.price })
  134. }
  135. })
  136. },
  137. //支付
  138. payMoney(){
  139. var params = {
  140. goodType:this.data.data.type,
  141. goods:this.data.data.goods,
  142. servicePrice:this.data.data.servicePrice,
  143. price:this.data.data.price,
  144. discount:this.data.data.discount,
  145. personVoucherIds:this.data.data.personVoucherIds,
  146. // usedVouchers:this.data.data.servicePrice,
  147. finalPrice:this.data.data.finalPrice
  148. }
  149. wx.showLoading({
  150. title: '加载中',
  151. })
  152. pay.payMoney(params).then(res=>{
  153. if(res.errMsg == 'requestPayment:ok'){
  154. wx.showToast({
  155. title: '支付成功',
  156. })
  157. this.triggerEvent('success',this.data.data)
  158. this.setData(
  159. {
  160. show:false
  161. }
  162. )
  163. }
  164. }).catch(error=>{
  165. })
  166. },
  167. }
  168. });