123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- // myComponents/pay/pay.js
- const pay = require('../../utils/pay')
- const api = require('../../api/index')
- const Decimal = require('../../utils/decimal');
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- },
- /**
- * 组件的初始数据
- */
- data: {
- show:false,
- data:{},
- voucherShow:'暂无可用优惠券',
- voucherTotal:0
- },
- /**
- * 组件的方法列表
- */
- methods: {
- //打开弹窗
- show(data){
- if(data.discount){
- data.finalPrice =Decimal.mul([data.price,data.discount])
- data.discountPrice =Decimal.sub([data.price,data.finalPrice])
- }else{
- data.finalPrice = Number(data.price)
- data.discountPrice = Decimal.sub([data.price,data.finalPrice])
- }
- this.setData(
- {
- data:data,
- show:true
- }
- )
- this.setData(
- {
- ['data.discountText']:this.decimalToDiscount(this.data.data.discount)
- }
- )
- if(this.data.data.type == -1 || this.data.data.type == 0 || this.data.data.type){
- this.queryPersonVoucher()
- }
- },
- decimalToDiscount(decimal){
- // 确保输入是有效的数字,并且范围在0到1之间(包括0但不包括1)
- if (typeof decimal !== 'number' || isNaN(decimal) || decimal <= 0 || decimal >= 1) {
- return ''
- }
- // 将小数乘以10,并转换为字符串
- var discount = Decimal.mul([decimal,10]);
- // 添加"折"字作为单位
- return discount + '折';
- },
- //获取优惠券总数
- queryPersonVoucher(){
- var params = {
- useScopes:[this.data.data.type],
- ifExpiration:false,
- }
- api.queryPersonVoucher(params).then(res=>{
- if(res.code == 200){
- var data = res.data.data.filter(item=>{
- return item.threshold <= Number(this.data.data.price)
- })
- if(data.length>0){
-
- this.setData(
- {
- voucherShow:`${data.length}张待使用券`,
- voucherTotal:data.length
- }
- )
- }else{
- this.setData(
- {
- voucherShow:'暂无可用优惠券'
- }
- )
- }
-
- }
- })
- },
- //关闭弹窗
- onClose(){
- this.setData(
- {
- show:false
- }
- )
- wx.showToast({
- title: '取消支付',
- icon: 'none'
- })
- },
- //查看优惠券
- checkCoupons(){
- var that = this
- wx.navigateTo({
- url: '/pages/voucher/voucher',
- events: {
- // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
- acceptDataFromVoucher: function(data) {
- console.log(data)
- var discountPrice = Number(data.discountPrice)
- // var totalPrice = Number(that.data.data.price)
- // var discount = Number(that.data.data.discount)
- // var finalPrice = 0
- // finalPrice =( Decimal.sub([totalPrice,discountPrice])<=0)?0:Decimal.sub([totalPrice,discountPrice])
- // if(discount){
- // finalPrice =Decimal.mul([finalPrice, discount])
- // }
- var voucherShow = ''
- if(discountPrice >0){
- voucherShow = `- ¥${discountPrice}`
- }else if(that.data.voucherTotal > 0){
- voucherShow = `${that.data.voucherTotal}张待使用券`
- }else{
- voucherShow='暂无可用优惠券'
- }
- that.setData(
- {
- ["data.personVoucherIds"]:data.chooseVoucher,
- // ["data.finalPrice"]:finalPrice,
- // ["data.discountPrice"]:Decimal.sub([totalPrice,finalPrice]),
- voucherShow:voucherShow
- }
- )
- that.getOrderDetail()
- },
- },
- success: function(res) {
- // 通过eventChannel向被打开页面传送数据
- res.eventChannel.emit('acceptDataFromPay', { useScopes:that.data.data.type,chooseVoucher:that.data.data.personVoucherIds||[],totalPrice:that.data.data.price })
- }
- })
- },
- //获取订单详情
- getOrderDetail(){
- var params = {
- goodType:this.data.data.type,
- goods:this.data.data.goods,
- servicePrice:this.data.data.servicePrice,
- price:this.data.data.price,
- discount:this.data.data.discount,
- personVoucherIds:this.data.data.personVoucherIds,
- // usedVouchers:this.data.data.servicePrice,
- finalPrice:this.data.data.finalPrice,
- activityId:this.data.data.activityId
- }
- api.getOrderDetail(params).then(res=>{
- if(res.code == 200){
- this.setData(
- {
- ["data.orderMessage"]:res.data.data.orderMessage,
- ["data.activities"]:res.data.data.activities,
- ["data.activityId"]:res.data.data.activities.id,
- ["data.price"]:res.data.data.orderMessage.price,
- ["data.discount"]:res.data.data.orderMessage.discount,
- ["data.discountText"]:this.decimalToDiscount(res.data.data.orderMessage.discount),
- ["data.finalPrice"]:res.data.data.orderMessage.finalPrice,
- ["data.discountPrice"]:Decimal.sub([res.data.data.orderMessage.price,res.data.data.orderMessage.finalPrice]),
- }
- )
- }
- })
- },
- //支付
- payMoney(){
- var params = {
- goodType:this.data.data.type,
- goods:this.data.data.goods,
- servicePrice:this.data.data.servicePrice,
- price:this.data.data.price,
- discount:this.data.data.discount,
- personVoucherIds:this.data.data.personVoucherIds,
- // usedVouchers:this.data.data.servicePrice,
- finalPrice:this.data.data.finalPrice,
- activityId:this.data.data.activityId
- }
- wx.showLoading({
- title: '加载中',
- })
- pay.payMoney(params).then(res=>{
- if(res.errMsg == 'requestPayment:ok'){
- wx.showToast({
- title: '支付成功',
- })
- this.triggerEvent('success',this.data.data)
- this.setData(
- {
- show:false
- }
- )
- }
- }).catch(error=>{
-
- })
- },
- }
- });
|