const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
const tools = require('../../../tools');
const helper = require('../../helper');
const config  = require('../../../etc/config.json')
const CMD = {}
CMD.get_callback_template = async function (mini_program_platform_id=config.dy,qm_id) {
    const url = 'https://new-media-iaa-fx.qimao.com/api/postback/rule/list';
    
    // 请求参数
    const params = new URLSearchParams({
        page: '1',
        page_size: '50',
        env: '',
        t: Date.now()  // 当前时间戳
    });
    let param_list = qm_id.split(',')
    let app_external_id = param_list[1]
    let target_id = param_list[0]
    // 请求头
    var dy_headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "app-id": "ttaeafd812319b128201",
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU1NzcwMjIxODI2MzQ0OTYxMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzQyMjgwNDU1LCJDbGllbnRJUCI6IjIxOS4xMzYuMTMwLjEwNyIsIkJ1ZmZlclRpbWUiOjAsImV4cCI6MTc0MjM2Njg1NSwiaXNzIjoicW1fbmV3X21lZGlhX2Z4IiwibmJmIjoxNzQyMjgwNDU1LCJFbnYiOiJyZWxlYXNlIiwiSXNTaW11bGF0aW9uTG9naW4iOmZhbHNlfQ.nUn5QkVnC-oQ6qwjc8Bh5jRTsTKqwzPS5cphBIiaBvU",
        "project": "reader_free_dyminiapp",
        "sec-ch-ua": "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Microsoft Edge\";v=\"134\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "signature": "1a0ace44e3187f9f6729fcf83aa0ccb357815d3ce56d744c6f28ff9efc1f4d88",
        "target-id": `${target_id}`,
        "cookie": "sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; sa_jssdk_2015_new-media-iaa-fx_qimao_com=%7B%22distinct_id%22%3A%22195a243a180505-022ff8224617bd2-4c657b58-921600-195a243a1815e0%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22http%3A%2F%2F120.79.128.142%3A4999%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTk1YTI0M2ExODA1MDUtMDIyZmY4MjI0NjE3YmQyLTRjNjU3YjU4LTkyMTYwMC0xOTVhMjQzYTE4MTVlMCJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D; acw_tc=0a47314f17422851558435810e0143dcd26df2a491694d1aabf9dfaa71e07d",
        "Referer": "https://new-media-iaa-fx.qimao.com/backend/postback-configuration/index?projectId=reader_free_dyminiapp&appId=ttaeafd812319b128201&accountId=558102575356793781",
        "Referrer-Policy": "strict-origin-when-cross-origin"
    };

    var wx_headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "app-id": "ttaeafd812319b128201",
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU1NzcwMjIxODI2MzQ0OTYxMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzQyMjgwNDU1LCJDbGllbnRJUCI6IjIxOS4xMzYuMTMwLjEwNyIsIkJ1ZmZlclRpbWUiOjAsImV4cCI6MTc0MjM2Njg1NSwiaXNzIjoicW1fbmV3X21lZGlhX2Z4IiwibmJmIjoxNzQyMjgwNDU1LCJFbnYiOiJyZWxlYXNlIiwiSXNTaW11bGF0aW9uTG9naW4iOmZhbHNlfQ.nUn5QkVnC-oQ6qwjc8Bh5jRTsTKqwzPS5cphBIiaBvU",
        "project": "reader_free_dyminiapp",
        "sec-ch-ua": "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Microsoft Edge\";v=\"134\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "signature": "1a0ace44e3187f9f6729fcf83aa0ccb357815d3ce56d744c6f28ff9efc1f4d88",
        "target-id": `${target_id}`,
        "cookie": "sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; sa_jssdk_2015_new-media-iaa-fx_qimao_com=%7B%22distinct_id%22%3A%22195a243a180505-022ff8224617bd2-4c657b58-921600-195a243a1815e0%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22http%3A%2F%2F120.79.128.142%3A4999%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTk1YTI0M2ExODA1MDUtMDIyZmY4MjI0NjE3YmQyLTRjNjU3YjU4LTkyMTYwMC0xOTVhMjQzYTE4MTVlMCJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D; acw_tc=0a47314f17422851558435810e0143dcd26df2a491694d1aabf9dfaa71e07d",
        "Referer": "https://new-media-iaa-fx.qimao.com/backend/postback-configuration/index?projectId=reader_free_dyminiapp&appId=ttaeafd812319b128201&accountId=558102575356793781",
        "Referrer-Policy": "strict-origin-when-cross-origin"
    };

    let headers = ""
    if(mini_program_platform_id==config.wx){
        headers = wx_headers
    }else if(mini_program_platform_id==config.dy){
        headers = dy_headers
    }

    headers['authorization'] = await tools.getQMMFToken()
    try {
        const response = await fetch(`${url}?${params}`, {
            method: 'GET',
            headers: headers
        });

        const data = await response.json();

        if(data.code!=200){
            throw data
        }
        
        console.log('响应状态:', response.status);
        console.log('响应数据:', data);
        return data.data;
    } catch (error) {
        console.error('请求错误:', error);
        throw error;
    }
}

// Node.js 18及以上版本可以直接运行
// Node.js 18以下版本需要先安装 node-fetch:
// npm install node-fetch@2

module.exports = CMD;