diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 40eff2671..cff271fb4 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1363,7 +1363,7 @@ void GUI_App::shutdown() } -std::string GUI_App::get_http_url(std::string country_code) +std::string GUI_App::get_http_url(std::string country_code, std::string path) { std::string url; if (country_code == "US") { @@ -1385,7 +1385,7 @@ std::string GUI_App::get_http_url(std::string country_code) url = "https://api.bambulab.com/"; } - url += "v1/iot-service/api/slicer/resource"; + url += path.empty() ? "v1/iot-service/api/slicer/resource" : path; return url; } @@ -1771,7 +1771,7 @@ void GUI_App::restart_networking() start_sync_user_preset(); } if (mainframe && this->app_config->get("staff_pick_switch") == "true") { - if (mainframe->m_webview) { mainframe->m_webview->SendDesignStaffpick(m_agent); } + if (mainframe->m_webview) { mainframe->m_webview->SendDesignStaffpick(true); } } } BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(" exit, m_agent=%1%")%m_agent; @@ -3818,7 +3818,7 @@ std::string GUI_App::handle_web_request(std::string cmd) else if (command_str.compare("modelmall_model_advise_get") == 0) { if (mainframe && this->app_config->get("staff_pick_switch") == "true") { if (mainframe->m_webview) { - mainframe->m_webview->SendDesignStaffpick(m_agent); + mainframe->m_webview->SendDesignStaffpick(true); } } } @@ -3826,10 +3826,8 @@ std::string GUI_App::handle_web_request(std::string cmd) if (root.get_child_optional("data") != boost::none) { pt::ptree data_node = root.get_child("data"); boost::optional id = data_node.get_optional("id"); - if (id.has_value() && m_agent) { - std::string url; - if (m_agent->get_model_mall_detail_url(&url, id.value()) == 0) - wxLaunchDefaultBrowser(url); + if (id.has_value() && mainframe->m_webview) { + mainframe->m_webview->OpenModelDetail(id.value(), m_agent); } } } @@ -4686,7 +4684,7 @@ void GUI_App::stop_http_server() void GUI_App::switch_staff_pick(bool on) { - mainframe->m_webview->SendDesignStaffpick(on ? m_agent : nullptr); + mainframe->m_webview->SendDesignStaffpick(on); } bool GUI_App::switch_language() diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index 1f052c3b9..05d88fdcc 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -603,7 +603,7 @@ public: std::string get_plugin_url(std::string name, std::string country_code); int download_plugin(std::string name, std::string package_name, InstallProgressFn pro_fn = nullptr, WasCancelledFn cancel_fn = nullptr); int install_plugin(std::string name, std::string package_name, InstallProgressFn pro_fn = nullptr, WasCancelledFn cancel_fn = nullptr); - std::string get_http_url(std::string country_code); + std::string get_http_url(std::string country_code, std::string path = {}); std::string get_model_http_url(std::string country_code); bool is_compatibility_version(); bool check_networking_version(); diff --git a/src/slic3r/GUI/WebViewDialog.cpp b/src/slic3r/GUI/WebViewDialog.cpp index 6b6f7ba45..05c049bcd 100644 --- a/src/slic3r/GUI/WebViewDialog.cpp +++ b/src/slic3r/GUI/WebViewDialog.cpp @@ -5,6 +5,7 @@ #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/MainFrame.hpp" #include "libslic3r_version.h" +#include "../Utils/Http.hpp" #include #include @@ -427,10 +428,10 @@ void WebViewPanel::SendRecentList(int images) RunScript(wxString::Format("window.postMessage(%s)", oss.str())); } -void WebViewPanel::SendDesignStaffpick(NetworkAgent *agent) +void WebViewPanel::SendDesignStaffpick(bool on) { - if (agent) { - agent->get_design_staffpick(0, 60, [this](std::string body) { + if (on) { + get_design_staffpick(0, 60, [this](std::string body) { if (body.empty() || body.front() != '{') { BOOST_LOG_TRIVIAL(warning) << "get_design_staffpick failed " + body; return; @@ -448,6 +449,13 @@ void WebViewPanel::SendDesignStaffpick(NetworkAgent *agent) } } +void WebViewPanel::OpenModelDetail(std::string id, NetworkAgent *agent) +{ + std::string url; + if ((agent ? agent->get_model_mall_detail_url(&url, id) : get_model_mall_detail_url(&url, id)) == 0) + wxLaunchDefaultBrowser(url); +} + void WebViewPanel::SendLoginInfo() { if (wxGetApp().getAgent()) { @@ -478,6 +486,32 @@ void WebViewPanel::ShowNetpluginTip() RunScript(strJS); } +void WebViewPanel::get_design_staffpick(int offset, int limit, std::function callback) +{ + auto host = wxGetApp().get_http_url(wxGetApp().app_config->get_country_code(), "v1/design-service/design/staffpick"); + std::string url = (boost::format("%1%/?offset=%2%&limit=%3%") % host % offset % limit).str(); + + Http http = Http::get(url); + http.header("accept", "application/json") + .header("Content-Type", "application/json") + .on_complete([this, callback](std::string body, unsigned status) { callback(body); }) + .on_error([this, callback](std::string body, std::string error, unsigned status) { + callback(body); + }) + .perform(); +} + +int WebViewPanel::get_model_mall_detail_url(std::string *url, std::string id) +{ + // https://makerhub-qa.bambu-lab.com/en/models/2077 + std::string h = wxGetApp().get_model_http_url(wxGetApp().app_config->get_country_code()); + auto l = wxGetApp().app_config->get("language"); + if (auto n = l.find('_'); n != std::string::npos) + l = l.substr(0, n); + *url = (boost::format("%1%%2%/models/%3%") % h % l % id).str(); + return 0; +} + void WebViewPanel::update_mode() { GetSizer()->Show(size_t(0), wxGetApp().app_config->get("internal_developer_mode") == "true"); diff --git a/src/slic3r/GUI/WebViewDialog.hpp b/src/slic3r/GUI/WebViewDialog.hpp index 0470a7b64..6a9c8296d 100644 --- a/src/slic3r/GUI/WebViewDialog.hpp +++ b/src/slic3r/GUI/WebViewDialog.hpp @@ -94,10 +94,14 @@ public: public: void SendRecentList(int images); - void SendDesignStaffpick(NetworkAgent *agent); + void SendDesignStaffpick(bool on); + void OpenModelDetail(std::string id, NetworkAgent *agent); void SendLoginInfo(); void ShowNetpluginTip(); + void get_design_staffpick(int offset, int limit, std::function callback); + int get_model_mall_detail_url(std::string *url, std::string id); + void update_mode(); private: