<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>创作者中心扫码登录</title>
<script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.1/build/qrcode.min.js"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0;font-family:system-ui}
body{background:#f5f5f7;padding:20px;max-width:500px;margin:0 auto}
.card{background:#fff;border-radius:16px;padding:20px;margin-bottom:15px;box-shadow:0 2px 10px #00000010}
h2{font-size:18px;text-align:center;margin-bottom:15px}
#qrcode{text-align:center;margin:10px 0}
#qrcode canvas{width:180px;height:180px}
.tip{padding:10px;border-radius:8px;margin:10px 0;font-size:14px}
.wait{background:#e8f4ff;color:#1967d2}
.ok{background:#e8f5e9;color:#2e7d32}
.err{background:#ffebee;color:#c62828}
.friend{display:flex;align-items:center;padding:10px;border-bottom:1px solid #f2f2f2;cursor:pointer}
.friend img{width:40px;height:40px;border-radius:50%;margin-right:10px}
input,textarea{width:100%;padding:10px;margin:6px 0;border:1px solid #ddd;border-radius:8px}
button{width:100%;padding:11px;background:#fe2c55;color:#fff;border:none;border-radius:8px}
</style>
</head>
<body>

<div class="card">
  <h2>📱 创作者中心扫码登录</h2>
  <div id="qrcode"></div>
  <div id="status" class="tip wait">加载二维码...</div>
</div>

<div class="card" id="friendBox" style="display:none">
  <h2>👥 好友列表</h2>
  <div id="friendList"></div>
</div>

<div class="card" id="sendBox" style="display:none">
  <h2>✉️ 发送私信</h2>
  <input id="touid" readonly placeholder="点击好友自动填入">
  <textarea id="msg" placeholder="消息内容"></textarea>
  <button onclick="sendMsg()">发送</button>
  <div id="resTip" class="tip" style="display:none"></div>
</div>

<script>
let qrKey = ""
let cookie = ""
let csrf = ""

async function getQR() {
  try {
    let r = await fetch("?act=getqr")
    let t = await r.text()
    let d = JSON.parse(t)
    if (d.data?.qrcode_url) {
      qrKey = d.data.qrcode_key
      QRCode.toCanvas(document.getElementById("qrcode"), d.data.qrcode_url, { width: 180 })
      document.getElementById("status").innerText = "请用抖音扫码登录创作者中心"
      check()
    }
  } catch (e) {
    document.getElementById("status").innerText = "二维码加载失败"
  }
}

async function check() {
  let t = setInterval(async () => {
    try {
      let r = await fetch("?act=checkqr&qrkey=" + qrKey)
      let d = JSON.parse(await r.text())
      if (d.data?.status === 3) {
        clearInterval(t)
        cookie = d.data.cookie
        let m = cookie.match(/passport_csrf_token=([^;]+)/)
        csrf = m ? m[1] : ""
        document.getElementById("status").className = "tip ok"
        document.getElementById("status").innerText = "✅ 登录成功"
        loadFriends()
      }
    } catch (e) {}
  }, 1500)
}

async function loadFriends() {
  let r = await fetch("?act=friend&cookie=" + encodeURIComponent(cookie))
  let d = JSON.parse(await r.text())
  let list = document.getElementById("friendList")
  d.aweme_list.forEach(u => {
    let item = document.createElement("div")
    item.className = "friend"
    item.innerHTML = `
      <img src="${u.avatar_168x168.url_list[0]}">
      <div>
        <div>${u.nickname}</div>
        <div style="font-size:12px;color:#888">ID: ${u.uid}</div>
      </div>`
    item.onclick = () => document.getElementById("touid").value = u.uid
    list.appendChild(item)
  })
  document.getElementById("friendBox").style.display = "block"
  document.getElementById("sendBox").style.display = "block"
}

async function sendMsg() {
  let touid = document.getElementById("touid").value
  let msg = document.getElementById("msg").value
  let tip = document.getElementById("resTip")

  if (!touid || !msg) {
    tip.className = "tip err"
    tip.innerText = "请选择好友并输入内容"
    tip.style.display = "block"
    return
  }

  tip.className = "tip wait"
  tip.innerText = "发送中..."
  tip.style.display = "block"

  let res = await fetch("?act=send", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ cookie, csrf, touid, msg })
  })

  let ret = JSON.parse(await res.text())
  tip.className = ret.status_code === 0 ? "tip ok" : "tip err"
  tip.innerText = ret.status_code === 0 ? "✅ 发送成功" : "❌ 发送失败"
}

getQR()
</script>
</body>
</html>
