Commit 5985d6e1 by guanghui.cui

代码重构

parent 4fe108ec
......@@ -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)
{
int status_code;
std::string msg;
std::string order_id;
int status;
if( posResponse.empty() || orderData.empty() )
if( posResponse.empty() || orderData.empty() ){
LOG(INFO)<<"getOdsResponseData empty input";
return false;
}
rapidjson::Document document, document1;
document.Parse(posResponse.c_str());
document1.Parse(orderData.c_str());
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;
}
if( !document.HasMember("status_code")
|| !document1.HasMember("order_id")
|| !document1.HasMember("status") )
if( !document.HasMember("fm_cmd") )
{
LOG(INFO)<<"Don't have needed parames";
return false;
}
status_code = document["status_code"].GetInt();
msg = document["msg"].GetString();
order_id = document1["order_id"].GetString();
status = document1["status"].GetInt();
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("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;
int fm_cmd = document["fm_cmd"].GetInt();
bool rlt=false;
if(fm_cmd==REQUEST_TYPE_NEWORDER_PUSH){
rlt=_getOrderResponseJson(posResponse,orderData,result);
}
else if(fm_cmd==REQUEST_TYPE_STOCK_WARN){
rlt=_getStockWarnResponseJson(posResponse,orderData,result);
}
return rlt;
}
bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result)
......@@ -1366,4 +1342,105 @@ int JsonModule::getPushType(IN const char* data)
return document["fm_cmd"].GetInt();
}
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:
std::string _convertToOrderStatusJson(orderStatusObj &obj);
std::string _convertToRefundJson(refundObj &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
......@@ -240,11 +240,11 @@ int main()
LOG(INFO) << "pos response data:"<<tmpBuf;
std::string tmp(tmpBuf);
//pos发送过来的数据为gb2312编码,需要转换为utf8
// std::string tmpUtf8= charset_g2u(tmp);
// LOG(INFO)<<"gb2312 to utf8:"<<back.data();
// jsonTool.getOdsResponseData(tmpUtf8, odsPushData, responseData);
std::string tmpUtf8= charset_g2u(tmp);
LOG(INFO)<<"gb2312 to utf8:"<<tmpUtf8.data();
jsonTool.getOdsResponseData(tmpUtf8, pushPosData, responseData);
jsonTool.getOdsResponseData(tmp, odsPushData, responseData);
//jsonTool.getOdsResponseData(tmp, odsPushData, responseData);
}else
{
jsonTool.getOdsResponseData(101, "receive data from [POS] failed!", responseData);
......@@ -265,7 +265,10 @@ int main()
}
// 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);
LOG(INFO)<<"send result:"<<rlt;
}
......
......@@ -56,7 +56,7 @@ int charset_convert(const char* charset_from, const char* charset_to, const std:
char *outbuf = buffer;
size_t outbytes = sizeof(buffer) - 1;
ret = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
if (ret == 0)
if (ret == 0 || errno == E2BIG)
{
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