fastbee/yack-mini/miniprogram/utils/command-storage.js

199 lines
4.6 KiB
JavaScript

/**
* 命令存储工具
*/
const COMMAND_KEY = 'yack_commands'; // 预设命令
const HISTORY_KEY = 'yack_command_history'; // 发送历史
/**
* 获取预设命令列表
* @param {string} deviceId - 设备编号(可选,不传则获取所有)
* @returns {Array}
*/
function getCommands(deviceId) {
try {
const commands = wx.getStorageSync(COMMAND_KEY) || [];
if (deviceId) {
return commands.filter(cmd => cmd.deviceId === deviceId);
}
return commands;
} catch (e) {
console.error('[CommandStorage] getCommands error:', e);
return [];
}
}
/**
* 保存预设命令列表
* @param {Array} commands
*/
function saveCommands(commands) {
try {
wx.setStorageSync(COMMAND_KEY, commands || []);
} catch (e) {
console.error('[CommandStorage] saveCommands error:', e);
}
}
/**
* 添加预设命令
* @param {Object} command
*/
function addCommand(command) {
const commands = getCommands();
// 检查是否已存在相同名称的命令
const exists = commands.find(c =>
c.name === command.name && c.deviceId === command.deviceId
);
if (exists) {
console.warn('[CommandStorage] 命令名称已存在');
return false;
}
command.id = `cmd_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
command.createTime = Date.now();
command.useCount = 0;
// 移除 description 字段,只保留必要信息
const cleanCommand = {
id: command.id,
name: command.name,
hex: command.hex,
deviceId: command.deviceId,
createTime: command.createTime,
useCount: command.useCount,
lastUsedTime: command.lastUsedTime || 0,
};
commands.push(cleanCommand);
saveCommands(commands);
return true;
}
/**
* 更新预设命令
* @param {Object} command
*/
function updateCommand(command) {
const commands = getCommands();
const index = commands.findIndex(c => c.id === command.id);
if (index >= 0) {
commands[index] = { ...commands[index], ...command };
saveCommands(commands);
return true;
}
return false;
}
/**
* 删除预设命令
* @param {string} id
*/
function removeCommand(id) {
const commands = getCommands();
const newCommands = commands.filter(c => c.id !== id);
saveCommands(newCommands);
console.log('[CommandStorage] 已删除命令:', id);
}
/**
* 获取发送历史(只返回当天的)
* @param {string} deviceId - 设备编号(可选)
* @returns {Array}
*/
function getHistory(deviceId) {
try {
const history = wx.getStorageSync(HISTORY_KEY) || [];
// 过滤:只保留当天的记录
const today = new Date();
today.setHours(0, 0, 0, 0);
const todayTimestamp = today.getTime();
const todayHistory = history.filter(item => {
const sendTime = item.sendTime || 0;
return sendTime >= todayTimestamp;
});
// 如果过滤后的数据有变化,更新存储
if (todayHistory.length !== history.length) {
setHistory(todayHistory);
}
// 如果指定了设备,只返回该设备的记录
if (deviceId) {
return todayHistory.filter(item => item.deviceId === deviceId);
}
return todayHistory;
} catch (e) {
console.error('[CommandStorage] getHistory error:', e);
return [];
}
}
/**
* 保存发送历史
* @param {Array} history
*/
function setHistory(history) {
try {
wx.setStorageSync(HISTORY_KEY, history || []);
} catch (e) {
console.error('[CommandStorage] setHistory error:', e);
}
}
/**
* 添加发送历史
* @param {Object} historyItem
*/
function addHistory(historyItem) {
const history = getHistory(); // 自动过滤当天的
historyItem.id = `history_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
historyItem.sendTime = Date.now();
history.unshift(historyItem); // 最新的在前面
// 限制历史记录数量(最多 100 条)
if (history.length > 100) {
history.splice(100);
}
setHistory(history);
}
/**
* 清空发送历史
*/
function clearHistory() {
try {
wx.removeStorageSync(HISTORY_KEY);
console.log('[CommandStorage] 已清空发送历史');
} catch (e) {
console.error('[CommandStorage] clearHistory error:', e);
}
}
/**
* 清空所有命令数据
*/
function clearAll() {
try {
wx.removeStorageSync(COMMAND_KEY);
wx.removeStorageSync(HISTORY_KEY);
console.log('[CommandStorage] 已清空所有命令数据');
} catch (e) {
console.error('[CommandStorage] clearAll error:', e);
}
}
module.exports = {
getCommands,
saveCommands,
addCommand,
updateCommand,
removeCommand,
getHistory,
setHistory,
addHistory,
clearHistory,
clearAll,
};