const axios = require('axios');
const querystring = require('querystring');
const tools = require('../../../tools');
const helper = require('../../helper');
const CMD = {}
CMD.search_id = function(bookId){
    const postData = {
        distributor_id: global.config.fanqie_config.distributorId,
        ts: helper.getCurrentUnixTimestamp(),
        sign: helper.getSign(global.config.fanqie_config.distributorId,global.config.fanqie_config.secretKey),
        book_id: bookId
    };
    var url = `https://www.changdunovel.com/novelsale/openapi/content/book_meta/v1/?distributor_id=${postData.distributor_id}&ts=${postData.ts}&sign=${postData.sign}&book_id=${postData.book_id}`;
    console.log("url",url)
    axios.get(url)
        .then(response => {
            console.log('search_id data:', response.data);
        })
        .catch(error => {
            console.error('Error:', error);
    });
}
// 设置 Cookie
CMD.search_name = function(keyword){
    // 搜索关键词
    const encodedKeyword = encodeURIComponent(keyword); // 对关键词进行 URL 编码

    // 设置正确的 Cookie 字符串
    const cookie = 'passport_csrf_token=82f80619e2d3c3eb0aa7962d7c94305e; sid_tt=c00d65a0717e75e79ad8aecaa82f4064; sessionid=c00d65a0717e75e79ad8aecaa82f4064';

    // 设置请求的 URL
    const url = `https://www.changdunovel.com/novelsale/distributor/content/book/list/v1?permission_statuses=3,4&search_type=2&query=${encodedKeyword}&page_index=0&page_size=10`;

    // 设置请求头
    const headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Agw-Js-Conv': 'str',
        'Appid': '546801',
        'Apptype': '1',
        'Cache-Control': 'no-cache',
        'Cookie': cookie,
        'Distributorid': '1814786227164185',
        'Pragma': 'no-cache',
        'Priority': 'u=1, i',
        'Referer': `https://www.changdunovel.com/sale/novel/list?permission_statuses=3%2C4&search_type=2&query=${encodedKeyword}&page_index=1&page_size=10`,
        'Sec-Ch-Ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"macOS"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
        'X-Secsdk-Csrf-Token': 'DOWNGRADE'
    };

    // 执行请求
    axios.get(url, { headers })
    .then(response => {
        console.log(response.data); // 输出结果
    })
    .catch(error => {
        console.error('Error:', error.message); // 输出错误信息
    });
}

module.exports = CMD;