import {createPayOrder, queryOrderState} from '../api/wx-jsapi'
import store from '../store'
export function wxPay(param) {
    return new Promise((resolve, reject) => {
        createPayOrder(param).then((res) => {
            if (res.yjReqNo) {
                store.dispatch({
                    type: 'storeYjReqNo',
                    yjReqNo: null
                })
            }
            if (param.totalFee === 0) {
                queryOrderState(res.tradeNo).then((response) => {
                    resolve(response)
                })
            } else {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',
                    {
                        appId: res.appId,
                        timeStamp: res.timeStamp,
                        nonceStr: res.serialNo,
                        package: res.prepayId,
                        signType: res.signType,
                        paySign: res.paySign,
                        totalFee: res.totalFee,
                    },
                    function () {
                        queryOrderState(res.tradeNo).then((response) => {
                            resolve(response)
                        })
                    }
                )
            }
        })
    })
}
