import authorize from '@/common/config/authorize.js'

// 调用微信支付
function wxRequestPayment(payData) {
    return new Promise((resolve, reject) => {
        uni.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.package,
            signType: payData.signType,
            paySign: payData.paySign,
            success: () => {
                resolve(true)
            },
            fail: () => {
                reject({ msg: 'error' })
            }
        })
    })
}

// 微信支付
const payMixins = {
    data() {
        
    },
    methods: {
        // 微信支付正常订单
        async weChatMiniOrderWxPay(params) {
            try {
			    // 微信支付请求 返回支付信息
			    const response = await this.PayService.WeChatScanMiniWxPay(params)
			    // 处理支付信息 调用微信支付
			    const payment = JSON.parse(response.data.rt10_payInfo)
                console.log('payment',payment)
			    const payFlag = await wxRequestPayment(payment)
			    console.log('payFlag',payFlag)
                uni.setStorageSync('shopOrderInfo', this.shopOrderInfo)
			    if (payFlag) {
			       // 微信支付成功回调
			       let linkData =  {
                        payAmount:this.payAmount,
                        orderId:this.shopOrderInfo.orderId,
                        type:'success'
                    }
			        uni.redirectTo({ url: `/pages/user/pay/success?data=${JSON.stringify({ data: linkData })}`})
			    } else {
                    // 微信支付失败
			        this.$util.msg('支付失败~',2000)
			    }
			 } catch (error) {
                // 微信支付失败
                this.$util.msg('支付失败~',2000)
			 } 
        },
        // 微信支付优惠券
        async weChatMiniCouponWxPay(params,UmEvent,UmPageName,UmSourcePage,UmCouponId,UmUserId) {
            try {
                console.log('params',params)
			    // 微信支付请求 返回支付信息
			    const response = await this.PayService.WeChatCouponMiniWxPay(params)
			    // 处理支付信息 调用微信支付
			    const payment = JSON.parse(response.data.rt10_payInfo)
                // 友盟埋点收集微信支付
                if (process.env.NODE_ENV != 'development') {
                    this.$uma.trackEvent(UmEvent, {
                        Um_Key_PageName: UmPageName,
                        Um_Key_SourcePage: UmSourcePage,
                        Um_Key_CouponId: UmCouponId,
                        Um_Key_userId: UmUserId
                    })
                }
			    const payFlag = await wxRequestPayment(payment)
			    if (payFlag) {
			        // 微信支付成功回调
			        uni.reLaunch({url: '/pages/tabBar/user/user'})
			    } else {
			        this.$util.msg('支付失败~',2000)
			    }
			 } catch (error) {
                // 微信支付失败
                this.$util.msg('支付失败~',2000)
			 }  
        },
        // 微信支付超级会员
        async weChatMiniVipWxPay(params) {
            try {
                console.log('params',params)
			    // 微信支付请求 返回支付信息
			    const response = await this.PayService.PayOrderVipWechat(params)
			    // 处理支付信息 调用微信支付
			    const payment = JSON.parse(response.data.rt10_payInfo)
                console.log('payment',payment)
			    const payFlag = await wxRequestPayment(payment)
			    console.log('payFlag',this.payFlag)
			    if (payFlag) {
			        // 微信支付成功回调
			        uni.reLaunch({url: '/pages/tabBar/user/user'})
			    } else {
			        this.$util.msg('支付失败~',2000)
			    }
			 } catch (error) {
                // 微信支付失败
                this.$util.msg('支付失败~',2000)
			 } 
        }
    }
}

export default payMixins
