v1.3.0 — 钉钉日历同步

- 早间立志卡片新增日历同步按钮
- /api/calendar-sync 从缓存读取当日钉钉会议
- 自动填入立志:去重防重复,保留时间+地点格式
- 日历缓存: ~/.workbuddy/data/ziwei-power/calendar_cache.json
This commit is contained in:
mac
2026-06-04 12:40:31 +08:00
parent 46862e5110
commit c42d396987
4 changed files with 83 additions and 0 deletions

21
app.py
View File

@@ -225,6 +225,27 @@ def api_reorder_wishes():
return jsonify({'ok': True})
# ── 日历同步 API ────────────────────────────
CALENDAR_CACHE = os.path.join(os.path.expanduser('~'), '.workbuddy', 'data', 'ziwei-power', 'calendar_cache.json')
@app.route('/api/calendar-sync', methods=['GET'])
@login_required
def api_calendar_sync():
"""获取指定日期的钉钉日历会议"""
import json as _json
date = request.args.get('date', '')
if not date:
return jsonify({'ok': False, 'error': '缺少 date 参数'}), 400
try:
with open(CALENDAR_CACHE, 'r') as f:
events = _json.load(f)
except (FileNotFoundError, _json.JSONDecodeError):
return jsonify({'ok': True, 'data': []})
today_events = [e for e in events if e.get('date') == date]
return jsonify({'ok': True, 'data': today_events})
# ── 启动 ──────────────────────────────────────────────
if __name__ == '__main__':

View File

@@ -362,6 +362,45 @@
}
}
/* ── 日历同步 ── */
window.syncCalendar = function() {
var date = document.getElementById('check-date').value;
var btn = document.querySelector('.btn-cal-sync');
if (btn) { btn.style.opacity = '0.5'; btn.disabled = true; }
fetch('/api/calendar-sync?date=' + encodeURIComponent(date))
.then(function(r){ return r.json(); })
.then(function(res){
if (btn) { btn.style.opacity = '1'; btn.disabled = false; }
if (!res.ok) { showToast(res.error || '同步失败', 'error'); return; }
var events = res.data || [];
if (events.length === 0) { showToast('今日无日程', 'info'); return; }
var added = 0;
events.forEach(function(e) {
var text = '【' + e.time + '】' + e.summary;
if (e.location) text += ' @' + e.location;
var existing = document.querySelectorAll('#morning-list input[type="text"]');
var already = false;
existing.forEach(function(inp){ if (inp.value.indexOf(e.summary) >= 0) already = true; });
if (already) return;
var count = document.querySelectorAll('#morning-list .item-row').length;
if (count >= 3) return;
addMorningRow(text);
added++;
});
if (added > 0) {
showToast('已同步 ' + added + ' 条日程', 'info');
triggerAutoSave();
} else {
showToast('日程已存在或已满', 'info');
}
})
.catch(function(){
if (btn) { btn.style.opacity = '1'; btn.disabled = false; }
showToast('同步失败', 'error');
});
};
window.addMorning = function() {
var count = document.querySelectorAll('#morning-list .item-row').length;
if (count >= 3) { showToast('最多 3 条立志', 'error'); return; }

View File

@@ -535,6 +535,26 @@ body {
color: var(--primary);
}
/* 日历同步按钮 */
.btn-cal-sync {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border: none;
background: transparent;
color: var(--text-muted);
border-radius: 6px;
cursor: pointer;
transition: all 0.2s;
flex-shrink: 0;
}
.btn-cal-sync:hover {
background: var(--success-light);
color: var(--success);
}
/* 编辑模式下才显示的元素 */
.edit-only { display: none; }
.card.editing .edit-only { display: flex; }

View File

@@ -113,6 +113,9 @@
<svg class="icon-h2"><use href="#icon-sun"/></svg>
<h3>早间立志</h3>
</div>
<button class="btn-cal-sync" onclick="syncCalendar()" title="同步钉钉日历">
<svg class="icon-sm"><use href="#icon-calendar"/></svg>
</button>
<button class="btn-edit-toggle" onclick="toggleEditMode(this)" title="编辑">
<svg class="icon-sm"><use href="#icon-pencil"/></svg>
</button>