diff --git a/src/index.js b/src/index.js index b86d302..916240d 100644 --- a/src/index.js +++ b/src/index.js @@ -138,25 +138,26 @@ async function handleCardInteraction(event) { break; case 'select_profile': { - const { profile_name, bucket, path: upload_path } = actionData.value; + const { profile_name, bucket, path_key } = actionData.value; log('📋 选择上传配置:', profile_name); + // path_key 是预设路径的名称,需要从配置中获取实际路径 setUserState(chatId, { profile_name, bucket, - upload_path + path_key }); await feishu.sendMessage(chatId, { msg_type: 'text', - content: { text: `✅ 已选择配置:**${profile_name}**\n\n📤 请发送文件,或点击"📎 选择文件上传"` } + content: { text: `✅ 已选择配置:**${profile_name}**\n\n📤 请发送文件` } }); break; } case 'upload_with_profile': { // 从配置卡片直接上传(需要先发送文件) - const { profile_name, bucket, path: upload_path } = actionData.value; + const { profile_name, bucket, path_key } = actionData.value; const state = getUserState(chatId); if (!state.file_key) { @@ -167,22 +168,34 @@ async function handleCardInteraction(event) { return; } + // 从预设路径配置中获取实际路径 + const fullConfig = loadFullConfig(); + const uploadPaths = fullConfig.uploadPaths || {}; + const upload_path = uploadPaths[path_key] || ''; + await doUpload(chatId, feishu, uploader, { file_key: state.file_key, file_name: state.file_name, message_id: state.message_id, bucket, upload_path, - path_label: profile_name + path_label: profile_name, + path_key }); clearUserState(chatId); break; } case 'confirm_upload': { - const { file_key, file_name, message_id, bucket, upload_path, path_label } = actionData.value; + const { file_key, file_name, message_id, bucket, path_key, path_label } = actionData.value; + + // 从预设路径配置中获取实际路径 + const fullConfig = loadFullConfig(); + const uploadPaths = fullConfig.uploadPaths || {}; + const upload_path = uploadPaths[path_key] || ''; + await doUpload(chatId, feishu, uploader, { - file_key, file_name, message_id, bucket, upload_path, path_label + file_key, file_name, message_id, bucket, upload_path, path_label, path_key }); clearUserState(chatId); break; @@ -213,17 +226,25 @@ async function showProfileCard(chatId, feishu, uploader) { const fullConfig = loadFullConfig(); const profiles = fullConfig.uploadProfiles || {}; - const profileButtons = Object.entries(profiles).map(([name, config]) => ({ - tag: 'button', - text: { tag: 'plain_text', content: name }, - type: 'primary', - value: { - action: 'select_profile', - profile_name: name, - bucket: config.bucket, - path: config.path || '' - } - })); + const profileButtons = Object.entries(profiles).map(([name, config]) => { + // config.path 是预设路径的名称(键) + const pathKey = config.path || ''; + const uploadPaths = fullConfig.uploadPaths || {}; + const pathValue = uploadPaths[pathKey] || ''; + const pathDisplay = pathValue || '(原文件名)'; + + return { + tag: 'button', + text: { tag: 'plain_text', content: `${name}` }, + type: 'primary', + value: { + action: 'select_profile', + profile_name: name, + bucket: config.bucket, + path_key: pathKey + } + }; + }); const card = { config: { wide_screen_mode: true }, @@ -310,22 +331,28 @@ async function handleFileReceived(messageData, feishu, uploader) { // 未选择配置,显示配置选择卡片 const fullConfig = loadFullConfig(); const profiles = fullConfig.uploadProfiles || {}; + const uploadPaths = fullConfig.uploadPaths || {}; - const profileButtons = Object.entries(profiles).map(([name, config]) => ({ - tag: 'button', - text: { tag: 'plain_text', content: `${name}` }, - type: 'primary', - value: { - action: 'confirm_upload', - file_key: fileKey, - file_name: fileName, - message_id: messageId, - chat_id: chatId, - bucket: config.bucket, - upload_path: config.path || '', - path_label: name - } - })); + const profileButtons = Object.entries(profiles).map(([name, config]) => { + // config.path 是预设路径的名称(键) + const pathKey = config.path || ''; + + return { + tag: 'button', + text: { tag: 'plain_text', content: `${name}` }, + type: 'primary', + value: { + action: 'confirm_upload', + file_key: fileKey, + file_name: fileName, + message_id: messageId, + chat_id: chatId, + bucket: config.bucket, + path_key: pathKey, + path_label: name + } + }; + }); const card = { config: { wide_screen_mode: true }, @@ -375,7 +402,13 @@ async function handleFileReceived(messageData, feishu, uploader) { // 显示确认卡片 async function showConfirmCard(chatId, feishu, info) { - const { file_name, bucket, upload_path, path_label } = info; + const { file_name, bucket, path_key, path_label } = info; + + // 从预设路径配置中获取实际路径 + const fullConfig = loadFullConfig(); + const uploadPaths = fullConfig.uploadPaths || {}; + const upload_path = uploadPaths[path_key] || ''; + let targetKey = upload_path || file_name; if (targetKey.startsWith('/')) targetKey = targetKey.substring(1); @@ -407,7 +440,7 @@ async function showConfirmCard(chatId, feishu, info) { message_id: info.message_id, chat_id: chatId, bucket, - upload_path, + path_key, path_label } }, @@ -545,31 +578,40 @@ async function handleProfileCommandV2(message, content, feishu) { if (subCommand === 'list' || !subCommand) { const profiles = fullConfig.uploadProfiles || {}; - await feishu.sendCard(chatId, createProfilesListCard(profiles)); + const uploadPaths = fullConfig.uploadPaths || {}; + await feishu.sendCard(chatId, createProfilesListCard(profiles, uploadPaths)); } else if (subCommand === 'add') { - // /profile add <名称> <存储桶> [路径] + // /profile add <名称> <存储桶> [路径键名] if (args.length < 3) { - throw new Error('用法:/profile add <名称> <存储桶> [路径]\n示例:/profile add IPA 上传 default /ipa/'); + throw new Error('用法:/profile add <名称> <存储桶> [路径键名]\n示例:/profile add IPA 上传 default ipa'); } const name = args[1]; const bucket = args[2]; - const pathValue = args[3] || ''; + const pathKey = args[3] || ''; // 验证存储桶是否存在 if (!fullConfig.buckets[bucket]) { throw new Error(`存储桶 "${bucket}" 不存在,可用:${Object.keys(fullConfig.buckets).join(', ')}`); } + // 验证路径键名是否存在(如果有提供) + if (pathKey && (!fullConfig.uploadPaths || !fullConfig.uploadPaths[pathKey])) { + const availablePaths = Object.keys(fullConfig.uploadPaths || {}).join(', '); + throw new Error(`路径 "${pathKey}" 不存在,可用:${availablePaths || '无'}`); + } + fullConfig.uploadProfiles = fullConfig.uploadProfiles || {}; fullConfig.uploadProfiles[name] = { bucket: bucket, - path: pathValue + path: pathKey // 存储路径键名,不是路径值 }; fs.writeFileSync(configPath, JSON.stringify(fullConfig, null, 2)); + + const pathDisplay = pathKey ? `${pathKey} (${fullConfig.uploadPaths[pathKey]})` : '(原文件名)'; await feishu.sendMessage(chatId, { msg_type: 'text', - content: { text: `✅ 已添加上传配置:**${name}**\n存储桶:${bucket}\n路径:${pathValue || '(原文件名)'}` } + content: { text: `✅ 已添加上传配置:**${name}**\n存储桶:${bucket}\n路径:${pathDisplay}` } }); } else if (subCommand === 'remove' || subCommand === 'del') { if (args.length < 2) { @@ -758,7 +800,7 @@ function createPathsListCard(paths) { } // 上传配置模板列表卡片(表格形式) -function createProfilesListCard(profiles) { +function createProfilesListCard(profiles, uploadPaths) { const entries = Object.entries(profiles); if (entries.length === 0) { @@ -775,7 +817,9 @@ function createProfilesListCard(profiles) { // 构建表格内容 let tableContent = '| 名称 | 存储桶 | 路径 |\n|------|--------|------|\n'; for (const [name, config] of entries) { - const pathDisplay = config.path || '(原文件名)'; + const pathKey = config.path || ''; + const pathValue = uploadPaths && uploadPaths[pathKey] ? uploadPaths[pathKey] : '(原文件名)'; + const pathDisplay = pathKey ? `${pathKey} → ${pathValue}` : '(原文件名)'; tableContent += `| **${name}** | ${config.bucket} | ${pathDisplay} |\n`; }