From 7cd0250356b2b394a11bdca103f5e4b6e9597f5b Mon Sep 17 00:00:00 2001 From: a158 Date: Tue, 17 Mar 2026 13:58:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BF=BB=E8=AF=91=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + build.sh | 3 +- src/tencent_sdk.cpp | 15 +++++++ src/tencent_sdk_tmt.cpp | 83 ++++++++++++++++++++++++++++++++++++++ src/tencent_sdk_tmt.h | 35 ++++++++++++++++ src/tencent_sdk_trtc.cpp | 12 ------ target/tencent_sdk_tmt.lua | 24 +++++++++++ 7 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 src/tencent_sdk.cpp create mode 100644 src/tencent_sdk_tmt.cpp create mode 100644 src/tencent_sdk_tmt.h create mode 100644 target/tencent_sdk_tmt.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index d708aab..57423b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,7 @@ else() /opt/lua54/lib/liblua.a /usr/local/lib/libtencentcloud-sdk-cpp-core.so /usr/local/lib/libtencentcloud-sdk-cpp-trtc.so + /usr/local/lib/libtencentcloud-sdk-cpp-tmt.so hpsocket ylib diff --git a/build.sh b/build.sh index 7350897..e74e5b4 100644 --- a/build.sh +++ b/build.sh @@ -3,6 +3,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # 编译 SDK +mkdir 3rdparty cd "$SCRIPT_DIR/3rdparty" if [ ! -f tencentcloud-sdk-cpp-master.zip ]; then wget https://download.fwlua.com/other/tencentcloud-sdk-cpp-master.zip @@ -12,7 +13,7 @@ rm -rf tencentcloud-sdk-cpp-master unzip tencentcloud-sdk-cpp-master.zip cd tencentcloud-sdk-cpp-master # 后面加入了哪个功能再修改 -./build.sh build -DBUILD_MODULES="trtc" +./build.sh build -DBUILD_MODULES="trtc;tmt" ./build.sh install # 安装 diff --git a/src/tencent_sdk.cpp b/src/tencent_sdk.cpp new file mode 100644 index 0000000..73c98f5 --- /dev/null +++ b/src/tencent_sdk.cpp @@ -0,0 +1,15 @@ +#include "basemodule.h" +#include "tencent_sdk_tmt.h" +#include "tencent_sdk_trtc.h" +extern "C" { + #ifdef _WIN32 + DLL_EXPORT + #endif + int fastweb_module_regist(void* sol2, void* lua) + { + sol::state* state = static_cast(sol2); + module::tencent_sdk_tmt::regist(state); + module::tencent_sdk_trtc::regist(state); + return 0; + } +} \ No newline at end of file diff --git a/src/tencent_sdk_tmt.cpp b/src/tencent_sdk_tmt.cpp new file mode 100644 index 0000000..9f2e6bd --- /dev/null +++ b/src/tencent_sdk_tmt.cpp @@ -0,0 +1,83 @@ +#include "tencent_sdk_tmt.h" +#include "dll_interface.h" + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace TencentCloud; +using namespace TencentCloud::Tmt::V20180321; +using namespace TencentCloud::Tmt::V20180321::Model; +using namespace std; + + +module::tencent_sdk_tmt::tencent_sdk_tmt() +{ + +} + +module::tencent_sdk_tmt::~tencent_sdk_tmt() +{ + // cos_http_io_deinitialize(); +} + +std::pair module::tencent_sdk_tmt::text(const std::string& secret_id,const std::string& secret_key,const std::string& source_text,const std::string& source_language,const std::string& target_language) +{ +// 密钥信息从环境变量读取,需要提前在环境变量中设置 TENCENTCLOUD_SECRET_ID 和 TENCENTCLOUD_SECRET_KEY + // 使用环境变量方式可以避免密钥硬编码在代码中,提高安全性 + // 生产环境建议使用更安全的密钥管理方案,如密钥管理系统(KMS)、容器密钥注入等 + // 请参见:https://cloud.tencent.com/document/product/1278/85305 + // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 + Credential cred = Credential(secret_id.c_str(), secret_key.c_str()); + // 使用临时密钥示例 + // Credential cred = Credential("SecretId", "SecretKey", "Token"); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = HttpProfile(); + httpProfile.SetEndpoint("tmt.tencentcloudapi.com"); + + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = ClientProfile(); + clientProfile.SetHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 + TmtClient client = TmtClient(cred, "ap-beijing", clientProfile); + + // 实例化一个请求对象,每个接口都会对应一个request对象 + TextTranslateRequest req = TextTranslateRequest(); + req.SetSourceText(source_text.c_str()); + req.SetSource(source_language.c_str()); + req.SetTarget(target_language.c_str()); + req.SetProjectId(0); + + + + // 返回的resp是一个TextTranslateResponse的实例,与请求对象对应 + auto outcome = client.TextTranslate(req); + if (!outcome.IsSuccess()) + { + return std::make_pair(false, outcome.GetError().PrintAll()); + } + TextTranslateResponse resp = outcome.GetResult(); + // 输出json格式的字符串回包 + return std::make_pair(true, resp.ToJsonString()); +} + +void module::tencent_sdk_tmt::regist(sol::state* lua) +{ + lua->new_usertype("fw_tencent_sdk_tmt", + "new", sol::constructors(), + "text", &module::tencent_sdk_tmt::text + ); +} + +void module::tencent_sdk_tmt::regist_global(const char* name, sol::state* lua) +{ + lua->registry()[name] = this; + (*lua)[name] = this; +} diff --git a/src/tencent_sdk_tmt.h b/src/tencent_sdk_tmt.h new file mode 100644 index 0000000..851fce9 --- /dev/null +++ b/src/tencent_sdk_tmt.h @@ -0,0 +1,35 @@ +#pragma once +#include "basemodule.h" +#include +namespace module +{ + class tencent_sdk_tmt : public module::base { + public: + tencent_sdk_tmt(); + ~tencent_sdk_tmt() override; + static void regist(sol::state* lua); + + + /** + * @brief 文本翻译 + * @param secret_id 密钥ID + * @param secret_key 密钥 + * @param source_text 源文本 + * @param source_language 源语言 + * @param target_language 目标语言 + * @return 是否成功 + */ + std::pair text( + const std::string& secret_id, + const std::string& secret_key, + const std::string& source_text, + const std::string& source_language, + const std::string& target_language + ); + + private: + virtual void regist_global(const char* name, sol::state* lua); + virtual void delete_global() { delete this; } + }; +} + diff --git a/src/tencent_sdk_trtc.cpp b/src/tencent_sdk_trtc.cpp index d17e095..12eb607 100644 --- a/src/tencent_sdk_trtc.cpp +++ b/src/tencent_sdk_trtc.cpp @@ -17,18 +17,6 @@ using namespace TencentCloud; using namespace TencentCloud::Trtc::V20190722; using namespace TencentCloud::Trtc::V20190722::Model; -extern "C" { -#ifdef _WIN32 - DLL_EXPORT -#endif - int fastweb_module_regist(void* sol2, void* lua) - { - sol::state* state = static_cast(sol2); - module::tencent_sdk_trtc::regist(state); - return 0; - } -} - module::tencent_sdk_trtc::tencent_sdk_trtc() { diff --git a/target/tencent_sdk_tmt.lua b/target/tencent_sdk_tmt.lua new file mode 100644 index 0000000..530bfec --- /dev/null +++ b/target/tencent_sdk_tmt.lua @@ -0,0 +1,24 @@ +local tencent_sdk_tmt = {} +tencent_sdk_tmt.__index = tencent_sdk_tmt + +--[[ + 创建一个新的 tencent_tmt 对象 + @return 返回一个新的 tencent_sdk_tmt 对象 +]] +function tencent_sdk_tmt.new(db) + local instance = setmetatable({}, tencent_sdk_tmt) + if db == nil then + instance.module = fw_tencent_sdk_tmt.new() + else + instance.module = db + end + + return instance +end + + +function tencent_sdk_tmt:text(secret_id,secret_key,source_text,source_language,target_language) + return self.module:text(secret_id,secret_key,source_text,source_language,target_language) +end + +return tencent_sdk_tmt