Files
JGE-RS-SL-API/item/item_list.js
2025-12-25 18:20:37 +08:00

74 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @ts-ignore 获取物品列表接口
/**
* 获取物品列表接口
* 通过PSK验证后读取all_item_data.js文件中的物品数据
* 返回所有物品的详细信息
*/
export async function execute(http_message) {
// 验证请求头中的PSK
let psk_valid = HttpReQUtils.check_psk(http_message);
if (!psk_valid) {
// 直接返回错误响应
return HttpResUtils.err_code(401, "未授权");
}
try {
// 获取已导入的物品数据
// @ts-ignore
let itemData = G.AllItemData.default;
if (!itemData) {
console.error("物品数据未加载");
return HttpResUtils.err_code(404, "物品数据未加载");
}
// 格式化物品数据为列表
let itemList = [];
for (let itemName in itemData) {
if (itemData.hasOwnProperty(itemName)) {
let item = itemData[itemName];
// 确保wp_7是数组类型
let wp7Value = item.wp_7;
if (wp7Value === undefined || wp7Value === null) {
wp7Value = [];
} else if (!Array.isArray(wp7Value)) {
// 如果不是数组,转换为数组
wp7Value = [wp7Value];
}
// 确保数组中的所有元素都是字符串类型
wp7Value = wp7Value.map(element => {
if (element === null || element === undefined) {
return "";
}
return String(element);
});
itemList.push({
name: itemName,
wp_1: item.wp_1 || "",
wp_2: item.wp_2 || 0,
wp_3: item.wp_3 || 0,
wp_4: item.wp_4 || 0,
wp_5: item.wp_5 || 0,
wp_7: wp7Value,
wp_8: item.wp_8 || "",
wp_11: item.wp_11 || "",
wp_12: item.wp_12 || "",
wp_13: item.wp_13 || ""
});
}
}
// 返回成功响应
return HttpResUtils.ok({
item_list: itemList,
total: itemList.length
});
} catch (error) {
console.error("获取物品列表失败:", error);
// 返回错误响应
return HttpResUtils.err_data_msg(null, "获取物品列表失败: " + (error.message || "未知错误"));
}
}