FIX: [STUDIO-3271] load staff pick model list with network agent

Change-Id: Ie2c36348a32a4cd0c0c1457fd715619b23550794
This commit is contained in:
chunmao.guo 2023-06-16 18:43:41 +08:00 committed by Lane.Wei
parent 739d8c9b28
commit 9b5a86ba01
4 changed files with 50 additions and 14 deletions

View File

@ -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<std::string> id = data_node.get_optional<std::string>("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()

View File

@ -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();

View File

@ -5,6 +5,7 @@
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/MainFrame.hpp"
#include "libslic3r_version.h"
#include "../Utils/Http.hpp"
#include <wx/sizer.h>
#include <wx/toolbar.h>
@ -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<void(std::string)> 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");

View File

@ -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<void(std::string)> callback);
int get_model_mall_detail_url(std::string *url, std::string id);
void update_mode();
private: