-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloud-sync.js
More file actions
592 lines (529 loc) · 21.9 KB
/
cloud-sync.js
File metadata and controls
592 lines (529 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// ============================================
// QQ农场计时器 - 云同步模块(GitHub Gist)
// ============================================
const CloudSync = {
GIST_DESCRIPTION: 'QQFarmTimer',
DATA_FILENAME: 'data.json',
gistId: null,
token: '',
sendKey: '',
// ========== 初始化 ==========
init() {
this.token = localStorage.getItem('farm-gh-token') || '';
this.sendKey = localStorage.getItem('farm-send-key') || '';
this.gistId = localStorage.getItem('farm-gist-id') || '';
this.updateUI();
},
// ========== UI 更新 ==========
updateUI() {
const statusEl = document.getElementById('sync-status');
const tokenInput = document.getElementById('gh-token-input');
const sendKeyInput = document.getElementById('send-key-input');
const tokenStatus = document.getElementById('token-status');
const sendkeyStatus = document.getElementById('sendkey-status');
if (tokenInput && this.token) {
tokenInput.value = this.token;
// 安全显示:只显示前后几位
const masked = this.token.slice(0, 7) + '...' + this.token.slice(-4);
tokenInput.placeholder = masked;
if (tokenStatus) {
tokenStatus.textContent = '✅ 已配置';
tokenStatus.className = 'status-badge connected';
}
} else {
if (tokenInput) tokenInput.value = '';
if (tokenStatus) {
tokenStatus.textContent = '未配置';
tokenStatus.className = 'status-badge';
}
}
if (sendKeyInput && this.sendKey) {
sendKeyInput.value = this.sendKey;
if (sendkeyStatus) {
sendkeyStatus.textContent = '✅ 已配置';
sendkeyStatus.className = 'status-badge connected';
}
} else {
if (sendKeyInput) sendKeyInput.value = '';
if (sendkeyStatus) {
sendkeyStatus.textContent = '未配置';
sendkeyStatus.className = 'status-badge';
}
}
if (statusEl) {
if (this.token && this.gistId) {
statusEl.textContent = '☁️ 已连接云端';
statusEl.className = 'sync-status connected';
} else if (this.token) {
statusEl.textContent = '☁️ 待首次同步';
statusEl.className = 'sync-status connected';
} else {
statusEl.textContent = '☁️ 未连接';
statusEl.className = 'sync-status';
}
}
},
// ========== 保存配置 ==========
saveConfig() {
const tokenEl = document.getElementById('gh-token-input');
const sendKeyEl = document.getElementById('send-key-input');
let changed = false;
if (tokenEl) {
const newToken = tokenEl.value.trim();
if (newToken && newToken !== this.token) {
this.token = newToken;
localStorage.setItem('farm-gh-token', newToken);
this.gistId = '';
localStorage.removeItem('farm-gist-id');
changed = true;
}
}
if (sendKeyEl) {
const newKey = sendKeyEl.value.trim();
if (newKey && newKey !== this.sendKey) {
this.sendKey = newKey;
localStorage.setItem('farm-send-key', newKey);
changed = true;
}
}
this.updateUI();
showToast(changed ? '✅ 配置已保存' : '✅ 配置无变化,已确认');
},
// ========== 配置导出 / 导入 ==========
exportConfig() {
const payload = {
app: 'QQFarmTimer',
version: 1,
exportedAt: new Date().toISOString(),
note: '包含敏感配置,仅供本地迁移使用,请勿上传或分享。',
sync: {
token: this.token || '',
gistId: this.gistId || '',
sendKey: this.sendKey || ''
},
settings: {
...state.settings
}
};
const blob = new Blob([
JSON.stringify(payload, null, 2)
], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
const date = new Date().toISOString().slice(0, 10);
link.href = url;
link.download = `qq-farm-timer-config-${date}.json`;
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
showToast('✅ 已导出本地配置,请妥善保管该文件');
},
openImportConfig() {
const input = document.getElementById('config-import-input');
if (!input) {
showToast('❌ 当前页面缺少导入入口');
return;
}
input.value = '';
input.click();
},
async importConfigFromFile(event) {
const file = event?.target?.files?.[0];
if (!file) return;
try {
const text = await file.text();
const parsed = JSON.parse(text);
const sync = parsed?.sync || {};
const settings = parsed?.settings || {};
const hasToken = typeof sync.token === 'string' && sync.token.trim().length > 0;
const hasSendKey = typeof sync.sendKey === 'string' && sync.sendKey.trim().length > 0;
const settingCount = Object.keys(settings).filter(key => settings[key] !== undefined).length;
if ((!parsed || typeof parsed !== 'object') || (!settingCount && !hasToken && !hasSendKey && !sync.gistId)) {
throw new Error('文件中没有可导入的配置');
}
const summary = [
'将覆盖当前设备上的本地配置。',
`<br><span>提醒设置:${settingCount || 0}项</span>`,
`<br><span>GitHub Token:${hasToken ? '包含' : '不包含'}</span>`,
`<br><span>Server酱 SendKey:${hasSendKey ? '包含' : '不包含'}</span>`
].join('');
showConfirm('导入本地配置', summary, () => this.applyImportedConfig(parsed));
} catch (error) {
showToast(`❌ 导入失败:${error.message || '文件格式不正确'}`);
}
},
applyImportedConfig(parsed) {
const sync = parsed?.sync || {};
const nextSettings = parsed?.settings || {};
const normalize = value => typeof value === 'string' ? value.trim() : '';
this.token = normalize(sync.token);
this.gistId = normalize(sync.gistId);
this.sendKey = normalize(sync.sendKey);
if (this.token) {
localStorage.setItem('farm-gh-token', this.token);
} else {
localStorage.removeItem('farm-gh-token');
}
if (this.gistId) {
localStorage.setItem('farm-gist-id', this.gistId);
} else {
localStorage.removeItem('farm-gist-id');
}
if (this.sendKey) {
localStorage.setItem('farm-send-key', this.sendKey);
} else {
localStorage.removeItem('farm-send-key');
}
state.settings = {
...state.settings,
...nextSettings
};
localStorage.setItem('farm-timer-state', JSON.stringify({
alerts: state.alerts.map(a => ({ ...a })),
history: state.history.slice(-200),
settings: state.settings
}));
if (typeof refreshSettingsForm === 'function') {
refreshSettingsForm();
}
this.updateUI();
if (typeof updateSyncStatusBar === 'function') {
updateSyncStatusBar();
}
showToast('✅ 本地配置已导入;敏感信息仍只保存在当前设备');
},
// ========== GitHub API 请求 ==========
async apiCall(path, method = 'GET', data = null) {
if (!this.token) {
throw new Error('未配置 GitHub Token');
}
const options = {
method,
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json',
}
};
if (data) {
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(data);
}
const resp = await fetch(`https://api.github.com${path}`, options);
if (resp.status === 401) {
throw new Error('Token 无效或已过期');
}
if (resp.status === 403) {
throw new Error('API 请求次数超限');
}
if (!resp.ok) {
const err = await resp.json().catch(() => ({}));
throw new Error(err.message || `请求失败 (${resp.status})`);
}
return resp.json();
},
// ========== 查找或创建 Gist ==========
async ensureGist() {
if (this.gistId) return this.gistId;
// 查找现有 Gist
try {
const gists = await this.apiCall('/gists');
for (const g of gists) {
if (g.description === this.GIST_DESCRIPTION) {
this.gistId = g.id;
localStorage.setItem('farm-gist-id', g.id);
return g.id;
}
}
} catch (e) {
console.warn('查找 Gist 失败:', e);
}
// 创建新 Gist
try {
const emptyData = {
description: this.GIST_DESCRIPTION,
public: false,
files: {
[this.DATA_FILENAME]: {
content: JSON.stringify({ alerts: [], history: [] })
}
}
};
const gist = await this.apiCall('/gists', 'POST', emptyData);
this.gistId = gist.id;
localStorage.setItem('farm-gist-id', gist.id);
return gist.id;
} catch (e) {
console.error('创建 Gist 失败:', e);
throw e;
}
},
// ========== 上传数据到云端 ==========
async pushAlarms(alarms) {
if (!this.token) return false;
try {
const gistId = await this.ensureGist();
// 收集自定义植物数据
const customPlants = Object.values(PLANTS_DATABASE).filter(p => p.isCustom);
const currentFarmLevel = parseInt(localStorage.getItem('farm-analysis-level')) || 1;
const payload = {
alerts: alarms,
history: state.history.slice(-200),
customPlants: customPlants,
settings: state.settings,
farmLevel: currentFarmLevel
};
console.log('[云同步] 推送农场等级:', currentFarmLevel);
const data = {
files: {
[this.DATA_FILENAME]: {
content: JSON.stringify(payload)
}
}
};
await this.apiCall(`/gists/${gistId}`, 'PATCH', data);
this.updateUI();
return true;
} catch (e) {
console.error('上传数据失败:', e);
showToast('❌ 云同步失败: ' + e.message);
return false;
}
},
// ========== 从云端拉取数据 ==========
async pullAlarms() {
if (!this.token) return null;
try {
const gistId = await this.ensureGist();
const gist = await this.apiCall(`/gists/${gistId}`);
const files = gist.files || {};
// 兼容旧版 alarms.json 和新版 data.json
const dataFile = files[this.DATA_FILENAME] || files['alarms.json'];
if (dataFile) {
const content = dataFile.content;
const parsed = JSON.parse(content);
console.log('[云同步] 拉取到的数据:', { farmLevel: parsed.farmLevel });
// 新版返回 { alerts, history, customPlants, settings },旧版返回纯数组
if (Array.isArray(parsed)) {
return { alerts: parsed, history: [], customPlants: [], settings: null };
}
return {
alerts: parsed.alerts || [],
history: parsed.history || [],
customPlants: parsed.customPlants || [],
settings: parsed.settings || null,
farmLevel: parsed.farmLevel || 1
};
}
return { alerts: [], history: [], customPlants: [], settings: null };
} catch (e) {
console.error('拉取数据失败:', e);
showToast('❌ 云同步失败: ' + e.message);
return null;
}
},
async sendServerChanMessage(title, desp) {
if (!this.sendKey) {
return { ok: false, message: '请先配置 Server酱 SendKey' };
}
try {
const resp = await fetch(
`https://sctapi.ftqq.com/${this.sendKey}.send`,
{
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `title=${encodeURIComponent(title)}&desp=${encodeURIComponent(desp)}`
}
);
const result = await resp.json();
if (result.code === 0) {
return { ok: true, result };
}
return { ok: false, message: result.message || '未知错误', result };
} catch (e) {
return { ok: false, message: e.message || '请求失败' };
}
},
async sendAlarmPush(alarm, options = {}) {
const label = alarm?.label || '定时器';
const plantName = alarm?.plant || '';
const triggeredAt = options.triggeredAt ? new Date(options.triggeredAt) : new Date();
const timeText = Number.isNaN(triggeredAt.getTime())
? new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
: triggeredAt.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
const message = options.message || (plantName
? `${plantName}成熟了!快去收菜!`
: `${label} 已到时间。`);
const desp = [
`**${label}**`,
'',
message,
'',
`触发时间:${timeText}`,
plantName ? `作物:${plantName}` : null
].filter(Boolean).join('\n');
if (!this.sendKey) {
return {
ok: false,
reason: 'missing-sendkey',
message: '当前设备未配置 Server酱 SendKey;若仓库 Secrets 已配置,GitHub Actions 仍会尝试补发'
};
}
const result = await this.sendServerChanMessage('🌾 农场收菜提醒', desp);
return {
ok: result.ok,
reason: result.ok ? null : 'request-failed',
message: result.ok ? '' : (result.message || '微信推送发送失败'),
result: result.result || null,
notifiedAt: result.ok ? new Date().toISOString() : null
};
},
// ========== 测试推送 ==========
async testPush() {
const result = await this.sendServerChanMessage('🌾 农场计时器测试', '推送功能正常!你将收到这条测试消息。');
if (result.ok) {
showToast('✅ 推送测试成功!请检查微信');
return true;
}
showToast('❌ 推送失败: ' + result.message);
return false;
},
// ========== 手动同步 ==========
async syncNow() {
if (!this.token) {
showToast('❌ 请先配置 GitHub Token');
if (typeof openSettingsPanel === 'function') {
openSettingsPanel({
targetId: 'token-setting-item',
focusId: 'gh-token-input'
});
}
return;
}
showToast('🔄 正在同步...');
try {
// 先拉取云端数据
const cloudData = await this.pullAlarms();
if (cloudData === null) return;
const cloudAlarms = cloudData.alerts || [];
const cloudHistory = cloudData.history || [];
const cloudCustomPlants = cloudData.customPlants || [];
const cloudSettings = cloudData.settings || null;
// 合并策略:以云端为准,但保留本地运行中的定时器
const now = new Date();
// 先把本地已过期闹钟归档到历史,避免同步时直接丢失
if (typeof cleanupExpiredAlerts === 'function') {
cleanupExpiredAlerts({ save: false, render: false });
} else {
state.alerts = state.alerts.filter(a => {
if (state.timers[a.id]) return true;
return new Date(a.endTime) > now;
});
}
// 先补齐云端中过期但尚未入历史的闹钟
if (typeof archiveAlertToHistory === 'function') {
cloudAlarms.forEach(alarm => {
if (!alarm?.endTime) return;
if (new Date(alarm.endTime) <= now) {
archiveAlertToHistory(alarm, alarm.endTime);
}
});
}
// 用云端数据替换本地(仅未过期的)
const activeCloudAlarms = cloudAlarms.filter(a => {
if (a.pushNotified) return false;
return new Date(a.endTime) > now;
});
// 合并闹钟:云端 + 本地运行中的
activeCloudAlarms.forEach(a => {
if (!state.timers[a.id]) {
state.timers[a.id] = a;
}
});
state.alerts = [...activeCloudAlarms];
// 合并历史记录:本地 + 云端,按 ID 去重,取最新200条
const existingHistoryIds = new Set(state.history.map(h => h.id));
cloudHistory.forEach(h => {
if (!existingHistoryIds.has(h.id)) {
state.history.push(h);
existingHistoryIds.add(h.id);
}
});
if (typeof sortHistoryItems === 'function') {
state.history = sortHistoryItems(state.history).slice(-200);
} else {
state.history = state.history.slice(-200);
}
// 合并自定义植物:云端 + 本地,按名称去重
let customPlantMerged = false;
if (cloudCustomPlants.length > 0) {
cloudCustomPlants.forEach(p => {
if (p.name && !PLANTS_DATABASE[p.name]) {
PLANTS_DATABASE[p.name] = typeof normalizePlantRecord === 'function'
? normalizePlantRecord({ ...p, isCustom: true }, { isCustom: true })
: {
...p,
isCustom: true,
growthTime: p.firstTime || p.growthTime || 0
};
customPlantMerged = true;
}
});
// 保存合并后的自定义植物
if (typeof saveCustomPlants === 'function') saveCustomPlants();
}
// 合并设置:以云端的为准(如果云端有的话)
if (cloudSettings) {
state.settings = { ...state.settings, ...cloudSettings };
if (typeof refreshSettingsForm === 'function') {
refreshSettingsForm();
}
}
// 恢复农场等级
if (cloudData.farmLevel) {
const farmLevel = parseInt(cloudData.farmLevel) || 1;
localStorage.setItem('farm-analysis-level', farmLevel);
analysisState.farmLevel = farmLevel;
const farmLevelInput = document.getElementById('farm-level-input');
if (farmLevelInput) {
farmLevelInput.value = farmLevel;
}
console.log('[云同步] 恢复农场等级:', farmLevel);
// 重新计算效率页面(使用新的农场等级)
if (typeof calculateEfficiency === 'function') {
calculateEfficiency();
}
} else {
console.log('[云同步] 云端数据中无农场等级字段');
}
// 推送本地数据到云端(包含合并后的结果)
await this.pushAlarms(state.alerts);
saveState();
renderRunningTimers();
renderAlertsList();
renderHistoryList();
if (customPlantMerged) {
renderPlantGrid(document.getElementById('plant-search-input')?.value || '');
if (typeof renderCustomPlantsList === 'function') renderCustomPlantsList();
}
this.updateUI();
const parts = [`${state.alerts.length}个闹钟`, `${state.history.length}条历史`];
const customCount = Object.values(PLANTS_DATABASE).filter(p => p.isCustom).length;
if (customCount > 0) parts.push(`${customCount}个自定义植物`);
showToast(`✅ 同步完成!${parts.join(',')}`);
} finally {
// 移除了顶部同步按钮动画,syncNow()已被移至设置面板
}
},
// ========== 断开连接 ==========
disconnect() {
this.token = '';
this.gistId = '';
localStorage.removeItem('farm-gh-token');
localStorage.removeItem('farm-gist-id');
const tokenInput = document.getElementById('gh-token-input');
if (tokenInput) tokenInput.value = '';
this.updateUI();
showToast('☁️ 已断开云同步');
}
};