import { generateId } from '@/api/request.js' const STORAGE_KEY = 'app_notes' function readLocal() { return uni.getStorageSync(STORAGE_KEY) || [] } function writeLocal(list) { uni.setStorageSync(STORAGE_KEY, list) } function seedIfEmpty() { const list = readLocal() if (list.length) return list const now = Date.now() const seeded = [ { id: generateId(), title: '欢迎使用记事本', content: '在这里记录灵感、待办或任何想法。支持置顶、搜索与编辑,后续可一键接入云端同步。', pinned: true, createdAt: now - 86400000, updatedAt: now - 3600000 }, { id: generateId(), title: '项目会议要点', content: '1. 确认 Q3 目标\n2. 排期评审\n3. 接口联调时间', pinned: false, createdAt: now - 172800000, updatedAt: now - 7200000 } ] writeLocal(seeded) return seeded } function sortNotes(list) { return [...list].sort((a, b) => { if (a.pinned !== b.pinned) return a.pinned ? -1 : 1 return (b.updatedAt || 0) - (a.updatedAt || 0) }) } /** * 获取笔记列表 * @param {{ keyword?: string }} params * @returns {Promise<{ list: Array, total: number }>} */ export async function fetchNotes(params = {}) { // TODO: return request({ url: '/api/notes', data: params }) let list = seedIfEmpty() const keyword = (params.keyword || '').trim().toLowerCase() if (keyword) { list = list.filter( item => item.title.toLowerCase().includes(keyword) || item.content.toLowerCase().includes(keyword) ) } list = sortNotes(list) return { list, total: list.length } } /** * 获取单条笔记 * @param {string} id */ export async function getNote(id) { // TODO: return request({ url: `/api/notes/${id}` }) const list = readLocal() return list.find(item => item.id === id) || null } /** * 创建笔记 * @param {{ title: string, content: string, pinned?: boolean }} data */ export async function createNote(data) { // TODO: return request({ url: '/api/notes', method: 'POST', data }) const now = Date.now() const note = { id: generateId(), title: (data.title || '').trim() || '无标题', content: (data.content || '').trim(), pinned: !!data.pinned, createdAt: now, updatedAt: now } const list = readLocal() list.unshift(note) writeLocal(list) return note } /** * 更新笔记 * @param {string} id * @param {{ title?: string, content?: string, pinned?: boolean }} data */ export async function updateNote(id, data) { // TODO: return request({ url: `/api/notes/${id}`, method: 'PUT', data }) const list = readLocal() const index = list.findIndex(item => item.id === id) if (index === -1) throw new Error('笔记不存在') const note = { ...list[index], ...data, title: data.title !== undefined ? (data.title.trim() || '无标题') : list[index].title, content: data.content !== undefined ? data.content.trim() : list[index].content, updatedAt: Date.now() } list[index] = note writeLocal(list) return note } /** * 删除笔记 * @param {string} id */ export async function deleteNote(id) { // TODO: return request({ url: `/api/notes/${id}`, method: 'DELETE' }) const list = readLocal().filter(item => item.id !== id) writeLocal(list) return true } /** * 切换置顶 * @param {string} id */ export async function toggleNotePin(id) { const note = await getNote(id) if (!note) throw new Error('笔记不存在') return updateNote(id, { pinned: !note.pinned }) }