添加key查看器

This commit is contained in:
SunWuyuan
2025-08-29 21:24:21 +08:00
parent c744f37f39
commit cd10d0f49a
6 changed files with 826 additions and 13 deletions
+53
View File
@@ -35,6 +35,57 @@ export default {
return kvLocalProvider.saveData(key, data);
}
},
/**
* 获取键名列表
* @param {Object} options - 查询选项
* @param {string} options.sortBy - 排序字段,默认为 "key"
* @param {string} options.sortDir - 排序方向,"asc" 或 "desc",默认为 "asc"
* @param {number} options.limit - 每页返回的记录数,默认为 100
* @param {number} options.skip - 跳过的记录数,默认为 0
* @returns {Promise<Object>} 包含键名列表和分页信息的响应对象
*
* 使用示例:
* ```javascript
* // 获取前10个键名
* const result = await dataProvider.loadKeys({ limit: 10 });
* if (result.success !== false) {
* console.log('键名列表:', result.keys);
* console.log('总数:', result.total_rows);
* }
*
* // 获取第二页数据(跳过前10个)
* const page2 = await dataProvider.loadKeys({ limit: 10, skip: 10 });
*
* // 按键名降序排列
* const sorted = await dataProvider.loadKeys({ sortDir: 'desc' });
* ```
*
* 返回值格式:
* ```javascript
* {
* keys: ["key1", "key2", "key3"],
* total_rows: 150,
* current_page: {
* limit: 10,
* skip: 0,
* count: 10
* },
* load_more: "/api/kv/namespace/_keys?..." // 仅服务器模式
* }
* ```
*/
loadKeys: async (options = {}) => {
const provider = getSetting("server.provider");
const useServer =
provider === "kv-server" || provider === "classworkscloud";
if (useServer) {
return kvServerProvider.loadKeys(options);
} else {
return kvLocalProvider.loadKeys(options);
}
},
};
export const ErrorCodes = {
@@ -43,5 +94,7 @@ export const ErrorCodes = {
SERVER_ERROR: "服务器错误",
SAVE_ERROR: "保存失败",
CONFIG_ERROR: "配置错误",
PERMISSION_DENIED: "无权限访问",
UNAUTHORIZED: "认证失败",
UNKNOWN_ERROR: "未知错误",
};
+68
View File
@@ -46,4 +46,72 @@ export const kvLocalProvider = {
return formatError("保存本地数据失败:" + error);
}
},
/**
* 获取本地存储的键名列表
* @param {Object} options - 查询选项
* @param {string} options.sortBy - 排序字段,默认为 "key"
* @param {string} options.sortDir - 排序方向,"asc" 或 "desc",默认为 "asc"
* @param {number} options.limit - 每页返回的记录数,默认为 100
* @param {number} options.skip - 跳过的记录数,默认为 0
* @returns {Promise<Object>} 包含键名列表和分页信息的响应对象
*
* 返回值示例:
* {
* keys: ["key1", "key2", "key3"],
* total_rows: 150,
* current_page: {
* limit: 10,
* skip: 0,
* count: 10
* },
* load_more: null // 本地存储不需要分页URL
* }
*/
async loadKeys(options = {}) {
try {
const db = await initDB();
const transaction = db.transaction(["kv"], "readonly");
const store = transaction.objectStore("kv");
// 获取所有键名
const allKeys = await store.getAllKeys();
// 设置默认参数
const {
sortBy = "key",
sortDir = "asc",
limit = 100,
skip = 0
} = options;
// 排序键名(本地存储只支持按键名排序)
const sortedKeys = allKeys.sort((a, b) => {
if (sortDir === "desc") {
return b.localeCompare(a);
}
return a.localeCompare(b);
});
// 应用分页
const totalRows = sortedKeys.length;
const paginatedKeys = sortedKeys.slice(skip, skip + limit);
// 构建响应数据
const responseData = {
keys: paginatedKeys,
total_rows: totalRows,
current_page: {
limit,
skip,
count: paginatedKeys.length
},
load_more: null // 本地存储不需要分页URL
};
return formatResponse(responseData);
} catch (error) {
return formatError("获取本地键名列表失败:" + error.message);
}
},
};
+65
View File
@@ -157,4 +157,69 @@ export const kvServerProvider = {
);
}
},
/**
* 获取键名列表
* @param {Object} options - 查询选项
* @param {string} options.sortBy - 排序字段,默认为 "key"
* @param {string} options.sortDir - 排序方向,"asc" 或 "desc",默认为 "asc"
* @param {number} options.limit - 每页返回的记录数,默认为 100
* @param {number} options.skip - 跳过的记录数,默认为 0
* @returns {Promise<Object>} 包含键名列表和分页信息的响应对象
*
* 返回值示例:
* {
* keys: ["key1", "key2", "key3"],
* total_rows: 150,
* current_page: {
* limit: 10,
* skip: 0,
* count: 10
* },
* load_more: "/api/kv/namespace/_keys?sortBy=key&sortDir=asc&limit=10&skip=10"
* }
*/
async loadKeys(options = {}) {
try {
const serverUrl = getSetting("server.domain");
const machineId = getSetting("device.uuid");
// 设置默认参数
const {
sortBy = "key",
sortDir = "asc",
limit = 100,
skip = 0
} = options;
// 构建查询参数
const params = new URLSearchParams({
sortBy,
sortDir,
limit: limit.toString(),
skip: skip.toString()
});
const res = await axios.get(`${serverUrl}/${machineId}/_keys?${params}`, {
headers: getHeaders(),
});
return formatResponse(res.data);
} catch (error) {
if (error.response?.status === 404) {
return formatError("命名空间不存在", "NOT_FOUND");
}
if (error.response?.status === 403) {
return formatError("无权限访问此命名空间", "PERMISSION_DENIED");
}
if (error.response?.status === 401) {
return formatError("认证失败", "UNAUTHORIZED");
}
console.log(error);
return formatError(
error.response?.data?.message || "获取键名列表失败",
"NETWORK_ERROR"
);
}
},
};