初始提交:实习工作日志系统(设计方案 + Vue3 应用 + Electron 打包)
This commit is contained in:
@@ -0,0 +1,405 @@
|
||||
import { db, DEFAULT_LOCATIONS, SCHEMA_VERSION, type Attendance, type Credential, type LogItem, type Memo, type Project, type WeeklyNote, type WorkLog } from './schema'
|
||||
import { addDays, nowLocalISO, toDateStr } from '../lib/date'
|
||||
|
||||
// ---------- 设置 ----------
|
||||
|
||||
export async function getSetting(key: string, fallback: string): Promise<string> {
|
||||
const row = await db.setting.get(key)
|
||||
return row ? row.value : fallback
|
||||
}
|
||||
|
||||
export async function setSetting(key: string, value: string): Promise<void> {
|
||||
await db.setting.put({ key, value })
|
||||
}
|
||||
|
||||
export interface WorkRule {
|
||||
work_start: string
|
||||
work_end: string
|
||||
grace_minutes: number
|
||||
lunch_minutes: number
|
||||
remind_weekly: string
|
||||
}
|
||||
|
||||
export async function getWorkRule(): Promise<WorkRule> {
|
||||
const values = await Promise.all([
|
||||
getSetting('work_start', '09:00'),
|
||||
getSetting('work_end', '18:00'),
|
||||
getSetting('grace_minutes', '10'),
|
||||
getSetting('lunch_minutes', '60'),
|
||||
getSetting('remind_weekly', 'friday')
|
||||
])
|
||||
return {
|
||||
work_start: values[0],
|
||||
work_end: values[1],
|
||||
grace_minutes: Number(values[2]) || 0,
|
||||
lunch_minutes: Number(values[3]) || 0,
|
||||
remind_weekly: values[4]
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLastPick(): Promise<{ project_id: number | null; category: string }> {
|
||||
const lp = await getSetting('last_project', '')
|
||||
const lc = await getSetting('last_category', 'dev')
|
||||
return { project_id: lp ? Number(lp) : null, category: lc || 'dev' }
|
||||
}
|
||||
|
||||
export async function getLastLocation(): Promise<string> {
|
||||
return getSetting('last_location', '')
|
||||
}
|
||||
|
||||
// 记录某天的上班地点;当天尚无打卡记录时先建一条仅含地点的记录
|
||||
export async function setAttendanceLocation(date: string, location: string): Promise<void> {
|
||||
const loc = location.trim()
|
||||
await setSetting('last_location', loc)
|
||||
if (loc) await rememberLocation(loc)
|
||||
const row = await getAttendance(date)
|
||||
if (row && row.id) {
|
||||
await db.attendance.update(row.id, { location: loc || null })
|
||||
} else if (loc) {
|
||||
await db.attendance.add({
|
||||
date, clock_in_at: null, clock_out_at: null,
|
||||
kind: 'normal', note: null, location: loc, created_at: nowLocalISO()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLocationPresets(): Promise<string[]> {
|
||||
const raw = await getSetting('location_presets', '')
|
||||
if (!raw) return DEFAULT_LOCATIONS.slice()
|
||||
try {
|
||||
const arr = JSON.parse(raw)
|
||||
if (Array.isArray(arr)) {
|
||||
return arr.filter(x => typeof x === 'string' && x.trim()).map(x => (x as string).trim())
|
||||
}
|
||||
} catch { /* 忽略损坏数据,回退默认 */ }
|
||||
return DEFAULT_LOCATIONS.slice()
|
||||
}
|
||||
|
||||
export async function saveLocationPresets(list: string[]): Promise<void> {
|
||||
const clean = Array.from(new Set(list.map(x => x.trim()).filter(Boolean)))
|
||||
await setSetting('location_presets', JSON.stringify(clean))
|
||||
}
|
||||
|
||||
// 自定义输入的地点自动加入常用列表,下次可直接选
|
||||
export async function rememberLocation(loc: string): Promise<void> {
|
||||
const v = loc.trim()
|
||||
if (!v) return
|
||||
const presets = await getLocationPresets()
|
||||
if (!presets.includes(v)) {
|
||||
await saveLocationPresets([...presets, v])
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 项目 ----------
|
||||
|
||||
export async function listProjects(includeArchived = false): Promise<Project[]> {
|
||||
const all = await db.project.orderBy('name').toArray()
|
||||
return includeArchived ? all : all.filter(p => p.archived !== 1)
|
||||
}
|
||||
|
||||
export async function addProject(name: string): Promise<void> {
|
||||
await db.project.add({ name: name.trim(), archived: 0, created_at: nowLocalISO() })
|
||||
}
|
||||
|
||||
export async function renameProject(id: number, name: string): Promise<void> {
|
||||
await db.project.update(id, { name: name.trim() })
|
||||
}
|
||||
|
||||
export async function setProjectArchived(id: number, archived: boolean): Promise<void> {
|
||||
await db.project.update(id, { archived: archived ? 1 : 0 })
|
||||
}
|
||||
|
||||
// ---------- 打卡 ----------
|
||||
|
||||
export async function getAttendance(date: string): Promise<Attendance | undefined> {
|
||||
return db.attendance.where('date').equals(date).first()
|
||||
}
|
||||
|
||||
export async function clockIn(date: string, location?: string): Promise<void> {
|
||||
const loc = ((location === undefined ? await getSetting('last_location', '') : location) || '').trim()
|
||||
const row = await getAttendance(date)
|
||||
if (row && row.id) {
|
||||
if (!row.clock_in_at) {
|
||||
const patch: Partial<Attendance> = { clock_in_at: nowLocalISO() }
|
||||
if (loc) patch.location = loc
|
||||
await db.attendance.update(row.id, patch)
|
||||
}
|
||||
if (loc) {
|
||||
await setSetting('last_location', loc)
|
||||
await rememberLocation(loc)
|
||||
}
|
||||
return
|
||||
}
|
||||
await db.attendance.add({
|
||||
date, clock_in_at: nowLocalISO(), clock_out_at: null,
|
||||
kind: 'normal', note: null, location: loc || null, created_at: nowLocalISO()
|
||||
})
|
||||
if (loc) {
|
||||
await setSetting('last_location', loc)
|
||||
await rememberLocation(loc)
|
||||
}
|
||||
}
|
||||
|
||||
export async function clockOut(date: string): Promise<void> {
|
||||
const row = await getAttendance(date)
|
||||
if (row && row.id && row.clock_in_at && !row.clock_out_at) {
|
||||
await db.attendance.update(row.id, { clock_out_at: nowLocalISO() })
|
||||
}
|
||||
}
|
||||
|
||||
export async function smartClockOut(): Promise<string> {
|
||||
const now = new Date()
|
||||
const today = toDateStr(now)
|
||||
if (now.getHours() < 4) {
|
||||
const yRec = await getAttendance(addDays(today, -1))
|
||||
if (yRec && yRec.id && yRec.clock_in_at && !yRec.clock_out_at) {
|
||||
await db.attendance.update(yRec.id, { clock_out_at: nowLocalISO() })
|
||||
return yRec.date
|
||||
}
|
||||
}
|
||||
const tRec = await getAttendance(today)
|
||||
if (tRec && tRec.id && tRec.clock_in_at && !tRec.clock_out_at) {
|
||||
await db.attendance.update(tRec.id, { clock_out_at: nowLocalISO() })
|
||||
return tRec.date
|
||||
}
|
||||
throw new Error('没有进行中的打卡')
|
||||
}
|
||||
|
||||
export async function saveMakeup(date: string, inAt: string, outAt: string, note: string, location?: string): Promise<void> {
|
||||
if ((location || '').trim()) await rememberLocation(location as string)
|
||||
const row = await getAttendance(date)
|
||||
const patch = {
|
||||
date,
|
||||
clock_in_at: inAt,
|
||||
clock_out_at: outAt,
|
||||
kind: 'makeup',
|
||||
note: note || null,
|
||||
location: (location || '').trim() || null,
|
||||
created_at: nowLocalISO()
|
||||
}
|
||||
if (row && row.id) await db.attendance.update(row.id, patch)
|
||||
else await db.attendance.add(patch)
|
||||
}
|
||||
|
||||
// 直接修改某天的上/下班时间(行内编辑),状态由 computeDay 运行时自动重算
|
||||
export async function setAttendanceTime(date: string, field: 'clock_in_at' | 'clock_out_at', time: string): Promise<void> {
|
||||
const t = (time || '').trim()
|
||||
if (!t) return
|
||||
const row = await getAttendance(date)
|
||||
if (row && row.id) {
|
||||
await db.attendance.update(row.id, { [field]: date + 'T' + t + ':00' })
|
||||
}
|
||||
}
|
||||
|
||||
export async function listAttendanceBetween(start: string, end: string): Promise<Attendance[]> {
|
||||
return db.attendance.where('date').between(start, end, true, true).toArray()
|
||||
}
|
||||
|
||||
// 休假标记:标记时清空上下班时间,取消后恢复可打卡状态
|
||||
export async function setLeave(date: string, leave: boolean): Promise<void> {
|
||||
const row = await getAttendance(date)
|
||||
if (leave) {
|
||||
const base = { date, kind: 'leave', clock_in_at: null, clock_out_at: null, created_at: nowLocalISO() }
|
||||
if (row && row.id) await db.attendance.update(row.id, base)
|
||||
else await db.attendance.add({ ...base, note: null, location: null })
|
||||
} else if (row && row.id) {
|
||||
await db.attendance.update(row.id, { kind: 'normal' })
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 工作日志 ----------
|
||||
|
||||
export async function ensureWorkLog(date: string): Promise<WorkLog> {
|
||||
const found = await db.work_log.where('date').equals(date).first()
|
||||
if (found) return found
|
||||
const id = await db.work_log.add({ date, created_at: nowLocalISO() })
|
||||
return { id, date, created_at: nowLocalISO() }
|
||||
}
|
||||
|
||||
export async function getWorkLogByDate(date: string): Promise<WorkLog | undefined> {
|
||||
return db.work_log.where('date').equals(date).first()
|
||||
}
|
||||
|
||||
export async function listLogItemsByDate(date: string): Promise<LogItem[]> {
|
||||
const wl = await getWorkLogByDate(date)
|
||||
if (!wl || !wl.id) return []
|
||||
const items = await db.log_item.where('work_log_id').equals(wl.id).toArray()
|
||||
return items.sort((a, b) => (b.id as number) - (a.id as number))
|
||||
}
|
||||
|
||||
export interface NewLogItem {
|
||||
content: string
|
||||
project_id: number | null
|
||||
category: string
|
||||
minutes: number | null
|
||||
takeaway: string | null
|
||||
remark: string | null
|
||||
}
|
||||
|
||||
export async function addLogItem(date: string, data: NewLogItem): Promise<void> {
|
||||
const wl = await ensureWorkLog(date)
|
||||
await db.log_item.add({
|
||||
work_log_id: wl.id as number,
|
||||
content: data.content,
|
||||
project_id: data.project_id,
|
||||
category: data.category,
|
||||
minutes: data.minutes,
|
||||
is_milestone: 0,
|
||||
needs_followup: 0,
|
||||
takeaway: data.takeaway,
|
||||
remark: data.remark,
|
||||
sort_order: 0,
|
||||
created_at: nowLocalISO()
|
||||
})
|
||||
await setSetting('last_project', data.project_id ? String(data.project_id) : '')
|
||||
await setSetting('last_category', data.category)
|
||||
}
|
||||
|
||||
export async function updateLogItem(id: number, patch: Partial<LogItem>): Promise<void> {
|
||||
await db.log_item.update(id, patch)
|
||||
}
|
||||
|
||||
export async function deleteLogItem(id: number): Promise<void> {
|
||||
await db.log_item.delete(id)
|
||||
}
|
||||
|
||||
export async function listLogItemsBetween(start: string, end: string): Promise<(LogItem & { date: string })[]> {
|
||||
const wls = await db.work_log.where('date').between(start, end, true, true).toArray()
|
||||
if (!wls.length) return []
|
||||
const map = new Map<number, string>(wls.map(w => [w.id as number, w.date]))
|
||||
const items = await db.log_item.where('work_log_id').anyOf(wls.map(w => w.id as number)).toArray()
|
||||
return items
|
||||
.map(it => ({ ...it, date: map.get(it.work_log_id) || '' }))
|
||||
.sort((a, b) => (a.date === b.date ? (a.id as number) - (b.id as number) : a.date < b.date ? -1 : 1))
|
||||
}
|
||||
|
||||
// ---------- 周记 ----------
|
||||
|
||||
export async function getWeeklyNote(weekStart: string): Promise<WeeklyNote | undefined> {
|
||||
return db.weekly_note.where('week_start').equals(weekStart).first()
|
||||
}
|
||||
|
||||
export async function saveWeeklyNote(
|
||||
weekStart: string,
|
||||
patch: { highlights?: string | null; challenges?: string | null; plan_next?: string | null }
|
||||
): Promise<void> {
|
||||
const row = await getWeeklyNote(weekStart)
|
||||
if (row && row.id) {
|
||||
await db.weekly_note.update(row.id, patch)
|
||||
} else {
|
||||
await db.weekly_note.add({
|
||||
week_start: weekStart,
|
||||
highlights: patch.highlights ?? null,
|
||||
challenges: patch.challenges ?? null,
|
||||
plan_next: patch.plan_next ?? null,
|
||||
created_at: nowLocalISO()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function listWeeklyNotesBetween(start: string, end: string): Promise<WeeklyNote[]> {
|
||||
return db.weekly_note.where('week_start').between(start, end, true, true).toArray()
|
||||
}
|
||||
|
||||
// ---------- 备忘录 ----------
|
||||
|
||||
export async function listMemos(): Promise<Memo[]> {
|
||||
const rows = await db.memo.toArray()
|
||||
return rows.sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1))
|
||||
}
|
||||
|
||||
export async function addMemo(title: string, content: string): Promise<Memo> {
|
||||
const now = nowLocalISO()
|
||||
const id = await db.memo.add({ title: title.trim(), content, created_at: now, updated_at: now })
|
||||
return { id, title: title.trim(), content, created_at: now, updated_at: now }
|
||||
}
|
||||
|
||||
export async function updateMemo(id: number, title: string, content: string): Promise<void> {
|
||||
await db.memo.update(id, { title: title.trim(), content, updated_at: nowLocalISO() })
|
||||
}
|
||||
|
||||
export async function deleteMemo(id: number): Promise<void> {
|
||||
await db.memo.delete(id)
|
||||
}
|
||||
|
||||
// ---------- 全局搜索 ----------
|
||||
|
||||
export async function searchLogItems(q: string, limit = 8): Promise<(LogItem & { date: string })[]> {
|
||||
const needle = q.trim().toLowerCase()
|
||||
if (!needle) return []
|
||||
const wls = await db.work_log.toArray()
|
||||
const map = new Map<number, string>(wls.map(w => [w.id as number, w.date]))
|
||||
const all = await db.log_item.toArray()
|
||||
return all
|
||||
.filter(it => ((it.content || '') + ' ' + (it.takeaway || '') + ' ' + (it.remark || '')).toLowerCase().includes(needle))
|
||||
.sort((a, b) => (b.id as number) - (a.id as number))
|
||||
.slice(0, limit)
|
||||
.map(it => ({ ...it, date: map.get(it.work_log_id) || '' }))
|
||||
}
|
||||
|
||||
export async function searchMemos(q: string, limit = 4): Promise<Memo[]> {
|
||||
const needle = q.trim().toLowerCase()
|
||||
if (!needle) return []
|
||||
const all = await db.memo.toArray()
|
||||
return all
|
||||
.filter(m => ((m.title || '') + ' ' + (m.content || '')).toLowerCase().includes(needle))
|
||||
.sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1))
|
||||
.slice(0, limit)
|
||||
}
|
||||
|
||||
// ---------- 备份 ----------
|
||||
|
||||
export async function exportAll(): Promise<string> {
|
||||
const tables = await Promise.all([
|
||||
db.project.toArray(),
|
||||
db.work_log.toArray(),
|
||||
db.log_item.toArray(),
|
||||
db.attendance.toArray(),
|
||||
db.weekly_note.toArray(),
|
||||
db.setting.toArray(),
|
||||
db.memo.toArray(),
|
||||
db.credential.toArray()
|
||||
])
|
||||
return JSON.stringify({
|
||||
schema_version: SCHEMA_VERSION,
|
||||
exported_at: nowLocalISO(),
|
||||
data: {
|
||||
project: tables[0],
|
||||
work_log: tables[1],
|
||||
log_item: tables[2],
|
||||
attendance: tables[3],
|
||||
weekly_note: tables[4],
|
||||
setting: tables[5],
|
||||
memo: tables[6],
|
||||
credential: tables[7]
|
||||
}
|
||||
}, null, 2)
|
||||
}
|
||||
|
||||
export async function importAll(jsonText: string): Promise<void> {
|
||||
let parsed: { schema_version?: number; data?: Record<string, unknown[]> } | null = null
|
||||
try {
|
||||
parsed = JSON.parse(jsonText)
|
||||
} catch {
|
||||
throw new Error('不是有效的 JSON 文件')
|
||||
}
|
||||
if (!parsed || (parsed.schema_version !== 1 && parsed.schema_version !== 2) || !parsed.data) {
|
||||
throw new Error('文件格式或 schema 版本不匹配')
|
||||
}
|
||||
const d = parsed.data
|
||||
await db.transaction('rw', [db.project, db.work_log, db.log_item, db.attendance, db.weekly_note, db.setting, db.memo, db.credential], async () => {
|
||||
await Promise.all([
|
||||
db.project.clear(), db.work_log.clear(), db.log_item.clear(),
|
||||
db.attendance.clear(), db.weekly_note.clear(), db.setting.clear(),
|
||||
db.memo.clear(), db.credential.clear()
|
||||
])
|
||||
if (d.project && d.project.length) await db.project.bulkAdd(d.project as never[])
|
||||
if (d.work_log && d.work_log.length) await db.work_log.bulkAdd(d.work_log as never[])
|
||||
if (d.log_item && d.log_item.length) await db.log_item.bulkAdd(d.log_item as never[])
|
||||
if (d.attendance && d.attendance.length) await db.attendance.bulkAdd(d.attendance as never[])
|
||||
if (d.weekly_note && d.weekly_note.length) await db.weekly_note.bulkAdd(d.weekly_note as never[])
|
||||
if (d.setting && d.setting.length) await db.setting.bulkAdd(d.setting as never[])
|
||||
if (d.memo && d.memo.length) await db.memo.bulkAdd(d.memo as never[])
|
||||
if (d.credential && d.credential.length) await db.credential.bulkAdd(d.credential as never[])
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user