357 lines
9.9 KiB
Plaintext
357 lines
9.9 KiB
Plaintext
/**
|
|
* 消息系统工具函数 - 严格遵循UTS Android要求
|
|
*/
|
|
|
|
import {
|
|
Message,
|
|
MessageRecipient,
|
|
MessageType,
|
|
MessageSenderType,
|
|
MessageReceiverType,
|
|
MessageStatus,
|
|
RecipientStatus
|
|
} from './msgTypes.uts'
|
|
|
|
export class MsgUtils {
|
|
|
|
/**
|
|
* 格式化时间显示
|
|
*/
|
|
static formatTime(timeStr: string | null): string {
|
|
if (timeStr === null || timeStr === '') {
|
|
return ''
|
|
}
|
|
|
|
const date = new Date(timeStr)
|
|
const now = new Date()
|
|
const diff = now.getTime() - date.getTime()
|
|
|
|
// 小于1分钟
|
|
if (diff < 60000) {
|
|
return '刚刚'
|
|
}
|
|
|
|
// 小于1小时
|
|
if (diff < 3600000) {
|
|
const minutes = Math.floor(diff / 60000)
|
|
return `${minutes}分钟前`
|
|
}
|
|
|
|
// 小于1天
|
|
if (diff < 86400000) {
|
|
const hours = Math.floor(diff / 3600000)
|
|
return `${hours}小时前`
|
|
}
|
|
|
|
// 小于7天
|
|
if (diff < 604800000) {
|
|
const days = Math.floor(diff / 86400000)
|
|
return `${days}天前`
|
|
}
|
|
|
|
// 大于7天显示具体日期
|
|
const year = date.getFullYear()
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
|
|
}
|
|
|
|
/**
|
|
* 格式化完整时间
|
|
*/
|
|
static formatFullTime(timeStr: string | null): string {
|
|
if (timeStr === null || timeStr === '') {
|
|
return ''
|
|
}
|
|
|
|
const date = new Date(timeStr)
|
|
const year = date.getFullYear()
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
const hours = date.getHours()
|
|
const minutes = date.getMinutes()
|
|
|
|
return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
|
|
}
|
|
|
|
/**
|
|
* 获取消息类型显示文本
|
|
*/
|
|
static getMessageTypeText(code: string): string {
|
|
const typeMap = new Map<string, string>()
|
|
typeMap.set('system', '系统消息')
|
|
typeMap.set('device', '设备消息')
|
|
typeMap.set('training', '训练提醒')
|
|
typeMap.set('social', '社交消息')
|
|
typeMap.set('assignment', '作业通知')
|
|
typeMap.set('achievement', '成就通知')
|
|
typeMap.set('chat', '即时消息')
|
|
typeMap.set('announcement', '公告通知')
|
|
typeMap.set('reminder', '提醒消息')
|
|
typeMap.set('alert', '警报消息')
|
|
|
|
return typeMap.get(code) ?? '未知类型'
|
|
}
|
|
|
|
/**
|
|
* 获取发送者类型显示文本
|
|
*/
|
|
static getSenderTypeText(senderType: MessageSenderType): string {
|
|
const typeMap = new Map<string, string>()
|
|
typeMap.set('user', '用户')
|
|
typeMap.set('device', '设备')
|
|
typeMap.set('system', '系统')
|
|
|
|
return typeMap.get(senderType) ?? '未知'
|
|
}
|
|
|
|
/**
|
|
* 获取消息状态显示文本
|
|
*/
|
|
static getStatusText(status: MessageStatus): string {
|
|
const statusMap = new Map<string, string>()
|
|
statusMap.set('draft', '草稿')
|
|
statusMap.set('sent', '已发送')
|
|
statusMap.set('delivered', '已送达')
|
|
statusMap.set('failed', '发送失败')
|
|
|
|
return statusMap.get(status) ?? '未知状态'
|
|
}
|
|
|
|
/**
|
|
* 获取接收状态显示文本
|
|
*/
|
|
static getRecipientStatusText(status: RecipientStatus): string {
|
|
const statusMap = new Map<string, string>()
|
|
statusMap.set('pending', '待发送')
|
|
statusMap.set('sent', '已发送')
|
|
statusMap.set('delivered', '已送达')
|
|
statusMap.set('read', '已读')
|
|
statusMap.set('failed', '失败')
|
|
|
|
return statusMap.get(status) ?? '未知'
|
|
}
|
|
|
|
/**
|
|
* 获取优先级颜色
|
|
*/
|
|
static getPriorityColor(priority: number): string {
|
|
if (priority >= 90) {
|
|
return '#ff4757' // 紧急 - 红色
|
|
} else if (priority >= 70) {
|
|
return '#ffa726' // 高 - 橙色
|
|
} else if (priority >= 50) {
|
|
return '#42a5f5' // 普通 - 蓝色
|
|
} else {
|
|
return '#66bb6a' // 低 - 绿色
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取消息摘要
|
|
*/
|
|
static getMessageSummary(content: string | null, maxLength: number): string {
|
|
if (content === null || content === '') {
|
|
return ''
|
|
}
|
|
|
|
if (content.length <= maxLength) {
|
|
return content
|
|
}
|
|
|
|
return content.substring(0, maxLength) + '...'
|
|
}
|
|
|
|
/**
|
|
* 检查消息是否已过期
|
|
*/
|
|
static isMessageExpired(expiresAt: string | null): boolean {
|
|
if (expiresAt === null || expiresAt === '') {
|
|
return false
|
|
}
|
|
|
|
return new Date(expiresAt).getTime() < Date.now()
|
|
}
|
|
|
|
/**
|
|
* 检查消息是否未读
|
|
*/
|
|
static isMessageUnread(recipient: MessageRecipient): boolean {
|
|
return recipient.status !== 'read'
|
|
}
|
|
|
|
/**
|
|
* 验证发送参数
|
|
*/
|
|
static validateSendParams(title: string | null, content: string | null): string | null {
|
|
if (content === null || content.trim() === '') {
|
|
return '消息内容不能为空'
|
|
}
|
|
|
|
if (content.length > 5000) {
|
|
return '消息内容不能超过5000个字符'
|
|
}
|
|
|
|
if (title !== null && title.length > 200) {
|
|
return '消息标题不能超过200个字符'
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* 安全获取UTSJSONObject中的字符串值
|
|
*/
|
|
static getStringFromJson(obj: UTSJSONObject | null, key: string): string | null {
|
|
if (obj === null) {
|
|
return null
|
|
}
|
|
|
|
try {
|
|
const value = obj.getString(key)
|
|
return value === '' ? null : value
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 安全获取UTSJSONObject中的数字值
|
|
*/
|
|
static getNumberFromJson(obj: UTSJSONObject | null, key: string): number | null {
|
|
if (obj === null) {
|
|
return null
|
|
}
|
|
|
|
try {
|
|
return obj.getNumber(key)
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 安全获取UTSJSONObject中的布尔值
|
|
*/
|
|
static getBooleanFromJson(obj: UTSJSONObject | null, key: string): boolean {
|
|
if (obj === null) {
|
|
return false
|
|
}
|
|
|
|
try {
|
|
return obj.getBoolean(key) ?? false
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建本地存储键
|
|
*/
|
|
static createStorageKey(key: string): string {
|
|
return `akmon_msg_${key}`
|
|
}
|
|
|
|
/**
|
|
* 保存到本地存储
|
|
*/
|
|
static saveToLocal(key: string, data: UTSJSONObject): void {
|
|
try {
|
|
const jsonStr = JSON.stringify(data)
|
|
uni.setStorageSync(this.createStorageKey(key), jsonStr)
|
|
} catch (e) {
|
|
console.error('保存到本地存储失败:', e)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 从本地存储读取
|
|
*/
|
|
static getFromLocal(key: string): UTSJSONObject | null {
|
|
try {
|
|
const jsonStr = uni.getStorageSync(this.createStorageKey(key))
|
|
if (jsonStr !== null && jsonStr !== '') {
|
|
return JSON.parse(jsonStr as string) as UTSJSONObject
|
|
}
|
|
} catch (e) {
|
|
console.error('从本地存储读取失败:', e)
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* 清除本地存储
|
|
*/
|
|
static clearLocal(key: string): void {
|
|
try {
|
|
uni.removeStorageSync(this.createStorageKey(key))
|
|
} catch (e) {
|
|
console.error('清除本地存储失败:', e)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MessageItem组件需要的独立导出函数
|
|
|
|
/**
|
|
* 格式化时间显示 - 独立函数导出
|
|
*/
|
|
export function formatTime(timeStr: string | null): string {
|
|
return MsgUtils.formatTime(timeStr)
|
|
}
|
|
|
|
/**
|
|
* 获取消息类型名称 - 独立函数导出
|
|
*/
|
|
export function getTypeName(typeCode: string): string {
|
|
// 默认消息类型映射
|
|
const typeMap = new Map<string, string>()
|
|
typeMap.set('system', '系统消息')
|
|
typeMap.set('notification', '通知')
|
|
typeMap.set('announcement', '公告')
|
|
typeMap.set('reminder', '提醒')
|
|
typeMap.set('alert', '警报')
|
|
typeMap.set('update', '更新')
|
|
typeMap.set('message', '消息')
|
|
typeMap.set('chat', '聊天')
|
|
typeMap.set('default', '普通消息')
|
|
|
|
return typeMap.get(typeCode) ?? '消息'
|
|
}
|
|
|
|
/**
|
|
* 获取消息类型颜色 - 独立函数导出
|
|
*/
|
|
export function getTypeColor(typeCode: string): string {
|
|
// 默认消息类型颜色映射
|
|
const colorMap = new Map<string, string>()
|
|
colorMap.set('system', '#9c27b0') // 紫色
|
|
colorMap.set('notification', '#2196f3') // 蓝色
|
|
colorMap.set('announcement', '#ff9800') // 橙色
|
|
colorMap.set('reminder', '#4caf50') // 绿色
|
|
colorMap.set('alert', '#f44336') // 红色
|
|
colorMap.set('update', '#00bcd4') // 青色
|
|
colorMap.set('message', '#607d8b') // 蓝灰色
|
|
colorMap.set('chat', '#795548') // 棕色
|
|
colorMap.set('default', '#757575') // 灰色
|
|
|
|
return colorMap.get(typeCode) ?? '#757575'
|
|
}
|
|
|
|
/**
|
|
* 截断文本 - 独立函数导出
|
|
*/
|
|
export function truncateText(text: string, maxLength: number): string {
|
|
return MsgUtils.getMessageSummary(text, maxLength)
|
|
}
|
|
|
|
/**
|
|
* 格式化日期 - 独立函数导出
|
|
*/
|
|
export function formatDate(date: Date): string {
|
|
const y = date.getFullYear()
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0')
|
|
const d = date.getDate().toString().padStart(2, '0')
|
|
return y + '-' + m + '-' + d
|
|
}
|