Commit f10cf669 by guanghui.cui

增加异常处理

parent f7536466
......@@ -69,6 +69,60 @@ JsonModule::~JsonModule()
}
const char* GetJsonStringSafe(rapidjson::Value& obj,const char* key)
{
if(obj.HasMember(key)){
rapidjson::Value& vObj = obj[key];
if(vObj.IsString()){
return vObj.GetString();
}
}
return "";
}
int GetJsonIntSafe(rapidjson::Value& obj,const char* key)
{
if(obj.HasMember(key)){
rapidjson::Value& vObj = obj[key];
if(vObj.IsInt()){
return vObj.GetInt();
}
}
return 0;
}
void JsonModule::jsonTest()
{
std::string json="{\"title\":\"PLAYER INFO\",\"num\":null,\"players\":[{ \"id\":123,\"name\":\"test\"}]}";
rapidjson::Document document; // 定义一个Document对象
document.Parse(json.data()); // 解析,Parse()无返回值,也不会抛异常
if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
{
// 可通过GetParseError()取得出错代码,
// 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
// 函数GetErrorOffset()返回出错发生的位置
LOG(ERROR)<<"JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
}
else
{
rapidjson::Value& vTitle = document["title"];
rapidjson::Value& vNum = document["num"];
LOG(INFO)<<"title:"<<GetJsonStringSafe(document,"title");
LOG(INFO)<<"num:"<<GetJsonStringSafe(document,"num");
rapidjson::Value& vPlayers = document["players"];
if(vPlayers.IsArray())
{
for(unsigned int i=0;i<vPlayers.Size();i++){
rapidjson::Value& players_obj = vPlayers[i];
LOG(INFO)<<"id:"<<GetJsonStringSafe(players_obj,"id");
LOG(INFO)<<"name:"<<GetJsonStringSafe(players_obj,"name");
}
}
}
}
bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
{
rapidjson::Document document; // 定义一个Document对象
......@@ -92,145 +146,85 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
order.offline_bonus=offlinePoints.GetBool();
}
rapidjson::Value& token = document["token"];
token.GetString();
rapidjson::Value& ver = document["ver"];
ver.GetInt();
GetJsonStringSafe(document,"token");
GetJsonStringSafe(document,"ver");
//配送信息
if(document.HasMember("delivery"))
{
rapidjson::Value& delivery_obj = document["delivery"];
if(delivery_obj.IsObject())
{
if(delivery_obj.HasMember("customerAddress"))
{
rapidjson::Value& customerAddress = delivery_obj["customerAddress"];
order.customerInfo.address=customerAddress.GetString();
}
if(delivery_obj.HasMember("customerName"))
{
rapidjson::Value& customerName = delivery_obj["customerName"];
order.customerInfo.name = customerName.GetString();
}
if(delivery_obj.HasMember("customerPhone"))
{
rapidjson::Value& customerPhone = delivery_obj["customerPhone"];
order.customerInfo.phone = customerPhone.GetString();
}
if(delivery_obj.HasMember("deliveryTime"))
{
rapidjson::Value& deliveryTime = delivery_obj["deliveryTime"];
order.delivery_time = deliveryTime.GetString();
}
if(delivery_obj.HasMember("name"))
{
rapidjson::Value& name = delivery_obj["name"];
order.deliveryInfo.driver_name = name.GetString();
}
if(delivery_obj.HasMember("phone"))
{
rapidjson::Value& phone = delivery_obj["phone"];
order.deliveryInfo.driver_phone = phone.GetString();
}
if(delivery_obj.HasMember("type"))
{
rapidjson::Value& type = delivery_obj["type"];
order.deliveryInfo.type = type.GetString();
order.customerInfo.address=GetJsonStringSafe(delivery_obj,"customerAddress");
order.customerInfo.name=GetJsonStringSafe(delivery_obj,"customerName");
order.customerInfo.phone=GetJsonStringSafe(delivery_obj,"customerPhone");
order.delivery_time=GetJsonStringSafe(delivery_obj,"deliveryTime");
order.deliveryInfo.driver_name=GetJsonStringSafe(delivery_obj,"name");
order.deliveryInfo.driver_phone=GetJsonStringSafe(delivery_obj,"phone");
order.deliveryInfo.type=GetJsonStringSafe(delivery_obj,"type");
}
}
}
//发票信息
if(document.HasMember("invoice"))
{
rapidjson::Value& invoice_obj = document["invoice"];
if(invoice_obj.IsObject())
{
if(invoice_obj.HasMember("companyName"))
{
rapidjson::Value& companyName = invoice_obj["companyName"];
companyName.GetString();
}
if(invoice_obj.HasMember("invoiceType"))
{
rapidjson::Value& invoiceType = invoice_obj["invoiceType"];
invoiceType.GetInt();
}
if(invoice_obj.HasMember("taxNum"))
{
rapidjson::Value& taxNum = invoice_obj["taxNum"];
taxNum.GetString();
}
if(invoice_obj.HasMember("title"))
{
rapidjson::Value& title = invoice_obj["title"];
title.GetString();
}
if(invoice_obj.HasMember("transNum"))
{
rapidjson::Value& transNum = invoice_obj["transNum"];
order.invoice_pickup_code = transNum.GetString();
}
}
GetJsonStringSafe(invoice_obj,"companyName");
GetJsonStringSafe(invoice_obj,"taxNum");
GetJsonStringSafe(invoice_obj,"title");
order.invoice_pickup_code = GetJsonStringSafe(invoice_obj,"transNum");
GetJsonIntSafe(invoice_obj,"invoiceType");
}
}
//订单内容
{
rapidjson::Value& orderContent_obj = document["orderContent"];
rapidjson::Value& createTime = orderContent_obj["createTime"];
order.create_time = createTime.GetString();
order.create_time = GetJsonStringSafe(orderContent_obj,"createTime");
//用户信息
//TODO 顾客信息为数组 因接口改版目前只有一个顾客信息
if(orderContent_obj.HasMember("customer"))
{
rapidjson::Value& customer_array = orderContent_obj["customer"];
if(customer_array.IsArray())
{
for(unsigned int i=0;i<customer_array.Size();i++){
rapidjson::Value& customer_obj = customer_array[i];
rapidjson::Value& accountId = customer_obj["accountId"];
order.customerInfo.account = accountId.GetString();
rapidjson::Value& accountType= customer_obj["accountType"];
order.customerInfo.account_type = atoi(accountType.GetString());
rapidjson::Value& level = customer_obj["level"];
order.customerInfo.account_level = level.GetString();
order.customerInfo.account = GetJsonStringSafe(customer_obj,"accountId");
order.customerInfo.account_type = atoi(GetJsonStringSafe(customer_obj,"accountType"));
order.customerInfo.account_level = GetJsonStringSafe(customer_obj,"level");
}
}
}
rapidjson::Value& deliveryFee = orderContent_obj["deliveryFee"];
order.delivery_price = deliveryFee.GetInt();
order.delivery_price = GetJsonIntSafe(orderContent_obj,"deliveryFee");
//订单信息
if(orderContent_obj.HasMember("orders"))
{
rapidjson::Value& orders_obj = orderContent_obj["orders"];
rapidjson::Value& deliveryStatus = orders_obj["deliveryStatus"];
order.deliveryInfo.status = atoi(deliveryStatus.GetString());
rapidjson::Value& deliveryStatusDesc = orders_obj["deliveryStatusDesc"];
order.deliveryInfo.status_desc = deliveryStatusDesc.GetString();
rapidjson::Value& orderId = orders_obj["orderId"];
order.order_id = orderId.GetString();
rapidjson::Value& pickupCode = orders_obj["pickupCode"];
order.pickup_code= pickupCode.GetString();
rapidjson::Value& pickupPoint = orders_obj["pickupPoint"];
order.pickup_point = pickupPoint.GetString();
rapidjson::Value& remark = orders_obj["remark"];
remark.GetString();
rapidjson::Value& status = orders_obj["status"];
order.status = atoi(status.GetString());
order.deliveryInfo.status = atoi(GetJsonStringSafe(orders_obj,"deliveryStatus"));
order.deliveryInfo.status_desc = GetJsonStringSafe(orders_obj,"deliveryStatusDesc");
order.order_id = GetJsonStringSafe(orders_obj,"orderId");
order.pickup_code= GetJsonStringSafe(orders_obj,"pickupCode");
order.pickup_point = GetJsonStringSafe(orders_obj,"pickupPoint");
GetJsonStringSafe(orders_obj,"remark");
order.status = atoi(GetJsonStringSafe(orders_obj,"status"));
order.ods_status=order.status;
rapidjson::Value& statusDesc = orders_obj["statusDesc"];
order.status_desc = statusDesc.GetString();
order.status_desc = GetJsonStringSafe(orders_obj,"statusDesc");
//订单商品
if(orders_obj.HasMember("products"))
{
rapidjson::Value& products_array = orders_obj["products"];
if(products_array.IsArray())
......@@ -240,34 +234,18 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
productAttr structProduct;
rapidjson::Value& attributes = product_obj["attributes"];
structProduct.pro.attributes = attributes.GetString();
rapidjson::Value& catgId= product_obj["catgId"];
structProduct.pro.combo_id = catgId.GetString();
rapidjson::Value& groupIndex = product_obj["groupIndex"];
structProduct.pro.bom_id = groupIndex.GetString();
rapidjson::Value& name = product_obj["name"];
structProduct.pro.name = name.GetString();
rapidjson::Value& price= product_obj["price"];
structProduct.pro.price = price.GetInt();
rapidjson::Value& productType = product_obj["productType"];
productType.GetString();
rapidjson::Value& qty = product_obj["qty"];
structProduct.pro.qty = qty.GetInt();
rapidjson::Value& sku= product_obj["sku"];
structProduct.pro.sku = sku.GetString();
rapidjson::Value& source = product_obj["source"];
structProduct.pro.source = source.GetString();
structProduct.pro.attributes = GetJsonStringSafe(product_obj,"attributes");
structProduct.pro.combo_id = GetJsonStringSafe(product_obj,"catgId");
structProduct.pro.bom_id = GetJsonStringSafe(product_obj,"groupIndex");
structProduct.pro.name = GetJsonStringSafe(product_obj,"name");
structProduct.pro.price = GetJsonIntSafe(product_obj,"price");
GetJsonStringSafe(product_obj,"productType");
structProduct.pro.qty = GetJsonIntSafe(product_obj,"qty");
structProduct.pro.sku = GetJsonStringSafe(product_obj,"sku");
structProduct.pro.source = GetJsonStringSafe(product_obj,"source");
//子商品信息
if(product_obj.HasMember("specs"))
{
rapidjson::Value& specs_array = product_obj["specs"];
if(specs_array.IsArray())
......@@ -276,18 +254,11 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
rapidjson::Value& specs_obj = specs_array[i];
productSpec structProductSpec;
structProductSpec.name = GetJsonStringSafe(specs_obj,"name");
structProductSpec.price = GetJsonIntSafe(specs_obj,"price");
structProductSpec.qty = GetJsonIntSafe(specs_obj,"qty");
structProductSpec.sku = GetJsonStringSafe(specs_obj,"sku");
rapidjson::Value& name = specs_obj["name"];
structProductSpec.name = name.GetString();
rapidjson::Value& price= specs_obj["price"];
structProductSpec.price = price.GetInt();
rapidjson::Value& qty = specs_obj["qty"];
structProductSpec.qty = qty.GetInt();
rapidjson::Value& sku = specs_obj["sku"];
structProductSpec.sku = sku.GetString();
structProduct.vecSpec.push_back(structProductSpec);
}
}
......@@ -298,8 +269,8 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
}
}
//支付信息
if(orderContent_obj.HasMember("payInfos"))
{
//TODO
//sumary未映射字段值
rapidjson::Value& payInfos_array = orderContent_obj["payInfos"];
if(payInfos_array.IsArray())
......@@ -308,39 +279,30 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
rapidjson::Value& payInfos_obj = payInfos_array[i];
paymentDetail detail;
rapidjson::Value& amount= payInfos_obj["amount"];
detail.amount = amount.GetInt();
detail.amount = GetJsonIntSafe(payInfos_obj,"amount");
//支付类型需要拼接 channel+payType
rapidjson::Value& payType = payInfos_obj["payType"];
std::string _strType=order.channel+payType.GetString();
std::string _strType=order.channel+GetJsonStringSafe(payInfos_obj,"payType");
detail.type = atoi(_strType.data());
rapidjson::Value& transNum = payInfos_obj["transNum"];
detail.trans_id = transNum.GetString();
if(payInfos_obj.HasMember("accountId")){
rapidjson::Value& accountId = payInfos_obj["accountId"];
if(!accountId.IsNull()){
detail.account_id=accountId.GetString();
}
}
detail.trans_id = GetJsonStringSafe(payInfos_obj,"transNum");
detail.account_id=GetJsonStringSafe(payInfos_obj,"accountId");
order.payInfo.vecDetail.push_back(detail);
}
}
}
rapidjson::Value& payType = orderContent_obj["payType"];
payType.GetString();
GetJsonStringSafe(orderContent_obj,"payType");
//积分信息
if(orderContent_obj.HasMember("points"))
{
rapidjson::Value& points_obj = orderContent_obj["points"];
if(points_obj.IsObject())
{
rapidjson::Value& totalPoint= points_obj["totalPoint"];
order.bonusInfo.summary = std::to_string(totalPoint.GetInt());
order.bonusInfo.summary = std::to_string(GetJsonIntSafe(points_obj,"totalPoint"));
//积分信息详情
if(points_obj.HasMember("pointDetails"))
{
rapidjson::Value& pointDetails_array = points_obj["pointDetails"];
if(pointDetails_array.IsArray())
......@@ -349,26 +311,13 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
rapidjson::Value& pointDetails_obj = pointDetails_array[i];
bonusDetail detail;
rapidjson::Value& bomId= pointDetails_obj["bomId"];
bomId.GetString();
rapidjson::Value& comboId = pointDetails_obj["comboId"];
comboId.GetString();
rapidjson::Value& desc = pointDetails_obj["desc"];
detail.desc = desc.GetString();
rapidjson::Value& groupId = pointDetails_obj["groupId"];
groupId.GetString();
rapidjson::Value& point = pointDetails_obj["point"];
detail.point = point.GetInt();
rapidjson::Value& sku = pointDetails_obj["sku"];
detail.sku = sku.GetString();
rapidjson::Value& type = pointDetails_obj["type"];
detail.type = atoi(type.GetString());
GetJsonStringSafe(pointDetails_obj,"bomId");
GetJsonStringSafe(pointDetails_obj,"comboId");
detail.desc = GetJsonStringSafe(pointDetails_obj,"desc");
GetJsonStringSafe(pointDetails_obj,"groupId");
detail.point = GetJsonIntSafe(pointDetails_obj,"point");
detail.sku = GetJsonStringSafe(pointDetails_obj,"sku");
detail.type = atoi(GetJsonStringSafe(pointDetails_obj,"type"));
order.bonusInfo.vecDetail.push_back(detail);
}
......@@ -378,18 +327,17 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
}
//优惠信息
if(orderContent_obj.HasMember("promotions"))
{
rapidjson::Value& promotions_obj = orderContent_obj["promotions"];
if(promotions_obj.IsObject())
{
rapidjson::Value& totalDiscount= promotions_obj["totalDiscount"];
totalDiscount.GetInt();
rapidjson::Value& totalOriginalPrice= promotions_obj["totalOriginalPrice"];
totalOriginalPrice.GetInt();
rapidjson::Value& totalPrmotionPrice= promotions_obj["totalPrmotionPrice"];
totalPrmotionPrice.GetInt();
GetJsonIntSafe(promotions_obj,"totalDiscount");
GetJsonIntSafe(promotions_obj,"totalOriginalPrice");
GetJsonIntSafe(promotions_obj,"totalPrmotionPrice");
//优惠信息详情
if(promotions_obj.HasMember("promtionDetails"))
{
rapidjson::Value& promtionDetails_array = promotions_obj["promtionDetails"];
if(promtionDetails_array.IsArray())
......@@ -397,102 +345,54 @@ bool JsonModule::getPushOrders(IN const char* json,OUT orderObj &order)
for(unsigned int i=0;i<promtionDetails_array.Size();i++){
rapidjson::Value& promtionDetails_obj = promtionDetails_array[i];
rapidjson::Value& bomId= promtionDetails_obj["bomId"];
bomId.GetString();
rapidjson::Value& comboId = promtionDetails_obj["comboId"];
comboId.GetString();
rapidjson::Value& desc = promtionDetails_obj["desc"];
desc.GetString();
rapidjson::Value& discount = promtionDetails_obj["discount"];
discount.GetInt();
rapidjson::Value& groupId = promtionDetails_obj["groupId"];
groupId.GetString();
rapidjson::Value& originalPrice = promtionDetails_obj["originalPrice"];
originalPrice.GetInt();
rapidjson::Value& pcode = promtionDetails_obj["pcode"];
pcode.GetString();
rapidjson::Value& prmotionPrice = promtionDetails_obj["prmotionPrice"];
prmotionPrice.GetInt();
rapidjson::Value& sku = promtionDetails_obj["sku"];
sku.GetString();
rapidjson::Value& type = promtionDetails_obj["type"];
type.GetString();
GetJsonStringSafe(promtionDetails_obj,"bomId");
GetJsonStringSafe(promtionDetails_obj,"comboId");
GetJsonStringSafe(promtionDetails_obj,"desc");
GetJsonIntSafe(promtionDetails_obj,"discount");
GetJsonStringSafe(promtionDetails_obj,"groupId");
GetJsonIntSafe(promtionDetails_obj,"originalPrice");
GetJsonStringSafe(promtionDetails_obj,"pcode");
GetJsonIntSafe(promtionDetails_obj,"prmotionPrice");
GetJsonStringSafe(promtionDetails_obj,"sku");
GetJsonStringSafe(promtionDetails_obj,"type");
}
}
}
}
}
rapidjson::Value& sessionId = orderContent_obj["sessionId"];
sessionId.GetString();
rapidjson::Value& totalAmount = orderContent_obj["totalAmount"];
order.total_price = totalAmount.GetInt();
rapidjson::Value& totalDiscount = orderContent_obj["totalDiscount"];
order.reduced_price = totalDiscount.GetInt();
GetJsonStringSafe(orderContent_obj,"sessionId");
order.total_price = GetJsonIntSafe(orderContent_obj,"totalAmount");
order.reduced_price = GetJsonIntSafe(orderContent_obj,"totalDiscount");
}
//第三方商户信息
if(document.HasMember("sellerInfo"))
{
rapidjson::Value& sellerInfo_obj = document["sellerInfo"];
if(sellerInfo_obj.IsObject())
{
if(sellerInfo_obj.HasMember("sellerId"))
{
rapidjson::Value& sellerId = sellerInfo_obj["sellerId"];
sellerId.GetString();
}
if(sellerInfo_obj.HasMember("sellerName"))
{
rapidjson::Value& sellerName = sellerInfo_obj["sellerName"];
sellerName.GetString();
}
GetJsonStringSafe(sellerInfo_obj,"sellerId");
GetJsonStringSafe(sellerInfo_obj,"sellerName");
}
}
//门店信息
if(document.HasMember("shopInfo"))
{
rapidjson::Value& shopInfo_obj = document["shopInfo"];
if(shopInfo_obj.IsObject()){
if(shopInfo_obj.HasMember("barCounter")){
rapidjson::Value& barCounter = shopInfo_obj["barCounter"];
barCounter.GetString();
}
if(shopInfo_obj.HasMember("operator")){
rapidjson::Value& vOperator = shopInfo_obj["operator"];
vOperator.GetString();
}
if(shopInfo_obj.HasMember("posId")){
rapidjson::Value& posId = shopInfo_obj["posId"];
order.storeInfo.pos_id = posId.GetString();
}
if(shopInfo_obj.HasMember("storeId")){
rapidjson::Value& storeId = shopInfo_obj["storeId"];
order.storeInfo.store_id = storeId.GetString();
}
GetJsonStringSafe(shopInfo_obj,"barCounter");
GetJsonStringSafe(shopInfo_obj,"operator");
order.storeInfo.pos_id = GetJsonStringSafe(shopInfo_obj,"posId");
order.storeInfo.store_id = GetJsonStringSafe(shopInfo_obj,"storeId");
//自助机信息
rapidjson::Value& selfHelpMac_obj = shopInfo_obj["selfHelpMac"];
if(selfHelpMac_obj.IsObject()){
rapidjson::Value& id = selfHelpMac_obj["id"];
order.storeInfo.vem_id = id.GetString();
rapidjson::Value& shelf = selfHelpMac_obj["shelf"];
order.storeInfo.vem_shelf = shelf.GetString();
order.storeInfo.vem_id = GetJsonStringSafe(selfHelpMac_obj,"id");
order.storeInfo.vem_shelf = GetJsonStringSafe(selfHelpMac_obj,"shelf");
}
}
}
......@@ -732,21 +632,18 @@ std::string JsonModule::_convertToOrderOperationReponseJson(IN const char* json)
std::string code = document["code"].GetString();
status_code=atoi(code.c_str());
rapidjson::Value& message = document["message"];
if(!message.IsNull()){
msg=message.GetString();
}
msg = GetJsonStringSafe(document,"message");
if( !document.HasMember("status")){
status=message.GetInt();
}
if( !document.HasMember("status_desc")){
rapidjson::Value& vStatus_desc = document["status_desc"];
if(!vStatus_desc.IsNull()){
status_desc=vStatus_desc.GetString();
if(document.HasMember("result"))
{
rapidjson::Value& result_obj = document["result"];
if(result_obj.IsObject())
{
status = atoi(GetJsonStringSafe(result_obj,"status"));
status_desc = GetJsonStringSafe(result_obj,"status_desc");
}
}
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
......@@ -1517,6 +1414,9 @@ std::string JsonModule::_convertPosOperationToOdsJson(orderOperationObj &operati
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("fm_cmd");
writer.Int(operation_obj.fm_cmd);
writer.Key("channel");
writer.String(operation_obj.channel.c_str());
......
......@@ -14,6 +14,8 @@ public:
JsonModule();
~JsonModule();
void jsonTest();
bool getPushOrders(IN const char* json,OUT orderObj &order);
/* 功能:判断是否是初始化数据
......
......@@ -134,6 +134,7 @@ void* listen_pos_func(void* arg)
// 将POS请求数据转换为中台可接受数据格式
if( jsonTool.convertDataPos2Ods(posRequestData, requestOdsData) )
{
LOG(INFO)<<"convert pos data to ods:"<<requestOdsData.data();
// 同步阻塞发送到ODS并等待返回
TCPClient ods;
if( ods.doConnect(ods_recv_port, ods_ip.c_str()) )
......@@ -143,6 +144,7 @@ void* listen_pos_func(void* arg)
std::string tmp;
if( ods.receive(tmp) )
{
LOG(INFO)<<"receive ods back:"<<tmp.data();
jsonTool.getPosResponseData(tmp,posRequestData, responseData);
}else
{
......@@ -165,7 +167,7 @@ void* listen_pos_func(void* arg)
}
// TODO待加入重试机制
LOG(INFO) << "ODS response data:"<<responseData.data();
LOG(INFO) << "ODS response data send to pos:"<<responseData.data();
pos.write(responseData.c_str());
pos.close();
if(reqType==REQUEST_TYPE_INIT){
......
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