@deepwhale-cn/dsh-douyin-desk 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +76 -4
- package/lib/index.js +85 -0
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -65,6 +65,11 @@ window.__ModuleLoader__.load({
|
|
|
65
65
|
.dwh-dy-hours{display:flex;gap:2px;align-items:flex-end;height:52px}
|
|
66
66
|
.dwh-dy-hour{flex:1;background:var(--dsw-alias-border-l2);border-radius:2px 2px 0 0;min-height:1px}
|
|
67
67
|
.dwh-dy-hour.hi{background:#2563eb}
|
|
68
|
+
.dwh-dy-modal{position:fixed;inset:0;background:rgba(0,0,0,.55);display:flex;align-items:center;justify-content:center;z-index:80}
|
|
69
|
+
.dwh-dy-modal .box{background:var(--dsw-alias-bg-base);border:.5px solid var(--dsw-alias-border-l2);border-radius:16px;padding:20px;width:340px;text-align:center}
|
|
70
|
+
.dwh-dy-modal img{width:240px;height:240px;border-radius:10px;background:#fff;margin:10px 0}
|
|
71
|
+
.dwh-dy-modal .tip{font-size:12px;color:var(--dsw-alias-label-secondary);line-height:1.7}
|
|
72
|
+
.dwh-dy-modal .acts{display:flex;gap:8px;justify-content:center;margin-top:12px}
|
|
68
73
|
.dwh-dy-ft{padding:10px 18px 14px;border-top:.5px solid var(--dsw-alias-border-l1);color:var(--dsw-alias-label-secondary);font-size:11.5px;line-height:1.7;display:flex;align-items:flex-end;justify-content:space-between;gap:12px;flex-wrap:wrap}
|
|
69
74
|
.dwh-dy-ft img{height:16px;display:block;margin-top:5px;opacity:.85}
|
|
70
75
|
`;
|
|
@@ -142,6 +147,7 @@ window.__ModuleLoader__.load({
|
|
|
142
147
|
<div class="dwh-dy-card">
|
|
143
148
|
<div class="dwh-dy-hd">
|
|
144
149
|
<b>深鲸抖音复盘</b><small class="st">正在读取…</small><span class="sp"></span>
|
|
150
|
+
<button class="dwh-dy-btn primary" data-act="login" style="display:none">扫码授权</button>
|
|
145
151
|
<button class="dwh-dy-btn" data-act="csv">导出 CSV</button>
|
|
146
152
|
<button class="dwh-dy-btn" data-act="report">生成周报</button>
|
|
147
153
|
<button class="dwh-dy-btn primary" data-act="sync">同步作品</button>
|
|
@@ -162,6 +168,18 @@ window.__ModuleLoader__.load({
|
|
|
162
168
|
<div>只读:不发布、不评论、不点赞;授权 1 天有效,过期重新扫码。<br>官方接口不含播放量,这里也不编一个。</div>
|
|
163
169
|
<img src="https://www.deepwhale.org.cn/images/badge/deepwhale-badge.png" alt="powered by dwh">
|
|
164
170
|
</div>
|
|
171
|
+
</div>
|
|
172
|
+
<div class="dwh-dy-modal" style="display:none">
|
|
173
|
+
<div class="box">
|
|
174
|
+
<b style="font-size:14px">扫码授权抖音</b>
|
|
175
|
+
<div class="tip" data-role="modal-tip">用抖音 App 扫描下方二维码,确认授权(有效期 1 天)</div>
|
|
176
|
+
<img alt="登录二维码" data-role="qr">
|
|
177
|
+
<div class="acts">
|
|
178
|
+
<button class="dwh-dy-btn primary" data-act="login-complete">已扫码,完成授权</button>
|
|
179
|
+
<button class="dwh-dy-btn" data-act="login-cancel">取消</button>
|
|
180
|
+
</div>
|
|
181
|
+
<div class="tip" data-role="login-msg" style="margin-top:8px"></div>
|
|
182
|
+
</div>
|
|
165
183
|
</div>`;
|
|
166
184
|
host.appendChild(root);
|
|
167
185
|
|
|
@@ -538,12 +556,63 @@ window.__ModuleLoader__.load({
|
|
|
538
556
|
else renderFav();
|
|
539
557
|
}
|
|
540
558
|
|
|
559
|
+
const modal = root.querySelector('.dwh-dy-modal');
|
|
560
|
+
const qrImg = root.querySelector('[data-role="qr"]');
|
|
561
|
+
const modalTip = root.querySelector('[data-role="modal-tip"]');
|
|
562
|
+
const loginMsg = root.querySelector('[data-role="login-msg"]');
|
|
563
|
+
|
|
564
|
+
function showLoginButton(show) {
|
|
565
|
+
const btn = root.querySelector('[data-act="login"]');
|
|
566
|
+
if (btn) btn.style.display = show ? '' : 'none';
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
async function beginLogin() {
|
|
570
|
+
loginMsg.textContent = '';
|
|
571
|
+
modalTip.textContent = '用抖音 App 扫描下方二维码,确认授权(有效期 1 天)';
|
|
572
|
+
try {
|
|
573
|
+
const res = await postJson('/douyin-desk/login/begin', {});
|
|
574
|
+
if (!res.ok || !res.qrDataUrl) throw new Error(res.error || '没有拿到二维码');
|
|
575
|
+
qrImg.src = res.qrDataUrl;
|
|
576
|
+
modal.style.display = 'flex';
|
|
577
|
+
} catch (error) {
|
|
578
|
+
setMsg(`发起授权失败:${error instanceof Error ? error.message : String(error)}`, 'err');
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
async function completeLogin() {
|
|
583
|
+
loginMsg.textContent = '正在确认…';
|
|
584
|
+
try {
|
|
585
|
+
const res = await postJson('/douyin-desk/login/complete', {});
|
|
586
|
+
if (res.ok) {
|
|
587
|
+
modal.style.display = 'none';
|
|
588
|
+
setMsg('授权成功,正在同步数据…', 'ok');
|
|
589
|
+
await refreshAll();
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (res.pending) {
|
|
593
|
+
loginMsg.textContent = `还没扫到,${String(res.retryAfterSeconds ?? 3)} 秒后再点一次`;
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
loginMsg.textContent = `授权未完成:${res.error ?? '请重试'}`;
|
|
597
|
+
} catch (error) {
|
|
598
|
+
loginMsg.textContent = `确认失败:${error instanceof Error ? error.message : String(error)}`;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
541
602
|
async function loadState() {
|
|
542
603
|
try {
|
|
543
604
|
const data = await getJson('/douyin-desk/state');
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
605
|
+
const st = data.status ?? {};
|
|
606
|
+
if (st.installed === false) {
|
|
607
|
+
stEl.textContent = '未安装 douyin-open-cli:先把官方 CLI 装上,再回来扫码';
|
|
608
|
+
showLoginButton(false);
|
|
609
|
+
} else if (st.needsLogin) {
|
|
610
|
+
stEl.textContent = '未授权:点右侧「扫码授权」用抖音扫码';
|
|
611
|
+
showLoginButton(true);
|
|
612
|
+
} else {
|
|
613
|
+
stEl.textContent = `${data.stateLine}${data.updatedAt ? ` · 上次同步 ${new Date(data.updatedAt).toLocaleString()}` : ''}`;
|
|
614
|
+
showLoginButton(false);
|
|
615
|
+
}
|
|
547
616
|
} catch (error) {
|
|
548
617
|
stEl.textContent = '状态读取失败';
|
|
549
618
|
setMsg(`状态读取失败:${error instanceof Error ? error.message : String(error)}`, 'err');
|
|
@@ -587,7 +656,7 @@ window.__ModuleLoader__.load({
|
|
|
587
656
|
setMsg(`同步完成:本地共 ${String(res.synced)} 条作品。`, 'ok');
|
|
588
657
|
await refreshAll();
|
|
589
658
|
} catch (error) {
|
|
590
|
-
if (error && error.needLogin) setMsg('
|
|
659
|
+
if (error && error.needLogin) { setMsg('授权过期:点右上角「扫码授权」重新扫码。', 'err'); showLoginButton(true); }
|
|
591
660
|
else setMsg(`同步失败:${error instanceof Error ? error.message : String(error)}`, 'err');
|
|
592
661
|
}
|
|
593
662
|
btn.disabled = false;
|
|
@@ -628,6 +697,9 @@ window.__ModuleLoader__.load({
|
|
|
628
697
|
}
|
|
629
698
|
}
|
|
630
699
|
|
|
700
|
+
root.querySelector('[data-act="login"]')?.addEventListener('click', () => { void beginLogin(); });
|
|
701
|
+
root.querySelector('[data-act="login-complete"]')?.addEventListener('click', () => { void completeLogin(); });
|
|
702
|
+
root.querySelector('[data-act="login-cancel"]')?.addEventListener('click', () => { modal.style.display = 'none'; });
|
|
631
703
|
root.querySelector('[data-act="sync"]').addEventListener('click', () => { void sync(); });
|
|
632
704
|
root.querySelector('[data-act="report"]').addEventListener('click', () => { void report(); });
|
|
633
705
|
root.querySelector('[data-act="csv"]').addEventListener('click', () => { void csv(); });
|
package/lib/index.js
CHANGED
|
@@ -14,6 +14,7 @@ import { analyze, buildIdeas, buildPublishKit, buildReport, cadence, checkCopy,
|
|
|
14
14
|
import {
|
|
15
15
|
STORE_DIR,
|
|
16
16
|
fetchText,
|
|
17
|
+
resolveCli,
|
|
17
18
|
loadFavorites,
|
|
18
19
|
loadHistory,
|
|
19
20
|
loadWorks,
|
|
@@ -23,6 +24,8 @@ import {
|
|
|
23
24
|
syncWorks,
|
|
24
25
|
} from './ingest.js';
|
|
25
26
|
import path from 'node:path';
|
|
27
|
+
import { readFileSync } from 'node:fs';
|
|
28
|
+
import { execFile } from 'node:child_process';
|
|
26
29
|
|
|
27
30
|
export const name = 'douyin-desk';
|
|
28
31
|
export const inject = ['tools', 'webServer', 'llm'];
|
|
@@ -95,6 +98,56 @@ async function rewriteText(ctx, { mode, text, idea }) {
|
|
|
95
98
|
return { ok: true, mode, text: out, provider: MODEL.provider, model: MODEL.model };
|
|
96
99
|
}
|
|
97
100
|
|
|
101
|
+
/** 跑 CLI 拿原始输出(登录二维码信息只有文本,不是 JSON)。 */
|
|
102
|
+
function runCliRaw(args, { timeout = 60_000 } = {}) {
|
|
103
|
+
const bin = resolveCli();
|
|
104
|
+
if (!bin) return Promise.resolve({ ok: false, error: '未找到 douyin-open-cli' });
|
|
105
|
+
return new Promise((resolve) => {
|
|
106
|
+
execFile(bin, args, { timeout, maxBuffer: 8 * 1024 * 1024, env: { ...process.env, PATH: `${path.join(process.env.HOME ?? '', '.local', 'bin')}:${process.env.PATH ?? ''}` } }, (error, stdout, stderr) => {
|
|
107
|
+
resolve({ ok: !error, stdout: String(stdout ?? ''), stderr: String(stderr ?? ''), error: error ? String(stderr || error.message) : '' });
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 发起扫码授权:返回二维码的 data URL,面板直接 <img> 显示。 */
|
|
113
|
+
async function loginBegin() {
|
|
114
|
+
const res = await runCliRaw(['auth', 'login', '--begin'], { timeout: 90_000 });
|
|
115
|
+
const text = `${res.stdout}\n${res.stderr}`;
|
|
116
|
+
// 已登录时 CLI 直接返回 JSON(没有二维码),告诉面板不用扫
|
|
117
|
+
try {
|
|
118
|
+
const j = JSON.parse(res.stdout);
|
|
119
|
+
if (j?.data?.logged_in === true) return { ok: true, alreadyLoggedIn: true };
|
|
120
|
+
} catch {
|
|
121
|
+
// 不是 JSON 就继续按二维码解析
|
|
122
|
+
}
|
|
123
|
+
const m = text.match(/QR image:\s*(\S+)/i) ?? text.match(/qr_image_path["\s:]+([^\s",}]+)/i);
|
|
124
|
+
if (!m) {
|
|
125
|
+
return { ok: false, error: res.error || '没有拿到二维码路径', raw: text.slice(0, 300) };
|
|
126
|
+
}
|
|
127
|
+
const qrPath = m[1].trim();
|
|
128
|
+
try {
|
|
129
|
+
const buf = readFileSync(qrPath);
|
|
130
|
+
return { ok: true, qrDataUrl: `data:image/png;base64,${buf.toString('base64')}`, qrPath };
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return { ok: false, error: `二维码文件读不到:${error instanceof Error ? error.message : String(error)}` };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 用户扫完码后确认;仍 pending 时告诉面板继续等。 */
|
|
137
|
+
async function loginComplete() {
|
|
138
|
+
const res = await runCliRaw(['auth', 'login', '--complete'], { timeout: 60_000 });
|
|
139
|
+
let json;
|
|
140
|
+
try {
|
|
141
|
+
json = JSON.parse(res.stdout);
|
|
142
|
+
} catch {
|
|
143
|
+
return { ok: false, error: res.error || '授权返回不是 JSON', raw: res.stdout.slice(0, 200) };
|
|
144
|
+
}
|
|
145
|
+
const data = json?.data ?? {};
|
|
146
|
+
if (data.status === 'success' || data.logged_in === true) return { ok: true, loggedIn: true };
|
|
147
|
+
if (data.status === 'pending') return { ok: false, pending: true, retryAfterSeconds: Number(data.retry_after_seconds ?? 3) };
|
|
148
|
+
return { ok: false, error: data.reason ?? data.status ?? '授权未完成' };
|
|
149
|
+
}
|
|
150
|
+
|
|
98
151
|
export function apply(ctx, config) {
|
|
99
152
|
if (config && typeof config === 'object') {
|
|
100
153
|
if (typeof config.provider === 'string' && config.provider) MODEL.provider = config.provider;
|
|
@@ -390,6 +443,38 @@ export function apply(ctx, config) {
|
|
|
390
443
|
},
|
|
391
444
|
}));
|
|
392
445
|
|
|
446
|
+
ctx.effect(() => ctx.webServer.register({
|
|
447
|
+
kind: 'exact',
|
|
448
|
+
path: '/douyin-desk/login/begin',
|
|
449
|
+
handler: async (req, res) => {
|
|
450
|
+
if (req.method !== 'POST') {
|
|
451
|
+
sendJson(res, 405, { ok: false, error: '只接受 POST' });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
sendJson(res, 200, await loginBegin());
|
|
456
|
+
} catch (error) {
|
|
457
|
+
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
}));
|
|
461
|
+
|
|
462
|
+
ctx.effect(() => ctx.webServer.register({
|
|
463
|
+
kind: 'exact',
|
|
464
|
+
path: '/douyin-desk/login/complete',
|
|
465
|
+
handler: async (req, res) => {
|
|
466
|
+
if (req.method !== 'POST') {
|
|
467
|
+
sendJson(res, 405, { ok: false, error: '只接受 POST' });
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
sendJson(res, 200, await loginComplete());
|
|
472
|
+
} catch (error) {
|
|
473
|
+
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
}));
|
|
477
|
+
|
|
393
478
|
ctx.effect(() => ctx.webServer.register({
|
|
394
479
|
kind: 'exact',
|
|
395
480
|
path: '/douyin-desk/favorites',
|