1 line
23 KiB
JSON
1 line
23 KiB
JSON
{"repo": "chaomixian/vflow", "files": {"docs/wiki/network.md": {"2acb986c5e94350fb5500d80c9f8bf9f7accf665": {"t": 1785746395.1937006, "n": 9266, "h": "7aaba798f20f5616", "c": "# 网络API参考\n\n网络API用于在Lua脚本中执行网络请求、获取IP地址以及调用AI服务。\n\n## 概述\n\n网络API提供以下功能:\n- **IP查询**:获取本地或公网IP地址\n- **HTTP请求**:发送GET、POST等HTTP请求\n- **AI对话**:调用OpenAI/DeepSeek等大模型API\n\n## API列表\n\n### vflow.network.get_ip_address\n\n获取设备的本地(局域网)或外部(公网)IP地址。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `type` | string | 否 | IP类型:`本地`、`外部`。默认:`本地` |\n| `ip_version` | string | 否 | IP版本:`IPv4`、`IPv6`。默认:`IPv4` |\n\n#### 返回值\n\n```lua\n{\n ip_address = string,\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 获取本地IPv4地址\nlocal local_ip = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n\nprint(\"本机IP: \" .. local_ip.ip_address)\n\n-- 获取公网IP地址\nlocal public_ip = vflow.network.get_ip_address({\n type = \"外部\",\n ip_version = \"IPv4\"\n})\n\nprint(\"公网IP: \" .. public_ip.ip_address)\n\n-- 获取IPv6地址\nlocal ipv6 = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv6\"\n})\n```\n\n---\n\n### vflow.network.http_request\n\n发送 HTTP 请求并获取响应。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.network.http_request({\n url = \"https://api.example.com/data\",\n method = \"GET\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `url` | string | 是 | 请求URL |\n| `method` | string | 否 | HTTP方法:`GET`、`POST`、`PUT`、`DELETE`、`PATCH`。默认:`GET` |\n| `headers` | dictionary | 否 | 请求头 |\n| `query_params` | dictionary | 否 | 查询参数 |\n| `body_type` | string | 否 | 请求体类型:`无`、`JSON`、`表单`、`原始文本` |\n| `body` | any | 否 | 请求体内容 |\n| `timeout` | number | 否 | 超时时间(秒)。默认:10 |\n\n#### 返回值\n\n```lua\n{\n response_body = string,\n status_code = number,\n response_headers = dictionary,\n error = string,\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 简单GET请求\nlocal response = vflow.network.http_request({\n url = \"https://api.github.com/users/octocat\",\n method = \"GET\"\n})\n\nif response.success then\n print(\"状态码: \" .. response.status_code)\n print(\"响应: \" .. response.response_body)\nend\n\n-- POST JSON数据\nlocal post_response = vflow.network.http_request({\n url = \"https://api.example.com/users\",\n method = \"POST\",\n headers = {\n [\"Authorization\"] = \"Bearer YOUR_TOKEN\",\n [\"Content-Type\"] = \"application/json\"\n },\n body_type = \"JSON\",\n body = {\n name = \"张三\",\n email = \"test@example.com\"\n }\n})\n\n-- 带查询参数的GET请求\nlocal search = vflow.network.http_request({\n url = \"https://api.example.com/search\",\n method = \"GET\",\n query_params = {\n q = \"vFlow\",\n page = 1,\n limit = 10\n }\n})\n\n-- 发送表单数据\nlocal form = vflow.network.http_request({\n url = \"https://httpbin.org/post\",\n method = \"POST\",\n body_type = \"表单\",\n body = {\n username = \"user123\",\n password = \"pass456\"\n }\n})\n\n-- 发送原始文本\nlocal text_req = vflow.network.http_request({\n url = \"https://example.com/api\",\n method = \"POST\",\n body_type = \"原始文本\",\n body = \"This is plain text content\",\n headers = {\n [\"Content-Type\"] = \"text/plain\"\n }\n})\n\n-- 使用魔法变量\nlocal token_response = vflow.network.http_request({\n url = \"{{auth_api.url}}\",\n method = \"POST\",\n body_type = \"JSON\",\n body = {\n token = \"{{auth_token.value}}\"\n }\n})\n```\n\n---\n\n### vflow.ai.completion\n\n调用大模型 API (OpenAI/DeepSeek) 进行智能对话。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.ai.completion({\n provider = \"OpenAI\",\n api_key = \"sk-...\",\n model = \"gpt-3.5-turbo\",\n prompt = \"Hello, how are you?\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `provider` | string | 是 | 服务商:`OpenAI`、`DeepSeek`、`自定义` |\n| `base_url` | string | 否 | API Base URL |\n| `api_key` | string | 是 | API密钥 |\n| `model` | string | 是 | 模型名称 |\n| `prompt` | string | 是 | 用户提示词 |\n| `system_prompt` | string | 否 | 系统提示词 |\n| `temperature` | number | 否 | 随机性(0-1)。默认:0.7 |\n\n#### 返回值\n\n```lua\n{\n result = string, -- AI的回答内容\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 使用OpenAI GPT-3.5\nlocal chat = vflow.ai.completion({\n provider = \"OpenAI\",\n base_url = \"https://api.openai.com/v1\",\n api_key = \"sk-your-api-key\",\n model = \"gpt-3.5-turbo\",\n prompt = \"解释什么是量子计算?\",\n temperature = 0.7\n})\n\nif chat.success then\n print(chat.result)\nend\n\n-- 使用DeepSeek\nlocal deepseek = vflow.ai.completion({\n provider = \"DeepSeek\",\n base_url = \"https://api.deepseek.com/v1\",\n api_key = \"your-deepseek-key\",\n model = \"deepseek-chat\",\n prompt = \"写一首关于春天的诗\"\n})\n\n-- 设置系统提示词\nlocal assistant = vflow.ai.completion({\n provider = \"OpenAI\",\n api_key = \"{{api_key.value}}\",\n model = \"gpt-3.5-turbo\",\n system_prompt = \"你是一个专业的翻译助手,只返回翻译结果,不要有其他说明。\",\n prompt = \"请将以下英文翻译成中文:\\nHello, how are you today?\",\n temperature = 0.3 -- 降低随机性以获得更准确的翻译\n})\n\n-- 结合OCR和AI\nlocal screenshot = vflow.system.capture_screen()\nlocal ocr_result = vflow.interaction.ocr({\n image = screenshot.image\n})\n\nif ocr_result.success then\n local ai_analysis = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"your-key\",\n model = \"deepseek-chat\",\n prompt = \"请总结以下文本的内容:\\n\" .. ocr_result.text\n })\n\n vflow.device.toast({\n message = ai_analysis.result\n })\nend\n\n-- 翻译功能\nfunction translate_text(text, from_lang, to_lang)\n local prompt = string.format(\n \"请将以下%s翻译成%s,只返回翻译结果:\\n%s\",\n from_lang,\n to_lang,\n text\n )\n\n local translation = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"your-key\",\n model = \"deepseek-chat\",\n prompt = prompt,\n temperature = 0.3\n })\n\n return translation.result\nend\n\nlocal translated = translate_text(\"Hello World\", \"英文\", \"中文\")\nprint(translated) -- 你好世界\n```\n\n---\n\n## 完整示例\n\n### 示例1:天气查询\n\n**manifest.json:**\n\n```json\n{\n \"id\": \"user.weather\",\n \"name\": \"天气查询\",\n \"description\": \"查询指定城市的天气\",\n \"category\": \"用户脚本\",\n \"inputs\": [\n {\n \"id\": \"city\",\n \"name\": \"城市名称\",\n \"type\": \"string\"\n }\n ],\n \"outputs\": [\n {\n \"id\": \"weather_info\",\n \"name\": \"天气信息\",\n \"type\": \"string\"\n }\n ],\n \"permissions\": []\n}\n```\n\n**script.lua:**\n\n```lua\nlocal city = inputs.city or \"北京\"\n\n-- 使用和风天气API(需要注册获取API key)\nlocal url = string.format(\n \"https://devapi.qweather.com/v7/weather/now?location=%s&key=YOUR_API_KEY\",\n city\n)\n\nlocal response = vflow.network.http_request({\n url = url,\n method = \"GET\"\n})\n\nif response.success and response.status_code == 200 then\n -- 解析JSON响应(需要使用Lua的JSON库)\n -- 这里简化处理\n vflow.device.toast({\n message = \"天气数据获取成功\"\n })\n\n return {\n weather_info = response.response_body\n }\nelse\n vflow.device.toast({\n message = \"获取天气失败\"\n })\n\n return {\n weather_info = \"错误: \" .. response.error\n }\nend\n```\n\n### 示例2:AI翻译助手\n\n**manifest.json:**\n\n```json\n{\n \"id\": \"user.ai_translator\",\n \"name\": \"AI翻译助手\",\n \"description\": \"使用AI进行多语言翻译\",\n \"category\": \"用户脚本\",\n \"inputs\": [\n {\n \"id\": \"text\",\n \"name\": \"要翻译的文本\",\n \"type\": \"string\"\n },\n {\n \"id\": \"target_lang\",\n \"name\": \"目标语言\",\n \"type\": \"string\"\n }\n ],\n \"outputs\": [\n {\n \"id\": \"translation\",\n \"name\": \"翻译结果\",\n \"type\": \"string\"\n }\n ],\n \"permissions\": []\n}\n```\n\n**script.lua:**\n\n```lua\nlocal text = inputs.text\nlocal target_lang = inputs.target_lang or \"中文\"\n\nif not text or text == \"\" then\n -- 如果没有输入文本,使用剪贴板内容\n local clipboard = vflow.system.get_clipboard()\n text = clipboard.text_content\nend\n\nif not text or text == \"\" then\n vflow.device.toast({ message = \"没有可翻译的文本\" })\n return { translation = \"\" }\nend\n\n-- 构建翻译提示词\nlocal prompt = string.format(\n \"请将以下文本翻译成%s,只返回翻译结果,不要有其他说明:\\n%s\",\n target_lang,\n text\n)\n\n-- 调用AI翻译\nlocal ai_result = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"YOUR_DEEPSEEK_API_KEY\",\n model = \"deepseek-chat\",\n prompt = prompt,\n temperature = 0.3\n})\n\nif ai_result.success then\n -- 将翻译结果复制到剪贴板\n vflow.system.set_clipboard({\n content = ai_result.result\n })\n\n -- 显示结果\n vflow.device.quick_view({\n content = ai_result.result\n })\n\n return {\n translation = ai_result.result\n }\nelse\n vflow.device.toast({\n message = \"翻译失败\"\n })\n return { translation = \"\" }\nend\n```\n\n### 示例3:网络状态监控\n\n**script.lua:**\n\n```lua\n-- 获取本地IP\nlocal local_ip = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n\n-- 获取公网IP\nlocal public_ip = vflow.network.get_ip_address({\n type = \"外部\",\n ip_version = \"IPv4\"\n})\n\n-- 测试网络连接\nlocal test = vflow.network.http_request({\n url = \"https://www.baidu.com\",\n method = \"GET\",\n timeout = 5\n})\n\nlocal status = \"网络连接正常\"\nif not test.success then\n status = \"网络连接失败\"\nend\n\n-- 组装信息\nlocal info = string.format(\n \"本地IP: %s\\n公网IP: %s\\n状态: %s\",\n local_ip.ip_address or \"未知\",\n public_ip.ip_address or \"未知\",\n status\n)\n\n-- 显示网络状态\nvflow.device.quick_view({\n content = info\n})\n\nreturn {\n local_ip = local_ip.ip_address,\n public_ip = public_ip.ip_address,\n status = status\n}\n```\n\n---\n\n## 注意事项\n\n1. **网络权限**:确保应用有网络访问权限\n2. **API密钥**:使用AI服务需要配置API密钥,请妥善保管\n3. **超时设置**:网络请求可能超时,建议设置合理的超时时间\n4. **错误处理**:网络请求可能失败,需要检查返回的`success`字段\n5. **HTTPS**:推荐使用HTTPS协议以确保安全\n\n---\n\n## 相关文档\n\n- [数据处理API](./data.md) - 数据处理\n- [系统API](./system.md) - 剪贴板操作\n- [界面交互API](./interaction.md) - 显示结果\n"}, "c13bb95db7c70906302847304499653396aa67b7": {"t": 1785763248.164552, "n": 9266, "h": "7aaba798f20f5616", "c": "# 网络API参考\n\n网络API用于在Lua脚本中执行网络请求、获取IP地址以及调用AI服务。\n\n## 概述\n\n网络API提供以下功能:\n- **IP查询**:获取本地或公网IP地址\n- **HTTP请求**:发送GET、POST等HTTP请求\n- **AI对话**:调用OpenAI/DeepSeek等大模型API\n\n## API列表\n\n### vflow.network.get_ip_address\n\n获取设备的本地(局域网)或外部(公网)IP地址。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `type` | string | 否 | IP类型:`本地`、`外部`。默认:`本地` |\n| `ip_version` | string | 否 | IP版本:`IPv4`、`IPv6`。默认:`IPv4` |\n\n#### 返回值\n\n```lua\n{\n ip_address = string,\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 获取本地IPv4地址\nlocal local_ip = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n\nprint(\"本机IP: \" .. local_ip.ip_address)\n\n-- 获取公网IP地址\nlocal public_ip = vflow.network.get_ip_address({\n type = \"外部\",\n ip_version = \"IPv4\"\n})\n\nprint(\"公网IP: \" .. public_ip.ip_address)\n\n-- 获取IPv6地址\nlocal ipv6 = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv6\"\n})\n```\n\n---\n\n### vflow.network.http_request\n\n发送 HTTP 请求并获取响应。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.network.http_request({\n url = \"https://api.example.com/data\",\n method = \"GET\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `url` | string | 是 | 请求URL |\n| `method` | string | 否 | HTTP方法:`GET`、`POST`、`PUT`、`DELETE`、`PATCH`。默认:`GET` |\n| `headers` | dictionary | 否 | 请求头 |\n| `query_params` | dictionary | 否 | 查询参数 |\n| `body_type` | string | 否 | 请求体类型:`无`、`JSON`、`表单`、`原始文本` |\n| `body` | any | 否 | 请求体内容 |\n| `timeout` | number | 否 | 超时时间(秒)。默认:10 |\n\n#### 返回值\n\n```lua\n{\n response_body = string,\n status_code = number,\n response_headers = dictionary,\n error = string,\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 简单GET请求\nlocal response = vflow.network.http_request({\n url = \"https://api.github.com/users/octocat\",\n method = \"GET\"\n})\n\nif response.success then\n print(\"状态码: \" .. response.status_code)\n print(\"响应: \" .. response.response_body)\nend\n\n-- POST JSON数据\nlocal post_response = vflow.network.http_request({\n url = \"https://api.example.com/users\",\n method = \"POST\",\n headers = {\n [\"Authorization\"] = \"Bearer YOUR_TOKEN\",\n [\"Content-Type\"] = \"application/json\"\n },\n body_type = \"JSON\",\n body = {\n name = \"张三\",\n email = \"test@example.com\"\n }\n})\n\n-- 带查询参数的GET请求\nlocal search = vflow.network.http_request({\n url = \"https://api.example.com/search\",\n method = \"GET\",\n query_params = {\n q = \"vFlow\",\n page = 1,\n limit = 10\n }\n})\n\n-- 发送表单数据\nlocal form = vflow.network.http_request({\n url = \"https://httpbin.org/post\",\n method = \"POST\",\n body_type = \"表单\",\n body = {\n username = \"user123\",\n password = \"pass456\"\n }\n})\n\n-- 发送原始文本\nlocal text_req = vflow.network.http_request({\n url = \"https://example.com/api\",\n method = \"POST\",\n body_type = \"原始文本\",\n body = \"This is plain text content\",\n headers = {\n [\"Content-Type\"] = \"text/plain\"\n }\n})\n\n-- 使用魔法变量\nlocal token_response = vflow.network.http_request({\n url = \"{{auth_api.url}}\",\n method = \"POST\",\n body_type = \"JSON\",\n body = {\n token = \"{{auth_token.value}}\"\n }\n})\n```\n\n---\n\n### vflow.ai.completion\n\n调用大模型 API (OpenAI/DeepSeek) 进行智能对话。\n\n#### Lua调用\n\n```lua\nlocal result = vflow.ai.completion({\n provider = \"OpenAI\",\n api_key = \"sk-...\",\n model = \"gpt-3.5-turbo\",\n prompt = \"Hello, how are you?\"\n})\n```\n\n#### 参数\n\n| 参数名 | 类型 | 必填 | 说明 |\n|--------|------|------|------|\n| `provider` | string | 是 | 服务商:`OpenAI`、`DeepSeek`、`自定义` |\n| `base_url` | string | 否 | API Base URL |\n| `api_key` | string | 是 | API密钥 |\n| `model` | string | 是 | 模型名称 |\n| `prompt` | string | 是 | 用户提示词 |\n| `system_prompt` | string | 否 | 系统提示词 |\n| `temperature` | number | 否 | 随机性(0-1)。默认:0.7 |\n\n#### 返回值\n\n```lua\n{\n result = string, -- AI的回答内容\n success = boolean\n}\n```\n\n#### 示例\n\n```lua\n-- 使用OpenAI GPT-3.5\nlocal chat = vflow.ai.completion({\n provider = \"OpenAI\",\n base_url = \"https://api.openai.com/v1\",\n api_key = \"sk-your-api-key\",\n model = \"gpt-3.5-turbo\",\n prompt = \"解释什么是量子计算?\",\n temperature = 0.7\n})\n\nif chat.success then\n print(chat.result)\nend\n\n-- 使用DeepSeek\nlocal deepseek = vflow.ai.completion({\n provider = \"DeepSeek\",\n base_url = \"https://api.deepseek.com/v1\",\n api_key = \"your-deepseek-key\",\n model = \"deepseek-chat\",\n prompt = \"写一首关于春天的诗\"\n})\n\n-- 设置系统提示词\nlocal assistant = vflow.ai.completion({\n provider = \"OpenAI\",\n api_key = \"{{api_key.value}}\",\n model = \"gpt-3.5-turbo\",\n system_prompt = \"你是一个专业的翻译助手,只返回翻译结果,不要有其他说明。\",\n prompt = \"请将以下英文翻译成中文:\\nHello, how are you today?\",\n temperature = 0.3 -- 降低随机性以获得更准确的翻译\n})\n\n-- 结合OCR和AI\nlocal screenshot = vflow.system.capture_screen()\nlocal ocr_result = vflow.interaction.ocr({\n image = screenshot.image\n})\n\nif ocr_result.success then\n local ai_analysis = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"your-key\",\n model = \"deepseek-chat\",\n prompt = \"请总结以下文本的内容:\\n\" .. ocr_result.text\n })\n\n vflow.device.toast({\n message = ai_analysis.result\n })\nend\n\n-- 翻译功能\nfunction translate_text(text, from_lang, to_lang)\n local prompt = string.format(\n \"请将以下%s翻译成%s,只返回翻译结果:\\n%s\",\n from_lang,\n to_lang,\n text\n )\n\n local translation = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"your-key\",\n model = \"deepseek-chat\",\n prompt = prompt,\n temperature = 0.3\n })\n\n return translation.result\nend\n\nlocal translated = translate_text(\"Hello World\", \"英文\", \"中文\")\nprint(translated) -- 你好世界\n```\n\n---\n\n## 完整示例\n\n### 示例1:天气查询\n\n**manifest.json:**\n\n```json\n{\n \"id\": \"user.weather\",\n \"name\": \"天气查询\",\n \"description\": \"查询指定城市的天气\",\n \"category\": \"用户脚本\",\n \"inputs\": [\n {\n \"id\": \"city\",\n \"name\": \"城市名称\",\n \"type\": \"string\"\n }\n ],\n \"outputs\": [\n {\n \"id\": \"weather_info\",\n \"name\": \"天气信息\",\n \"type\": \"string\"\n }\n ],\n \"permissions\": []\n}\n```\n\n**script.lua:**\n\n```lua\nlocal city = inputs.city or \"北京\"\n\n-- 使用和风天气API(需要注册获取API key)\nlocal url = string.format(\n \"https://devapi.qweather.com/v7/weather/now?location=%s&key=YOUR_API_KEY\",\n city\n)\n\nlocal response = vflow.network.http_request({\n url = url,\n method = \"GET\"\n})\n\nif response.success and response.status_code == 200 then\n -- 解析JSON响应(需要使用Lua的JSON库)\n -- 这里简化处理\n vflow.device.toast({\n message = \"天气数据获取成功\"\n })\n\n return {\n weather_info = response.response_body\n }\nelse\n vflow.device.toast({\n message = \"获取天气失败\"\n })\n\n return {\n weather_info = \"错误: \" .. response.error\n }\nend\n```\n\n### 示例2:AI翻译助手\n\n**manifest.json:**\n\n```json\n{\n \"id\": \"user.ai_translator\",\n \"name\": \"AI翻译助手\",\n \"description\": \"使用AI进行多语言翻译\",\n \"category\": \"用户脚本\",\n \"inputs\": [\n {\n \"id\": \"text\",\n \"name\": \"要翻译的文本\",\n \"type\": \"string\"\n },\n {\n \"id\": \"target_lang\",\n \"name\": \"目标语言\",\n \"type\": \"string\"\n }\n ],\n \"outputs\": [\n {\n \"id\": \"translation\",\n \"name\": \"翻译结果\",\n \"type\": \"string\"\n }\n ],\n \"permissions\": []\n}\n```\n\n**script.lua:**\n\n```lua\nlocal text = inputs.text\nlocal target_lang = inputs.target_lang or \"中文\"\n\nif not text or text == \"\" then\n -- 如果没有输入文本,使用剪贴板内容\n local clipboard = vflow.system.get_clipboard()\n text = clipboard.text_content\nend\n\nif not text or text == \"\" then\n vflow.device.toast({ message = \"没有可翻译的文本\" })\n return { translation = \"\" }\nend\n\n-- 构建翻译提示词\nlocal prompt = string.format(\n \"请将以下文本翻译成%s,只返回翻译结果,不要有其他说明:\\n%s\",\n target_lang,\n text\n)\n\n-- 调用AI翻译\nlocal ai_result = vflow.ai.completion({\n provider = \"DeepSeek\",\n api_key = \"YOUR_DEEPSEEK_API_KEY\",\n model = \"deepseek-chat\",\n prompt = prompt,\n temperature = 0.3\n})\n\nif ai_result.success then\n -- 将翻译结果复制到剪贴板\n vflow.system.set_clipboard({\n content = ai_result.result\n })\n\n -- 显示结果\n vflow.device.quick_view({\n content = ai_result.result\n })\n\n return {\n translation = ai_result.result\n }\nelse\n vflow.device.toast({\n message = \"翻译失败\"\n })\n return { translation = \"\" }\nend\n```\n\n### 示例3:网络状态监控\n\n**script.lua:**\n\n```lua\n-- 获取本地IP\nlocal local_ip = vflow.network.get_ip_address({\n type = \"本地\",\n ip_version = \"IPv4\"\n})\n\n-- 获取公网IP\nlocal public_ip = vflow.network.get_ip_address({\n type = \"外部\",\n ip_version = \"IPv4\"\n})\n\n-- 测试网络连接\nlocal test = vflow.network.http_request({\n url = \"https://www.baidu.com\",\n method = \"GET\",\n timeout = 5\n})\n\nlocal status = \"网络连接正常\"\nif not test.success then\n status = \"网络连接失败\"\nend\n\n-- 组装信息\nlocal info = string.format(\n \"本地IP: %s\\n公网IP: %s\\n状态: %s\",\n local_ip.ip_address or \"未知\",\n public_ip.ip_address or \"未知\",\n status\n)\n\n-- 显示网络状态\nvflow.device.quick_view({\n content = info\n})\n\nreturn {\n local_ip = local_ip.ip_address,\n public_ip = public_ip.ip_address,\n status = status\n}\n```\n\n---\n\n## 注意事项\n\n1. **网络权限**:确保应用有网络访问权限\n2. **API密钥**:使用AI服务需要配置API密钥,请妥善保管\n3. **超时设置**:网络请求可能超时,建议设置合理的超时时间\n4. **错误处理**:网络请求可能失败,需要检查返回的`success`字段\n5. **HTTPS**:推荐使用HTTPS协议以确保安全\n\n---\n\n## 相关文档\n\n- [数据处理API](./data.md) - 数据处理\n- [系统API](./system.md) - 剪贴板操作\n- [界面交互API](./interaction.md) - 显示结果\n"}}}} |