import { request } from '@/utils/request' import { API_BASE } from '@/config' /** 通知 */ export interface Notification { id: number type: 'system' | 'group' | 'personal' title: string content: string is_read: number created_at: string group_id?: number | null is_pinned?: number is_urgent?: number publish_at?: string | null expire_at?: string | null image_url?: string link_url?: string } /** 通知列表响应 */ export interface NotificationList { list: Notification[] total: number page: number pageSize: number } /** 发布公告参数 */ export interface PublishParams { title: string content?: string type?: 'system' | 'group' | 'personal' group_id?: number is_pinned?: boolean is_urgent?: boolean publish_at?: string expire_at?: string image_url?: string link_url?: string } /** 获取通知列表 */ export function getNotifications(params?: { type?: string; page?: number; pageSize?: number }) { return request({ url: '/notifications', data: params }) } /** 获取置顶/强提醒公告 */ export function getPinnedNotifications() { return request({ url: '/notifications/pinned' }) } /** 获取未读数量 */ export function getUnreadCount() { return request<{ count: number }>({ url: '/notifications/unread-count' }) } /** 标记单条已读 */ export function markRead(id: number) { return request({ url: `/notifications/${id}/read`, method: 'PUT' }) } /** 全部标记已读 */ export function markAllRead() { return request({ url: '/notifications/read-all', method: 'PUT' }) } /** 上传通知图片(返回文件名) */ export function uploadNotificationImage(filePath: string): Promise<{ image_url: string }> { return new Promise((resolve, reject) => { const token = uni.getStorageSync('xc:token') uni.uploadFile({ url: `${API_BASE}/notifications/upload-image`, filePath, name: 'file', header: { ...(token ? { Authorization: `Bearer ${token}` } : {}) }, success: (res) => { try { const data = JSON.parse(res.data) if (data.code === 0) { resolve(data.data) } else { reject(data) } } catch { reject({ message: '上传失败' }) } }, fail: () => reject({ message: '网络错误' }) }) }) } /** 获取通知图片完整 URL */ export function getNotificationImageUrl(filename: string): string { if (!filename) return '' // 如果已经是完整 URL 或 blob URL,直接返回 if (filename.startsWith('http') || filename.startsWith('blob:')) return filename return `${API_BASE}/notifications/image/${filename}` } /** 发布公告 */ export function publishNotification(data: PublishParams) { return request<{ id: number }>({ url: '/notifications', method: 'POST', data }) } /** 编辑公告 */ export function updateNotification(id: number, data: Partial) { return request({ url: `/notifications/${id}`, method: 'PUT', data }) } /** 删除公告 */ export function deleteNotification(id: number) { return request({ url: `/notifications/${id}`, method: 'DELETE' }) }