asynchelper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef ASYNCHELPER_H
  2. #define ASYNCHELPER_H
  3. #include <QFuture>
  4. #include <QFutureWatcher>
  5. #include <QObject>
  6. #include <functional>
  7. #include "../network/networkaccessmanager.h"
  8. #include "../widgets/maskoverlay.h"
  9. // 前向声明安全执行回调的辅助函数
  10. void safeExecuteCallback(const std::function<void()>& callback, const QString& callbackType);
  11. class AsyncHelper : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. static AsyncHelper* instance();
  16. // 通用的异步请求处理模板
  17. template<typename T>
  18. class Request
  19. {
  20. public:
  21. Request(const QFuture<T>& future)
  22. : m_future(future)
  23. , m_successCallback(nullptr)
  24. , m_errorCallback(nullptr)
  25. , m_finallyCallback(nullptr)
  26. , m_maskParent(nullptr)
  27. {}
  28. // 设置成功回调
  29. Request& onSuccess(std::function<void(const T&)> callback)
  30. {
  31. m_successCallback = callback;
  32. return *this;
  33. }
  34. // 设置错误回调
  35. Request& onError(std::function<void(const QString&)> callback)
  36. {
  37. m_errorCallback = callback;
  38. return *this;
  39. }
  40. // 设置最终回调
  41. Request& onFinally(std::function<void()> callback)
  42. {
  43. m_finallyCallback = callback;
  44. return *this;
  45. }
  46. // 设置遮罩层
  47. Request& withMask(QWidget* parent)
  48. {
  49. if (parent) {
  50. MaskOverlay::instance()->show(parent, 0, MaskOverlay::ActiveWindow);
  51. m_maskParent = parent;
  52. }
  53. return *this;
  54. }
  55. // 执行请求
  56. void exec()
  57. {
  58. auto* watcher = new QFutureWatcher<T>();
  59. // 复制回调函数,确保在 lambda 中使用的是副本
  60. auto successCallback = m_successCallback;
  61. auto errorCallback = m_errorCallback;
  62. auto finallyCallback = m_finallyCallback;
  63. auto maskParent = m_maskParent;
  64. // 处理完成信号
  65. QObject::connect(watcher,
  66. &QFutureWatcher<T>::finished,
  67. [watcher, successCallback, errorCallback, finallyCallback, maskParent]() {
  68. if (maskParent) {
  69. MaskOverlay::instance()->hide();
  70. }
  71. T result = watcher->result();
  72. if (successCallback) {
  73. successCallback(result);
  74. }
  75. // 安全执行finally回调
  76. if (finallyCallback) {
  77. safeExecuteCallback(finallyCallback, "finally");
  78. }
  79. watcher->deleteLater();
  80. });
  81. // 处理取消信号 - 同样修改捕获方式
  82. QObject::connect(watcher,
  83. &QFutureWatcher<T>::canceled,
  84. [watcher, errorCallback, finallyCallback, maskParent]() {
  85. // 隐藏遮罩层
  86. if (maskParent) {
  87. MaskOverlay::instance()->hide();
  88. }
  89. if (errorCallback) {
  90. errorCallback(QObject::tr("请求被取消"));
  91. }
  92. // 安全执行finally回调
  93. if (finallyCallback) {
  94. safeExecuteCallback(finallyCallback, "finally");
  95. }
  96. watcher->deleteLater();
  97. });
  98. // 设置 Future
  99. watcher->setFuture(m_future);
  100. }
  101. private:
  102. const QFuture<T>& m_future;
  103. std::function<void(const T&)> m_successCallback;
  104. std::function<void(const QString&)> m_errorCallback;
  105. std::function<void()> m_finallyCallback;
  106. QWidget* m_maskParent = nullptr;
  107. };
  108. // 创建通用请求
  109. template<typename T>
  110. static Request<T> request(const QFuture<T>& future)
  111. {
  112. return Request<T>(future);
  113. }
  114. // 创建 HTTP 请求
  115. static Request<HttpResponse> http(const QFuture<HttpResponse>& future)
  116. {
  117. return Request<HttpResponse>(future);
  118. }
  119. private:
  120. explicit AsyncHelper(QObject* parent = nullptr);
  121. static AsyncHelper* m_instance;
  122. };
  123. #endif // ASYNCHELPER_H