const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
const tools = require('../../../tools');
const config  = require('../../../etc/config.json');
const redis_help = require('../../use_redis');
const helper = require('../../helper');
const CMD = {}
// app_type = 4 是抖音
// app_type = 6 是微信
CMD.get_zh_recharge_template = async function(app_type) {
    try{
        let timestamp = helper.getCurrentUnixTimestamp()
        let url = `https://ecom.zhihu.com/api/km-agent/payment_templates?app_type=${app_type}&app_id=&media_channel_type=5`
        const response =   await fetch(url, {
            "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",
                "priority": "u=1, i",
                "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",
                "x-requested-with": "XMLHttpRequest",
                "cookie": "_xsrf=7bCsoKjmfrIE0D36ajUc9s6CM9OxdPHQ; _zap=aa0b20fb-9951-4f27-9f66-dafff809f1a9; d_c0=ADASSwlwjxmPTrW0768vZ0BKygxzh_LFDDI=|1731902666; __zse_ck=004_Yfd5z9=wVYdipziWJIurZOunLG37mTG2H9iYSlwWRDLk8=MX0olTiRGFlFAjfxcEHEKs3rAZEP/YK23mOi0JdimzivR8x6V8xrTX/SfG2rxez3yjRYZe5TLBDSnO/5x5-qJOwYJOI+PpcVzmlrlnRdtJiOBmlpDaX+iwb4g5wrE5nNLFf5lpJfeqFnUf1/zvSv87a+WPkgCwI5omwr8gRzjTiqCSHq+aDiXaB6KX9RDdi72KjThWCjiJa356jV2Ha; BEC=738c6d0432e7aaf738ea36855cdce904; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1744797535,1747044239; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1747044239; HMACCOUNT=8B23C823A8286BDF; captcha_session_v2=2|1:0|10:1747044240|18:captcha_session_v2|88:WU9XenluNjRDQnRqalZvSmxYU2dNZldqRW1oc1l1aUdUTFh0SmNFWFVOS2E4Zm1ieVVqL0JxTWcwUHB5dng2Qg==|e48d0f7dc867c3386e846e6a046467e209447e0f45de2ec58e40cb5ea89b5024; captcha_ticket_v2=2|1:0|10:1747044266|17:captcha_ticket_v2|728:eyJ2YWxpZGF0ZSI6IkNOMzFfODlzRTFBbml6YmRKcUE1YzFXcHRIOXZ1Q0h4dXBPMXVsQzRNYklwREZpMVlhUkpjY2pseWFEVjR5Y3ZzLktzLnB2NUROQ1Z5LjUxbENrUzQzVFBxTkRqYnlKcWFYYnZRcGM4WGVJQWhQR0dvX3ZIaGlzVldRdzVtelVlcE1sdC5BVTJhYi5VSVM1bWswZUNqdkRlRnRqa1NoWFpjNmdQdUlHVzllX3RWbVFQOG5scmZQUWJBX1JXRWxKTTlBZEgweFpJNkY1U0J2TE52UDlvZlNlY1M1VzhiOGxOcTNrdjA1QzZ1SWhSQTRKQUVQQ2ZOX0tvNHE2RTN6Zk14SklaOVZ6QWtvVS42dE1GbHN3ODJ4b2JGTXlTMXdITGtUNTNpNVJCUUdYWUZ2THdIVTZ3LmNtd3dsX1pGcFJ0VzBfZlRqOU40KjBtcEdEY1JlZVBXbEMwTFlhYlA4bzgxUmxsUWkqZHdKelpKRUdxSnRmKnoqTnhJZl93NlR0Y2RkT3ZvWVVfRFRkVWF3cHl2UlhTcExEb0Q5TldDR0d2cDZxOVJoMU9WNlZXNHdRaEQ0X2Z3U3VPSjlFZipLY3JKTVdvc1o2KkpvVDl6Vms0enVJLkVCLlN0T3JXRm5ZVWduZFlzNW9kY0pCbGRjYkk0Nmd1OUIudlZIQjJIbWdMUHgxQVFiLjk1Q003N192X2lfMSJ9|86552ad9b795608fe66c8628d1a00b37678dfd2c0345979a0ade4da1d9075998; z_c0=2|1:0|10:1747044266|4:z_c0|92:Mi4xWjA2M1hBQUFBQUFBTUJKTENYQ1BHU1lBQUFCZ0FsVk5xaFVQYVFEYmZqdjk5aW90YXVCVWRVaUxjdmcxMlpoNF93|7715e73dbd09a2ff037024e7c3c87c621ee1d97648f3c5a9c82ed50a82e93837; ecom_merchant=1876277363932790784",
                "Referer": "https://ecom.zhihu.com/vip-apps/out-distribution/contentLib/list",
                "Referrer-Policy": "no-referrer-when-downgrade"
            },
            "body": null,
            "method": "GET"
          });
    
          const data =  await response.json();
          console.log('get_zh_recharge_template::响应数据:', data);
          if(data.data!=undefined&&data.data.length<=0){
              throw data
          }
          console.log('get_zh_recharge_template::响应状态:', response.status);
          return data.data;
    }catch(error){
        console.error('get_zh_recharge_template::请求错误:', error);
        return null;
    }

}

CMD.get_zh_recharge_template_new = async function(member_token,app_id) {
    let timestamp = helper.getCurrentUnixTimestamp()
    let postData = {
        "timestamp": `${timestamp}`,
        "app_id": config.zhihu_conifg.app_id,
        "data": {
          "member_token": member_token,//"03-6-34",
          "app_id": app_id, //"tt163b6086890c4d7c01",
          "offset": 0,
          "limit": 100,
        }
    }
    let sign = tools.zh_sign(JSON.stringify(postData),config.zhihu_conifg.app_secret)

    let client = tools.getOneNewClinet({
        "Content-Type":"application/json; charset=utf-8",
        "Accept":"application/json",
        "x-sign":sign
    })

    try{
        let response =  await client.post("https://open.zhihu.com/openapi/km-agent/v1/payment-templates", postData )
        return response.data;
    }catch(e){
        console.error('get_zh_recharge_template::请求错误:', e);
        return null;
    }

}

module.exports = CMD;