
/**
 * 两种获取token封装（getPxToken和getDuibaToken）
 * 
 * 
 */
/**
 * @param  {} callback
 */
if (!document.getElementById("scriptkey")) {
    const scriptkey = document.createElement('script')
    scriptkey.async = 'async';
    scriptkey.src = `getTokenKey`;
    scriptkey.id = "scriptkey";
    document.getElementsByTagName('head')[0].appendChild(scriptkey);

    const scriptjs = document.createElement('script')
    scriptjs.async = 'async';
    scriptjs.id = "scriptjs";
    scriptjs.src = `//yun.duiba.com.cn/js-libs/px-token/0.0.3/px-token.min.js`;
    document.getElementsByTagName('head')[0].appendChild(scriptjs);
    scriptjs.onload = () => {
    }
  
}

const getPxToken = (callback) => {

    if (window.getPxToken) {
        __getPxToken(callback);
        return;
    }
    console.log("px-token.min.js尚未加载完成，请稍后再试")
    // callback("waiting..."); //这里就不返回，不做处理

}
function __getPxToken(callback) {
    console.info(location.origin)
    if(location.origin == "http://localhost:8088"){
        callback("11111");
    }else{

        if (window.getPxToken) {
            window.getPxToken((e, token) => {
                console.info('get px token:', e);
                switch (e) {
                    case 'net error':
                        break;
                    case 'need reload':
                        break;
                    case 'need login':
                        break;
                    case 'state invalid':
                        break;
                    default:
                        callback(token);
                }
            })
        }
    }
}
/**
 * @param  {} resolve
 * @param  {} reject
 * @param  {} againOrderId
 */
const getDuibaToken=(resolve, reject,againOrderId)=>{
    $.ajax({
        url: '/ctoken/getToken',
        type: 'post',
        dataType: 'json',
        success: function (res) {
            if (res.success) {
                var data = {
                    actId: CFG?.actId||"",
                    oaId: CFG?.oaId||""
                };
                eval(res.token);
                var key = '';
                data.token = window[key];

                if (againOrderId) {
                    data.againOrderId = againOrderId;
                }
                resolve && resolve(data);
            } else {
                reject('systemError', { message: res.message });
            }
        },
        error: function () {
            reject('networkError');
        }
    });
}

module.exports={
    getPxToken,
    getDuibaToken
} 
