Commit 5985d6e1 by guanghui.cui

代码重构

parent 4fe108ec
...@@ -582,60 +582,36 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std ...@@ -582,60 +582,36 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::string &orderData, std::string &result) bool JsonModule::getOdsResponseData(const std::string &posResponse, const std::string &orderData, std::string &result)
{ {
int status_code; if( posResponse.empty() || orderData.empty() ){
std::string msg; LOG(INFO)<<"getOdsResponseData empty input";
std::string order_id;
int status;
if( posResponse.empty() || orderData.empty() )
return false; return false;
}
rapidjson::Document document, document1; rapidjson::Document document, document1;
document.Parse(posResponse.c_str()); document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str()); document1.Parse(orderData.c_str());
if (document.HasParseError() || document1.HasParseError()) if (document.HasParseError() || document1.HasParseError())
{ {
LOG(ERROR)<<"getOdsResponseData posResponse JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset(); LOG(INFO)<<"getOdsResponseData posResponse JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
return false; return false;
} }
if( !document.HasMember("status_code") if( !document.HasMember("fm_cmd") )
|| !document1.HasMember("order_id")
|| !document1.HasMember("status") )
{ {
LOG(INFO)<<"Don't have needed parames";
return false; return false;
} }
status_code = document["status_code"].GetInt(); int fm_cmd = document["fm_cmd"].GetInt();
msg = document["msg"].GetString(); bool rlt=false;
if(fm_cmd==REQUEST_TYPE_NEWORDER_PUSH){
order_id = document1["order_id"].GetString(); rlt=_getOrderResponseJson(posResponse,orderData,result);
status = document1["status"].GetInt(); }
else if(fm_cmd==REQUEST_TYPE_STOCK_WARN){
rapidjson::StringBuffer buffer; rlt=_getStockWarnResponseJson(posResponse,orderData,result);
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); }
writer.StartObject();
writer.Key("fm_cmd");
writer.Int(2);
writer.Key("order_id");
writer.String(order_id.c_str());
writer.Key("status");
writer.Int(status);
writer.Key("status_code");
writer.Int(status_code);
writer.Key("msg");
writer.String(msg.c_str());
writer.EndObject();
result = buffer.GetString(); return rlt;
return true;
} }
bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result) bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result)
...@@ -1367,3 +1343,104 @@ int JsonModule::getPushType(IN const char* data) ...@@ -1367,3 +1343,104 @@ int JsonModule::getPushType(IN const char* data)
} }
return 0; return 0;
} }
bool JsonModule::_getOrderResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result)
{
rapidjson::Document document, document1;
document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str());
if( !document.HasMember("status_code")
|| !document1.HasMember("order_id")
|| !document1.HasMember("status")
|| !document1.HasMember("channel") )
{
LOG(INFO)<<"Don't have needed parames";
return false;
}
int status_code = document["status_code"].GetInt();
std::string msg = document["msg"].GetString();
std::string order_id = document1["order_id"].GetString();
int status = document1["status"].GetInt();
std::string channel = document1["channel"].GetString();
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("fm_cmd");
writer.Int(2);
writer.Key("order_id");
writer.String(order_id.c_str());
writer.Key("channel");
writer.String(channel.c_str());
writer.Key("status");
writer.Int(status);
writer.Key("status_code");
writer.Int(status_code);
writer.Key("msg");
writer.String(msg.c_str());
writer.EndObject();
result = buffer.GetString();
return true;
}
bool JsonModule::_getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result)
{
//待接口确定
rapidjson::Document document, document1;
document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str());
if( !document.HasMember("status_code")
|| !document1.HasMember("order_id")
|| !document1.HasMember("status")
|| !document1.HasMember("channel") )
{
LOG(INFO)<<"Don't have needed parames";
return false;
}
int status_code = document["status_code"].GetInt();
std::string msg = document["msg"].GetString();
std::string order_id = document1["order_id"].GetString();
int status = document1["status"].GetInt();
std::string channel = document1["channel"].GetString();
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("fm_cmd");
writer.Int(1016);
writer.Key("order_id");
writer.String(order_id.c_str());
writer.Key("channel");
writer.String(channel.c_str());
writer.Key("status");
writer.Int(status);
writer.Key("status_code");
writer.Int(status_code);
writer.Key("msg");
writer.String(msg.c_str());
writer.EndObject();
result = buffer.GetString();
return true;
}
\ No newline at end of file
...@@ -89,6 +89,9 @@ private: ...@@ -89,6 +89,9 @@ private:
std::string _convertToOrderStatusJson(orderStatusObj &obj); std::string _convertToOrderStatusJson(orderStatusObj &obj);
std::string _convertToRefundJson(refundObj &obj); std::string _convertToRefundJson(refundObj &obj);
std::string _convertToStockWarnJson(stockWarnObj &obj); std::string _convertToStockWarnJson(stockWarnObj &obj);
bool _getOrderResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result);
bool _getStockWarnResponseJson(IN const std::string& posResponse, IN const std::string& orderData, OUT std::string& result);
}; };
#endif #endif
...@@ -240,11 +240,11 @@ int main() ...@@ -240,11 +240,11 @@ int main()
LOG(INFO) << "pos response data:"<<tmpBuf; LOG(INFO) << "pos response data:"<<tmpBuf;
std::string tmp(tmpBuf); std::string tmp(tmpBuf);
//pos发送过来的数据为gb2312编码,需要转换为utf8 //pos发送过来的数据为gb2312编码,需要转换为utf8
// std::string tmpUtf8= charset_g2u(tmp); std::string tmpUtf8= charset_g2u(tmp);
// LOG(INFO)<<"gb2312 to utf8:"<<back.data(); LOG(INFO)<<"gb2312 to utf8:"<<tmpUtf8.data();
// jsonTool.getOdsResponseData(tmpUtf8, odsPushData, responseData); jsonTool.getOdsResponseData(tmpUtf8, pushPosData, responseData);
jsonTool.getOdsResponseData(tmp, odsPushData, responseData); //jsonTool.getOdsResponseData(tmp, odsPushData, responseData);
}else }else
{ {
jsonTool.getOdsResponseData(101, "receive data from [POS] failed!", responseData); jsonTool.getOdsResponseData(101, "receive data from [POS] failed!", responseData);
...@@ -265,7 +265,10 @@ int main() ...@@ -265,7 +265,10 @@ int main()
} }
// TODO待加入重试机制 // TODO待加入重试机制
LOG(INFO) << "pos response data convert to ODS:"<<responseData; LOG(INFO) << "pos response data convert to ODS:"<<responseData.data();
//test
//responseData="{\"fm_cmd\": 2,\"order_id\" : \"1234\",\"status\" : 1,\"channel\" : \"1234\",\"status_code\" : 100,\"msg\" : \"\"}";
bool rlt = ods.send(responseData); bool rlt = ods.send(responseData);
LOG(INFO)<<"send result:"<<rlt; LOG(INFO)<<"send result:"<<rlt;
} }
......
...@@ -56,7 +56,7 @@ int charset_convert(const char* charset_from, const char* charset_to, const std: ...@@ -56,7 +56,7 @@ int charset_convert(const char* charset_from, const char* charset_to, const std:
char *outbuf = buffer; char *outbuf = buffer;
size_t outbytes = sizeof(buffer) - 1; size_t outbytes = sizeof(buffer) - 1;
ret = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes); ret = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
if (ret == 0) if (ret == 0 || errno == E2BIG)
{ {
string_to.append(buffer); string_to.append(buffer);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment