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-iaa-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': '4c6e8d8709c14de9a4397b6cbb978f85',
        'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0MDg4MjI1LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0MTc0NjI1LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQwODgyMjUsIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.LaEHd147NoCt1Wfo9ZZlCs4pludA7D3LajN4xff2H5o',
        'project': 'reader_free_dyminiapp',
        'signature': '889e0e141a9e82d37d4611d130d16b277a09b88cd9d3ff0b117ecf616679bb0e',
        'target-id': '558102575356793781',
        '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=0aef832217422796109355798e009653b7fda67dfeeb3ff092f5d3dcf75e97',
        'Referer': 'https://new-media-iaa-fx.qimao.com/backend/book-manage/index?projectId=reader_free_dyminiapp&appId=ttaeafd812319b128201&accountId=558102575356793781'
    };


    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;