 /*
 * 手太欠
 * 愿这世界都如故事里一样 美好而动人~
 */

Page({
    data: {
        orderNo     : '',  // 订单编号
        total       : '',  // 订单金额
        paySuccess  : false, // 兑换成功显示
        disabled    : true
    },

    onLoad(options) {
        this.setData({
            orderNo : options.order_no,
            total   : options.total
        })
    },

    onShow() {},

    /**
     * 确认支付
     */
    payBtn() {
        wx.showLoading({
            title: '支付中...',
            mask : true
        })
        let that = this
        wx.$api.mall.mallPay(this.data.orderNo,{type: 'miniapp', openid: wx.getStorageSync("openid")}).then(res=>{
            wx.hideLoading()
            this.setData({
                disabled: false
            })
            let payInfo = JSON.parse(res.data.wechat)
            wx.requestPayment({
                timeStamp: payInfo.timeStamp,
                nonceStr : payInfo.nonceStr,
                package  : payInfo.package,
                paySign  : payInfo.paySign,
                signType : payInfo.signType,
                success  : res=>{
                    if(res.errMsg == "requestPayment:ok"){
                        wx.showToast({
                            title: '支付成功',
                            icon: 'none',
                            duration: 2000,
                            //显示透明蒙层，防止触摸穿透
                            mask:true,
                            success: function () {
                                that.setData({
                                    paySuccess: true
                                })
                                setTimeout(()=>{
                                    wx.redirectTo({
                                        url: '/pages/order/index?state=paid'
                                    })
                                },3000)
                            }
                        })
                    }
                },
                fail  : err=>{
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                        duration: 500,
                        //显示透明蒙层，防止触摸穿透
                        mask:true,
                        success: function () {
                            that.setData({
                                paySuccess: true
                            })
                            setTimeout(()=>{
                                wx.redirectTo({
                                    url: '/pages/order/index?state=unpay'
                                })
                            },3000)
                        }
                    })
                }
            })
        }).catch(err => {});
    }
})
