소스 검색

V0.11.2
1、添加了WebAPI的基类和公共头文件

Apple 4 일 전
부모
커밋
612c269357
3개의 변경된 파일181개의 추가작업 그리고 0개의 파일을 삭제
  1. 139 0
      common/WebAPI/WebAPIBase.cpp
  2. 27 0
      common/WebAPI/WebAPIBase.h
  3. 15 0
      common/commonDefine.h

+ 139 - 0
common/WebAPI/WebAPIBase.cpp

@@ -0,0 +1,139 @@
+#include "WebAPIBase.h"
+
+#include "spdlog/spdlog.h"
+
+
+WebAPIBase::~WebAPIBase()
+{
+    if(m_httpApi != nullptr)
+    {
+        delete m_httpApi;
+        m_httpApi = nullptr;
+    }
+}
+
+
+/* 初始化WebApi */
+bool WebAPIBase::initWebApi(const QString& url,const QString& serID, const QString appType, const QString& serverIP)
+{
+    if(m_httpApi == nullptr)
+    {
+        m_httpApi = new lhhttpapi;
+    }
+    
+    if(!m_httpApi->Load())
+    {
+        SPDLOG_ERROR("Load WebAPI failed");
+        return false;
+    }
+    SPDLOG_DEBUG("URL: {}", url.toStdString());
+    SPDLOG_DEBUG("ServerID: {}", serID.toStdString());
+    SPDLOG_DEBUG("ServerKey: {}", appType.toStdString());
+
+    void* pHttp = nullptr;
+    int i = 0;
+    for(i = 0; i < 3; i++)
+    {
+        pHttp = m_httpApi->DBInit(url.toStdString().c_str(), true);
+        if(pHttp != nullptr)
+        {
+            break;
+        }
+        SPDLOG_ERROR("设置WebAPI地址失败: {}", m_httpApi->DoGetLastError(&i).toStdString());
+    }
+    if(i >= 3)
+    {
+        SPDLOG_ERROR("WebAPI设置地址错误!");
+        return false;
+    }
+    
+    int ret = 0;
+
+#ifdef QT_DEBUG
+    /* 获取服务器列表 */
+    // char serverList[8192]={0};
+    // ret = m_httpApi->DBGetServerList(serverList, 8192-1);
+    // if(ret != 0)
+    // {
+    //     SPDLOG_LOGGER_ERROR(m_logger, "Get server list failed: {}, error info: {}", ret, m_httpApi->DoGetLastError(&ret).toStdString());
+    //     return false;
+    // }
+    // SPDLOG_LOGGER_INFO(m_logger, QString("\nGet server list success:%1").arg(serverList));
+#endif
+
+    /* 登录,第二个参数是限制的服务 */
+    for(i = 0; i < 3; i++)
+    {
+        ret = m_httpApi->DBLogin("", serID, appType, m_userToken, true, pHttp);
+        if(ret == 0)
+        {
+            break;
+        }
+        SPDLOG_ERROR("Login failed: {}, error info: {}", ret, m_httpApi->DoGetLastError(&ret).toStdString());
+        /* 登录失败,等待一段时间 */
+        QThread::msleep(200);
+    }
+    if(i >= 3)
+    {
+        SPDLOG_ERROR("登陆WebAPI失败!");
+        return false;
+    }
+
+    SPDLOG_INFO("WebAPI Login success!");
+
+    return true;
+}
+
+
+/**
+ * @brief 将json的key转换成小写,这种转换不会转值中嵌套的json
+ * 
+ * @param json 
+ * @param result 
+ * @return true 
+ * @return false 
+ */
+bool WebAPIBase::convertJsonKeyToLower(const nJson& json, nJson& result)
+{
+    for(auto& it : json.items())
+    {
+        std::string key = it.key();
+        std::transform(key.begin(), key.end(), key.begin(), ::tolower); // 转换成小写
+        /* 是object */
+        if(it.value().is_object())
+        {
+            convertJsonKeyToLower(it.value(), result[key]); // 递归处理嵌套的对象
+        }
+        /* 是array */
+        else if(it.value().is_array())
+        {
+            nJson arrayResult = nJson::array();
+            for(auto& item : it.value())
+            {
+                if(item.is_object())
+                {
+                    /* 如果是对象,递归转换 */
+                    nJson itemJson = nJson::object();
+                    convertJsonKeyToLower(item, itemJson);
+                    /* 将转换后的对象添加到数组中 */
+                    arrayResult.push_back(itemJson);
+                }
+                else
+                {
+                    arrayResult.push_back(item);
+                }
+            }
+            result[key] = arrayResult;
+        }
+        else
+        {
+            /* 直接赋值非对象和非数组类型 */
+            result[key] = it.value(); 
+        }
+    }
+
+    return true;
+}
+
+
+

+ 27 - 0
common/WebAPI/WebAPIBase.h

@@ -0,0 +1,27 @@
+#ifndef _WEBAPIBASE_H_
+#define _WEBAPIBASE_H_
+
+#include "lhhttpapi.h"
+#include "commonDefine.h"
+
+class WebAPIBase
+{
+public:
+    WebAPIBase() = default;
+    virtual ~WebAPIBase();
+
+    /* 初始化WebAPI */
+    virtual bool initWebApi(const QString& url, const QString& serID, const QString appType, const QString& serverIP = "");
+
+protected:
+    /* 将json的key转换成小写 */
+    bool convertJsonKeyToLower(const nJson& json, nJson& result);
+
+protected:
+    lhhttpapi* m_httpApi = nullptr;
+    QString m_userToken;
+
+};
+
+
+#endif /* _WEBAPIBASE_H_ */

+ 15 - 0
common/commonDefine.h

@@ -0,0 +1,15 @@
+#ifndef COMMONDEFINE_H
+#define COMMONDEFINE_H
+
+#include "nlohmann/json.hpp"
+
+
+/*--------------------------------------------------------------------------
+ * 公共宏定义
+ *--------------------------------------------------------------------------*/
+
+#define nJson nlohmann::json
+
+
+
+#endif /* COMMONDEFINE_H */