From 9b643901cdf52acfb0a37c31019ac2451ffe5e68 Mon Sep 17 00:00:00 2001 From: hugy <504650082@qq.com> Date: Tue, 4 Jul 2023 17:17:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/3.9.5.81.md | 100 ++++++++++++++++++++++++++++++++++++ src/db.cc | 2 +- src/http_server_callback.cc | 38 ++++++++++++++ src/wechat_function.h | 3 +- 4 files changed, 140 insertions(+), 3 deletions(-) diff --git a/doc/3.9.5.81.md b/doc/3.9.5.81.md index 0f8d0b5..f5694b8 100644 --- a/doc/3.9.5.81.md +++ b/doc/3.9.5.81.md @@ -374,4 +374,104 @@ enableHttp=0时,使用ip,port的tcp服务回传消息。 ], "msg":"success" } +``` + + +#### 7.查询数据库** +###### 接口功能 +> 查询数据库 + +###### 接口地址 +> [/api/execSql](/api/execSql) + +###### HTTP请求方式 +> POST JSON + +###### 请求参数 +|参数|必选|类型|说明| +|---|---|---|---| +|dbHandle |true |number| | +|sql |true |string| 执行的sql | + +###### 返回字段 +|返回字段|字段类型|说明 | +|---|---|---| +|code|int|返回状态,0成功, 非0失败| +|msg|string|返回信息| +|data|array|sqlite返回的结果| + + + +###### 接口示例 +入参: +``` javascript +{ + "dbHandle":2006119800400, + "sql":"select * from MSG where localId =301;" +} +``` +响应: +``` javascript +{ + "code": 1, + "data": [ + [ + "localId", + "TalkerId", + "MsgSvrID", + "Type", + "SubType", + "IsSender", + "CreateTime", + "Sequence", + "StatusEx", + "FlagEx", + "Status", + "MsgServerSeq", + "MsgSequence", + "StrTalker", + "StrContent", + "DisplayContent", + "Reserved0", + "Reserved1", + "Reserved2", + "Reserved3", + "Reserved4", + "Reserved5", + "Reserved6", + "CompressContent", + "BytesExtra", + "BytesTrans" + ], + [ + "301", + "1", + "8824834301214701891", + "1", + "0", + "0", + "1685401473", + "1685401473000", + "0", + "0", + "2", + "1", + "795781866", + "wxid_123", + "testtest", + "", + "0", + "2", + "", + "", + "", + "", + "", + "", + "CgQIEBAAGo0BCAcSiAE8bXNnc291cmNlPJPHNpZ25hdHVyZT52MV9wd12bTZyRzwvc2lnbmF0dXJPgoJPHRtcF9ub2RlPgoJCTxwsaXNoZXItaWQ+Jmx0OyFbQ0RBVEFbXV0mZ3Q7PC9wdWJsaXNoZXItaWQ+Cgk8L3RtcF9ub2RlPgo8L21zZ3NvdXJjZT4KGiQIAhIgNDE1MDA0NjRhZTRmMjk2NjhjMzY2ZjFkOTdmMjAwNDg=", + "" + ] + ], + "msg": "success" +} ``` \ No newline at end of file diff --git a/src/db.cc b/src/db.cc index f7b9e92..bee18c0 100644 --- a/src/db.cc +++ b/src/db.cc @@ -40,7 +40,7 @@ void FreeResult(std::vector> &data) { int DB::SelectDataInner(UINT64 db, const char *sql, std::vector> &data) { common::sqlite3_prepare p_sqlite3_prepare = - (common::sqlite3_prepare)(this->base_addr_ + offset::k_sqlite3_prepare); + (common::sqlite3_prepare)(base_addr_ + offset::k_sqlite3_prepare); common::sqlite3_step p_sqlite3_step = (common::sqlite3_step)(base_addr_ + offset::k_sqlite3_step); common::sqlite3_column_count p_sqlite3_column_count = diff --git a/src/http_server_callback.cc b/src/http_server_callback.cc index 30b9d9b..700a27e 100644 --- a/src/http_server_callback.cc +++ b/src/http_server_callback.cc @@ -5,9 +5,26 @@ #include "global_context.h" #include "hooks.h" #include "db.h" + +#define STR2LL(str) (wxhelper::Utils::IsDigit(str) ? stoll(str) : 0) namespace common = wxhelper::common; + +INT64 GetINT64Param(nlohmann::json data, std::string key) { + INT64 result; + try { + result = data[key].get(); + } catch (nlohmann::json::exception) { + result = STR2LL(data[key].get()); + } + return result; +} + +std::string GetStringParam(nlohmann::json data, std::string key) { + return data[key].get(); +} + std::wstring GetWStringParam(nlohmann::json data, std::string key) { return wxhelper::Utils::UTF8ToWstring(data[key].get()); } @@ -210,6 +227,27 @@ std::string HttpDispatch(struct mg_connection *c, struct mg_http_message *hm) { ret_data["msg"] = "success"; ret = ret_data.dump(); return ret; + } else if (mg_http_match_uri(hm, "/api/execSql")) { + UINT64 db_handle = GetINT64Param(j_param, "dbHandle"); + std::string sql = GetStringParam(j_param, "sql"); + std::vector> items; + int success = wxhelper::DB::GetInstance().Select(db_handle, sql.c_str(), items); + nlohmann::json ret_data = { + {"data", nlohmann::json::array()}, {"code", success}, {"msg", "success"}}; + if (success == 0) { + ret_data["msg"] = "no data"; + ret = ret_data.dump(); + return ret; + } + for (auto it : items) { + nlohmann::json temp_arr = nlohmann::json::array(); + for (size_t i = 0; i < it.size(); i++) { + temp_arr.push_back(it[i]); + } + ret_data["data"].push_back(temp_arr); + } + ret = ret_data.dump(); + return ret; } else { nlohmann::json ret_data = { {"code", 200}, {"data", {}}, {"msg", "not support url"}}; diff --git a/src/wechat_function.h b/src/wechat_function.h index 8f382f0..b57e911 100644 --- a/src/wechat_function.h +++ b/src/wechat_function.h @@ -247,7 +247,7 @@ const UINT64 k_sqlite3_backup_init= 0x24e8450; const UINT64 k_sqlite3_errcode = 0x256bfb0; const UINT64 k_sqlite3_close = 0x256a110; const UINT64 k_sqlite3_step = 0x24f2350; -const UINT64 k_sqlite3_column_count = 0x1df3c80; +const UINT64 k_sqlite3_column_count = 0x24f2b70; const UINT64 k_sqlite3_column_name = 0x24f3570; const UINT64 k_sqlite3_column_type = 0x24f33c0; const UINT64 k_sqlite3_column_blob = 0x24f2ba0; @@ -268,7 +268,6 @@ const UINT64 kStorageEnd= 0x0; const UINT64 kMultiDBMgr= 0x3acfb68; const UINT64 kPublicMsgMgr= 0x3acc268; const UINT64 kFavoriteStorageMgr= 0x3acf0d0; -const UINT64 kFTSFavoriteMgr= 0x24f1400; } // namespace offset } // namespace V3_9_5_81