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_qm_account_list = async function () {
    const url = 'https://new-media-fx.qimao.com/api/account/optimizer/list';
    // 请求参数
    const params = new URLSearchParams({
        page: '1',
        page_size: '50',
        env: '',
        t: Date.now()  // 当前时间戳
    });
    // 请求头
    var 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-external-id": "",
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM5NTk1MzY2LCJDbGllbnRJUCI6IjYxLjE0MC45My4yMDIiLCJCdWZmZXJUaW1lIjowLCJleHAiOjE3Mzk2ODE3NjYsImlzcyI6InFtX25ld19tZWRpYV9meCIsIm5iZiI6MTczOTU5NTM2NiwiRW52IjoicmVsZWFzZSIsIklzU2ltdWxhdGlvbkxvZ2luIjpmYWxzZX0.23-zyHDy8Mc_JN3vN2gvZLJv6CW0ARoQO3b7N6oZJuA",
        "project": "",
        "sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Microsoft Edge\";v=\"133\", \"Chromium\";v=\"133\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "signature": "3f0e54fda1cb84147dfb9679a38a7592bb29b5c00be061f06bffd64876817ac5",
        "target-id": "",
        "cookie": "sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; acw_tc=1a0c380d17395953377404144e004c61382d50868332444d857d7a8b7fb305; sa_jssdk_2015_new-media-fx_qimao_com=%7B%22distinct_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%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_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzYmRjMTI0NjQ1ZTItMGNiZWFlZTE3MmQ0Y2U4LTRjNjU3YjU4LTkyMTYwMC0xOTNiZGMxMjQ2NTU2NyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D",
        "Referer": "https://new-media-fx.qimao.com/backend/account-manage/index?projectId=0&appId=0&accountId=0",
        "Referrer-Policy": "strict-origin-when-cross-origin"
    };


    headers['authorization'] = await tools.getQMToken()
    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;