// # Source: https://github.com/Wenmoux/checkbox/blob/master/scripts/cg163.js
// # Raw: https://raw.githubusercontent.com/Wenmoux/checkbox/master/scripts/cg163.js
// # Repo: Wenmoux/checkbox
// # Path: scripts/cg163.js
// # UploadedAt: 2021-09-16T02:16:27Z
// # SHA256: 71024025bbb27a426f7d5e1899ada14ca39b718b0b04918f478fd8b0deda6c7e
// # Category: web版/抓包
// # Evidence: web/H5关键词 + cookie/token/header
// 

const headers = {
  headers: {
    Authorization:
      config.cg163.Authorization || " bearer xxxxx",
    "user-agent":
      "Mozilla/5.0 (Linux; Android 10; Redmi K30 Build/QKQ1.190825.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/85.0.4183.127 Mobile Safari/537.36",
    //                "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
  },
};
const axios = require("axios")
function check() {
  return new Promise(async (resolve) => {
    try {
      const url = `https://n.cg.163.com/api/v2/users/@me`;
      let res = await axios.get(url, headers);
      console.log("cookie未失效,即将开始签到...");
      ckstatus = 1;
    } catch (err) {
       console.log(err)
      console.log("cookie已失效");
      ckstatus = 0;
    }
    resolve();
  });
}

function sign() {
  return new Promise(async (resolve) => {
    try {
      const url = `https://n.cg.163.com/api/v2/sign-today`;
      let res = await axios.post(url, "", headers);
      console.log("签到成功");
      msg = "签到成功！！ ";
    } catch (err) {
      //   console.log(err)
      msg = "签到失败,已签到过或其它未知原因！！ ";
      console.log(msg);
    }
    resolve("【网易云游戏】："+msg);
  });
}
async function cg163() {
  ckstatus = 0;
  await check();
  if (ckstatus == 1) {
    return await sign();
  } else {
    console.log("cookie失效,请重新抓取cookies...");
    return "【网易云游戏】: cookie失效,请重新抓取cookies...";
  }
}
//cg163()
module.exports = cg163;
