confirmPayment.js 8.93 KB
// pack-activity/pack/confirmPayment.js
import util, { unixtoTime } from '../../utils/util';
import App from '../../utils/api';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        opt:{
            detail:{},
            combine_out_trade_no:'',
            totalize:'',
        },
        type: '',
        time: 300,
        countdown: '',
        name:"",
        date: "",
        phone:"",
        calendarData:'',
        sex:"先生",
        index:0,
        sexList:[
            {
                sex:'先生',
                value:'1',
            },
            {
                sex:'女士',
                value:'2',
            },
            {
                sex:'公司',
                value:'3',
            }
        ]
    },
    modal:'',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            opt:{
                detail:options.detail?JSON.parse(options.detail):'',
                combine_out_trade_no:options.combine_out_trade_no,
                totalize:options.totalize?options.totalize:'',
                sellerid:options.sellerid?options.sellerid:'',
            },
            type:options.type,
        })
        console.log('onLoad',{...this.data.opt});
        let obj=wx.getStorageSync('info')
        console.log('objobjobjobj',{...obj})
        this.setData({
            name:obj.name,
            date:obj.date,
            phone:obj.phone,
            index:obj.sex*1-1,
            calendarData:obj.calendarData,
        })
        this.data.name=obj.name;
        this.data.date=obj.date;
        this.data.phone=obj.phone;
        this.data.index=obj.sex*1-1;
        this.countdown()
        this.modal = this.selectComponent("#payModal"); //通过给组件所起的id调用组件
    },
    showModal() {
        this.modal.showModal() //调用组件中打开模态框方法
    },
    confirmPayment(){
        App.PayOnline.CombineTransactionsByNewRecharge({
            combine_out_trade_no:this.data.opt.combine_out_trade_no
        }).then(res=>{
            console.log(res.data);
            let that=this;
            if(res.ret!='200'){
                 return util.toast(res.msg,'none');
            }
            if(res.ret=='200'){
                console.log(res.ret);
                wx.requestPayment({
                    timeStamp: res.data.timeStamp+'',
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success (ress) { 
                        if (ress.errMsg == 'requestPayment:ok') {
                            console.log('支付成功',ress);
                            return that.showModal()
                        }
                        console.log('支付失败',ress);
                        wx.showToast({
                            title: '支付失败,请重新下单',
                            duration: 1000,
                            success: function () {
                                setTimeout(function () {
                                    wx.reLaunch({
                                        url: `/pages/customer/detail_owner_invitation_new?id=${that.data.opt.detail.oid}&hasInvitation=1&hasSellerShare=1&tplid=1`
                                    })
                                }, 1000);
                            }
                        })
                    },
                    fail (ress) {
                        console.log('支付失败',ress);
                        wx.showToast({
                            title: '支付失败,请重新下单',
                            duration: 1000,
                            icon:'none',
                            success: function () {
                                setTimeout(function () {
                                    wx.reLaunch({
                                        url: `/pages/customer/detail_owner_invitation_new?id=${that.data.opt.detail.oid}&hasInvitation=1&hasSellerShare=1&tplid=1`
                                    })
                                }, 1000);
                            }
                        })
                    },
                })
            }
        })
    },
    // 充值后跳转
    recharge(){
        console.log('充值完成跳转',this.data.opt.detail.oid);
        this.closeModal() 
        wx.reLaunch({
            url: `/pages/customer/detail_owner_invitation_new?id=${this.data.opt.detail.oid}&hasInvitation=1&hasSellerShare=1&tplid=1`
        })
    },
    // 绑定名称
    bindKeyNameInput(event){
        const value = event.detail.value.replace(/\s+/g, "");
        this.setData({
            name: value 
        }) 
        console.log(value);
    },
    // 绑定电话
    bindKeyPhoneInput(event){
        const value = event.detail.value.replace(/\s+/g, "");
        this.setData({
            phone: value 
        }) 
        console.log(value);
    },
    // 购买代金券后跳转
    certificate(){
        console.log({...this.data.opt.detail});
        wx.reLaunch({
            url: '/pages/me/coupon_news'
        })
    },
    closeModal() {
        this.modal.closeModal() //调用组件中关闭模态框方法
    },
    // 点击确认支付
    formSubmit() {
        if(this.data.time==0){
            return util.toast('当前订单已过期,请重新进入优惠券购买页面下单', 'none');
        }
        if(this.data.type=='2'){
            let data=this.data
            if (!data.name) {
                util.toast('未填写姓名', 'none')
                return;
            }
            if (!data.phone) {
                util.toast('未填写手机号码', 'none')
                return;
            }
            if (!/^1\d{10}$/.test(data.phone)) {
                util.toast('手机号码错误', 'none')
                return;
            }
            let recharge_money=this.data.opt.detail.recharge_money*1;
            if(recharge_money==0){
                setTimeout(() => {
                    util.toast('您的会员充值已经在办理中,到店消费时请和收银核实,谢谢!','none',3000);
                    wx.navigateBack(1)
                }, 3500);
                return 
            }
            this.addInfo()
        }else{
            this.confirmPayment()
        }
    },
    addInfo(){
        let params={
            sellerid:this.data.opt.sellerid,
            rname:this.data.name,
            sex:this.data.index*1+1,//1男  2女 3公司
            phone:this.data.phone,
            c_day:this.data.calendarData
        }
        console.log({...params});
        App.GuestManage.InsGuest(params).then(res=>{
            console.log(res);
            if(res.ret=='200'){
                this.confirmPayment()
                let obj={
                    name:this.data.name,
                    phone:this.data.phone,
                    sex:this.data.index*1+1,
                    calendarData:this.data.calendarData,
                    date:this.data.date,
                }
                wx.setStorageSync('info', obj)
            }
        })
    },
    // 选择日期
    bind_ChangeDate(event) {
        let date = event.detail.value
        let dateGl=event.detail.value.split('-');
        let calendarData;
        if(dateGl.length==3){
            calendarData=dateGl[0]+'年'+dateGl[1]+'月'+dateGl[2]+'日(公历)';
            console.log(dateGl);
            console.log(calendarData);
        }
        this.setData({
            date,
            calendarData
        })
    },
    // 选择性别
    bind_Changesex(event){
        console.log('event.detail',{...event.detail});
        let index = event.detail.value;
        this.setData({
            index
        })
        console.log('index',index);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    // 倒计时
    countdown() {
        var minute = Math.floor(this.data.time*1 / 60);
        var second = this.data.time % 60;
        second < 10 ? second = '0' + second : '';
        this.setData({
            countdown: minute + ':' + second,
            time: this.data.time - 1
        })
        if(this.data.time==0){
            this.setData({
                countdown: '0:00'
            })
            return false;
        }
        setTimeout(this.countdown, 1000);
    },
    navEvent(){
      wx.navigateBack(2);
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})