import { request } from '@/api/request.js' export const REMIND_CHANNEL_OPTIONS = [ { value: 'sms', label: '短信' }, { value: 'email', label: '邮件' }, { value: 'bark', label: 'Bark推送' }, { value: 'site', label: '站内信' }, { value: 'app', label: 'APP' } ] export const REMIND_MINUTES_OPTIONS = [ { label: '不提前', value: 0 }, { label: '提前 5 分钟', value: 5 }, { label: '提前 10 分钟', value: 10 }, { label: '提前 15 分钟', value: 15 }, { label: '提前 30 分钟', value: 30 }, { label: '提前 1 小时', value: 60 }, { label: '提前 2 小时', value: 120 }, { label: '提前 1 天', value: 1440 } ] export function normalizeSchedule(item) { const content = (item.content || item.title || '').trim() const title = (item.title || content.split('\n')[0] || '无标题').trim().slice(0, 60) let remindChannels = item.remindChannels || item.remind_channels || [] if (!Array.isArray(remindChannels)) { remindChannels = [] } remindChannels = remindChannels.map(ch => ch === 'SITE_MSG' ? 'app' : ch.toLowerCase()) const datetime = item.schedule_time ? new Date(item.schedule_time.replace(' ', 'T')).getTime() : item.datetime || 0 const createdAt = item.created_at ? new Date(item.created_at.replace(' ', 'T')).getTime() : item.createdAt || datetime const updatedAt = item.updated_at ? new Date(item.updated_at.replace(' ', 'T')).getTime() : item.updatedAt || datetime return { ...item, id: item.id, title, content, remindChannels, remindMinutes: item.advance_minutes ?? item.remindMinutes ?? 15, completed: item.is_finished ?? item.completed ?? false, datetime, createdAt, updatedAt } } /** * 获取日程列表 */ export async function fetchSchedules(params = {}) { const data = await request({ url: '/app/schedule/list', data: { page: params.page || 1, pageSize: params.pageSize || 50, keyword: params.keyword || '', status: params.status || '' } }) const list = (data.list || []).map(normalizeSchedule) return { list, total: data.total || 0 } } /** * 获取单条日程 */ export async function getSchedule(id) { const data = await request({ url: `/app/schedule/${id}` }) return data ? normalizeSchedule(data) : null } /** * 创建日程 */ export async function createSchedule(data) { const channels = (data.remindChannels || []).map(ch => { if (ch === 'app') return 'SITE_MSG' return ch.toUpperCase() }) const res = await request({ url: '/app/schedule', method: 'POST', data: { content: data.content, schedule_time: formatDateTime(data.datetime), remind_channels: channels, advance_minutes: data.remindMinutes ?? 15 } }) return res } /** * 更新日程 */ export async function updateSchedule(id, data) { const channels = (data.remindChannels || []).map(ch => { if (ch === 'app') return 'SITE_MSG' return ch.toUpperCase() }) await request({ url: `/app/schedule/${id}`, method: 'PUT', data: { content: data.content, schedule_time: formatDateTime(data.datetime), remind_channels: channels, advance_minutes: data.remindMinutes ?? 15 } }) } /** * 删除日程 */ export async function deleteSchedule(id) { await request({ url: `/app/schedule/${id}`, method: 'DELETE' }) return true } /** * 切换完成状态 */ export async function toggleScheduleComplete(id) { const res = await request({ url: `/app/schedule/${id}/toggle`, method: 'POST' }) return { ...res, completed: res?.is_finished ?? false } } function formatDateTime(timestamp) { const d = new Date(timestamp) const pad = n => String(n).padStart(2, '0') return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}` } export function getRemindChannelLabel(value) { return REMIND_CHANNEL_OPTIONS.find(item => item.value === value)?.label || value } export function getRemindChannelLabels(channels = []) { if (!channels.length) return '未设置' return channels.map(getRemindChannelLabel).join('、') } export function getRemindMinutesLabel(minutes) { const item = REMIND_MINUTES_OPTIONS.find(option => option.value === minutes) return item?.label || (minutes ? `提前 ${minutes} 分钟` : '不提前') } export function getScheduleSummary(item) { const text = (item?.content || item?.title || '').trim() const line = text.split('\n')[0] return line || '无内容' }