import Bus from "../bus.js";
import {
    post,
    get
} from '@/axios/http.js'


///////////////////////////
//xf_api
// 1-初始化 api/query
function xf_query(params) {
    Bus.debug && (params.openid = Bus.testOpenId)
    return readyPost(Bus.domin + "/api/query", params)
}

// 2-用户完成新手指引 api/finishGuide
function xf_finishGuide(params) {
    params.userId = Bus.userInfo['id']
    Bus.debug && (params.userId = Bus.testUserId)
    return readyPost(Bus.domin + "/api/finishGuide", params)
}

// 3-抽奖 api/lottery
function xf_lottery(params) {
    params.userId = Bus.userInfo['id']
    Bus.debug && (params.userId = Bus.testUserId)
    return readyPost(Bus.domin + "/api/lottery", params)
}

// 4-我的奖品 api/prize 
function xf_prize(params) {
    params.userId = Bus.userInfo['id']
    Bus.debug && (params.userId = Bus.testUserId)
    return readyPost(Bus.domin + "/api/prize", params)
}


// 5-保存实物奖物流信息 api/saveLogistics
function xf_saveLogistics(params) {
    params.userId = Bus.userInfo['id']
    Bus.debug && (params.userId = Bus.testUserId)
    return readyPost(Bus.domin + "/api/saveLogistics", params)
}


// 6-修改实物奖物流信息 api/updateLogistics
function xf_updateLogistics(params) {
    params.userId = Bus.userInfo['id']
    Bus.debug && (params.userId = Bus.testUserId)
    return readyPost(Bus.domin + "/api/updateLogistics", params)
}


// 初始化 test_api/query
function test_query(params) {
    return readyPost(Bus.domin + "/api/query", params)
}




///////////////////////////
//jgj_api
function jgj_lotteryInfo(params) {
    return getJgjOpenIdToken().then((res) => {
        Object.assign(params, res);
        return readyPost(Bus.jgj_domain + "/act-reward/external/lottery/info", params)
    })
}

function jgj_lotteryDraw(params) {
    return getJgjOpenIdToken().then((res) => {
        Object.assign(params, res);
        return readyPost(Bus.jgj_domain + "/act-reward/external/lottery/draw", params)
    })
}


//请求业务员地域编号
function jgj_queryAgentInfo(params) {
    return getJgjOpenIdToken().then((res) => {
        Object.assign(params, res);
        return readyPost(Bus.jgj_domain + "/act-core/external/third/party/queryAgentInfo", params)
    })
}

function jgj_getPaShareToken(params) {
    return getJgjOpenIdToken().then((res) => {
        Object.assign(params, res);
        return readyPost(Bus.jgj_getPaShareToken_url, params)
    })
}


//获取用户昵称
function jgj_userInfo(params) {
    return getJgjOpenIdToken().then((res) => {
        Object.assign(params, res);
        return readyPost(Bus.jgj_domain + "/act-core/external/third/party/user/info", params)
    })
}

//获取用户openid opentoken
function jgj_getJgjOpenIdToken(params) {
    return new Promise((resolve, reject) => {
        PALifeOpen.getOpenId({
            appId: Bus.jgj_appId
        }, (res) => {
            if (res.ret == 0) {
                res.CODE = "00"
                resolve(res)
            } else {
                reject(res)
            }
        }, (e) => { //
            console.debug('failed ', e);
            reject(e)
        });

    })
}

///////////////////////////



//单独请求接口 single_request
// function single_request(params) {
//     return new Promise((resolve, reject) => {
//         PALifeOpen.getOpenId({
//             appId: Bus.jgj_appId
//         }, (res) => {
//             if (res.ret == 0) {
//                 res.CODE = "00"
//                 resolve(res)
//             } else {
//                 reject(res)
//             }
//         }, (e) => { //
//             console.debug('failed ', e);
//             reject(e)
//         });

//     })
// }

function single_request(params) {
    // return getJgjOpenIdToken().then((res) => {
    // Object.assign(params, res);
    return readyPost(params.url, params.requestData)
    // })
}


///////////////////////////
// param.timestamp = new Date().getTime();
///////////////////////////


///////////////////////////

function getJgjOpenIdToken(cb) {
    var p = new Promise((resolve, reject) => {
        PALifeOpen.getOpenId({
            appId: Bus.jgj_appId
        }, (res) => {
            console.log(res);
            if (res.ret == 0) {
                Bus.jgj_data.openId = res.data.openId;
                Bus.jgj_data.openToken = res.data.openToken;

                resolve({
                    openid: res.data.openId,
                    openId: res.data.openId,
                    openToken: res.data.openToken,
                    merchantCode: Bus.jgj_appId,
                    activityId: Bus.jgj_activityId,
                    appId: Bus.jgj_appId,
                })
            }
        }, (e) => { //
            console.debug('failed ', e);
            reject(e)
        });

    })

    return p;
}

///////////////////////////


//核心readyPost方法
function readyPost(url, params) {
    // Object.assign(params, { retry: 5, retryDelay: 1000 })

    return new Promise((resolve, reject) => {
        post(url, params)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export {
    xf_query,
    xf_finishGuide,
    xf_lottery,
    xf_prize,
    xf_saveLogistics,
    xf_updateLogistics,
    test_query,


    jgj_lotteryInfo,
    jgj_lotteryDraw,
    jgj_queryAgentInfo,
    jgj_getPaShareToken,
    jgj_userInfo,
    jgj_getJgjOpenIdToken,

    single_request,
}
