pay.js 6.2 KB

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