<style>
.test-links {
    padding: 20px;
}
.test-links a{
    display: block;
    line-height: 32px;
    font-size: 24px;
    margin-bottom: 20px;
    text-decoration: underline;
}
</style>

<template>
    <div class="test-links">
        <mt-button type="default" @click.native="handlePay">支付</mt-button>
    </div>
</template>

<script>
import lib from 'lib'
import {Indicator, Toast, MessageBox} from 'mint-ui';
import axios from 'axios'

export default {
    data () {
        return {
            response: ''
        }
    },
    mounted () {
        // let code = lib.getParam('code') || '';
        // let state = lib.getParam('state') || '';
        // $.ajax(`${lib.apiUrl}/auth/authWebAT?code=${code}&state=${state}`, (res) => {
        //     this.response = JSON.stringify(res);
        // })
    },
    methods: {
        async handlePay() {
            let configDone = await this.configWX();
            let uid = lib.getParam('uid');
            let dwAppId = lib.getParam('dwAppId');

            if(!configDone) {
                MessageBox('提示', '未知错误，请重试');
                return;
            };
            
            axios.post(`${lib.apiUrl}/order/proxyPay`, {
                dwAppId: dwAppId,
                uid: uid,
                productId: 1,
                count: 1 
            }).then(({data}) => {
                let pData = data.data.platformData;
                console.log(pData);
                wx.chooseWXPay({
                    timestamp: pData.timestamp,
                    nonceStr: pData.nonceStr,
                    package: pData.package,
                    signType: pData.signType,
                    paySign: pData.paySign,
                    success (res) {
                        // 支付成功后的回调函数
                    }
                })

            })
        },
        configWX() {
            return new Promise((resolve, reject) => {
                axios.get(`${lib.apiUrl}/auth/jsSign`, {
                    params: {
                        url: location.href
                    }
                }).then(({data}) => {
                    wx.config({
                        debug: false,
                        appId: 'wxc137e1554d993f74',
                        nonceStr: data.data.nonceStr,
                        signature: data.data.signature,
                        timestamp: data.data.timestamp,
                        jsApiList: [
                            "chooseWXPay"
                        ]
                    });

                    wx.error(function(res){
                        MessageBox("提示", JSON.stringify(res))
                    });

                    wx.ready(() => {
                        resolve(true)
                    })
                }).catch(({data}) => {
                    reject(false)
                })
            })
        },
        checkOrderState() {
            
        }
    }
}
</script>
