chore: 修复 Windows 下公招结果上传失败的问题

This commit is contained in:
zzyyyl
2022-08-19 14:07:32 +08:00
parent 1b34381f45
commit f18b7cd514
2 changed files with 50 additions and 5 deletions

View File

@@ -134,7 +134,7 @@ namespace asst::utils
#ifdef _WIN32
const char* src_str = utf8_str.c_str();
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0);
const std::size_t wsz_ansi_length = static_cast<std::size_t>(len) + 1;
const std::size_t wsz_ansi_length = static_cast<std::size_t>(len) + 1U;
auto wsz_ansi = new wchar_t[wsz_ansi_length];
memset(wsz_ansi, 0, sizeof(wsz_ansi[0]) * wsz_ansi_length);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wsz_ansi, len);
@@ -157,6 +157,39 @@ namespace asst::utils
#endif
}
#ifdef _WIN32
inline std::string utf8_to_unicode_escape(const std::string& utf8_str)
{
const char* src_str = utf8_str.c_str();
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0);
const std::size_t wstr_length = static_cast<std::size_t>(len) + 1U;
auto wstr = new wchar_t[wstr_length];
memset(wstr, 0, sizeof(wstr[0]) * wstr_length);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wstr, len);
std::string unicode_escape_str = {};
constexpr char hexcode[] = "0123456789abcdef";
for (const wchar_t* pchr = wstr; *pchr; ++pchr) {
const wchar_t chr = *pchr;
if (chr > 255) {
unicode_escape_str += "\\u";
unicode_escape_str.push_back(hexcode[chr >> 12]);
unicode_escape_str.push_back(hexcode[(chr >> 8) & 15]);
unicode_escape_str.push_back(hexcode[(chr >> 4) & 15]);
unicode_escape_str.push_back(hexcode[chr & 15]);
}
else {
unicode_escape_str.push_back(chr & 255);
}
}
delete[] wstr;
wstr = nullptr;
return unicode_escape_str;
}
#endif
template <typename RetTy, typename ArgType>
constexpr inline RetTy make_rect(const ArgType& rect)
{

View File

@@ -635,13 +635,19 @@ void asst::AutoRecruitTask::upload_to_penguin(const json::value& details)
std::string body_escape = utils::string_replace_all_batch(body.to_string(), {
{"\"", "\\\""} });
#ifdef _WIN32
std::string body_escapes = utils::utf8_to_unicode_escape(body_escape);
#else
std::string body_escapes = body_escape;
#endif
std::string extra_param;
if (!m_penguin_id.empty()) {
extra_param = "-H \"authorization: PenguinID " + m_penguin_id + "\"";
}
std::string cmd_line = utils::string_replace_all_batch(opt.penguin_report.cmd_format, {
{ "[body]", body_escape },
{ "[extra]", extra_param} });
{ "[body]", body_escapes },
{ "[extra]", extra_param } });
Log.trace("request_penguin |", cmd_line);
@@ -679,9 +685,15 @@ void asst::AutoRecruitTask::upload_to_yituliu(const json::value& details)
std::string body_escape = utils::string_replace_all_batch(body.to_string(), {
{"\"", "\\\""} });
#ifdef _WIN32
std::string body_escapes = utils::utf8_to_unicode_escape(body_escape);
#else
std::string body_escapes = body_escape;
#endif
std::string cmd_line = utils::string_replace_all_batch(opt.yituliu_report.cmd_format, {
{ "[body]", body_escape },
{ "[extra]", ""} });
{ "[body]", body_escapes },
{ "[extra]", "" } });
Log.trace("request_yituliu |", cmd_line);