Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
familyMart_takeaway
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guanghui.cui
familyMart_takeaway
Commits
151657fa
Commit
151657fa
authored
Mar 24, 2018
by
guanghui.cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pos操作ods返回处理
parent
5184cd29
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
235 additions
and
54 deletions
+235
-54
src/JsonModule.cpp
+220
-48
src/JsonModule.h
+11
-3
src/main.cpp
+4
-3
No files found.
src/JsonModule.cpp
View file @
151657fa
...
...
@@ -538,6 +538,56 @@ bool JsonModule::checkInitData(const std::string &data, int &posListenPort)
return
true
;
}
bool
JsonModule
::
convertInitDataPos2Ods
(
IN
const
std
::
string
&
data
,
OUT
std
::
string
&
result
)
{
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
data
.
c_str
());
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
LOG
(
ERROR
)
<<
"convertInitDataPos2Ods JSON parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
return
false
;
}
int
fm_cmd
=
document
[
"fm_cmd"
].
GetInt
();
int
listen_port
=
document
[
"listen_port"
].
GetInt
();
std
::
string
store_id
=
document
[
"store_id"
].
GetString
();
std
::
string
pos_id
=
document
[
"pos_id"
].
GetString
();
std
::
string
operator_id
=
document
[
"operator_id"
].
GetString
();
bool
is_master
=
document
[
"is_master"
].
GetBool
();
rapidjson
::
StringBuffer
buffer
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
writer
.
Key
(
"fm_cmd"
);
writer
.
Int
(
fm_cmd
);
writer
.
Key
(
"listen_port"
);
writer
.
Int
(
listen_port
);
writer
.
Key
(
"storeId"
);
//中台命名
writer
.
String
(
store_id
.
data
());
writer
.
Key
(
"pos_id"
);
writer
.
String
(
pos_id
.
data
());
writer
.
Key
(
"operator_id"
);
writer
.
String
(
operator_id
.
data
());
writer
.
Key
(
"is_master"
);
writer
.
Bool
(
is_master
);
writer
.
Key
(
"ver"
);
writer
.
Int
(
1
);
writer
.
EndObject
();
return
buffer
.
GetString
();
return
true
;
}
bool
JsonModule
::
getInitBackData
(
IN
const
char
*
inJson
,
OUT
std
::
string
&
outJson
)
{
rapidjson
::
Document
document
;
// 定义一个Document对象
...
...
@@ -617,17 +667,97 @@ bool JsonModule::getPosResponseData(int status, const std::string &msg, std::str
return
true
;
}
bool
JsonModule
::
getPosResponseData
(
const
std
::
string
&
odsResponse
,
std
::
string
&
result
)
bool
JsonModule
::
getPosResponseData
(
const
std
::
string
&
odsResponse
,
IN
const
std
::
string
&
posReq
,
std
::
string
&
result
)
{
serverResponseOperationObj
obj
;
if
(
_getServerResponseData
(
odsResponse
.
data
(),
obj
)){
result
=
_convertServerResponseToJson
(
obj
);
return
true
;
bool
rlt
=
false
;
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
posReq
.
c_str
());
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
LOG
(
ERROR
)
<<
"getPosResponseData JSON parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
return
false
;
}
return
false
;
if
(
!
document
.
HasMember
(
"fm_cmd"
))
{
LOG
(
INFO
)
<<
"Don't have fm_cmd"
;
return
false
;
}
int
fm_cmd
=
document
[
"fm_cmd"
].
GetInt
();
if
(
fm_cmd
==
OPERATION_POS_CONFIRM
||
fm_cmd
==
OPERATION_POS_CANCEL
||
fm_cmd
==
OPERATION_POS_REFUND_AGREE
||
fm_cmd
==
OPERATION_POS_REFUND_DISAGREE
)
{
result
=
_convertToOrderOperationReponseJson
(
odsResponse
.
data
());
rlt
=
true
;
}
else
if
(
fm_cmd
==
REQUEST_TYPE_POS_PRIORITY
){
}
// serverResponseOperationObj obj;
// if(_getServerResponseData(odsResponse.data(),obj)){
// result=_convertServerResponseToJson(obj);
// return true;
// }
return
rlt
;
//result = "{\"status_code\":100, \"msg\":\"\"}";
}
std
::
string
JsonModule
::
_convertToOrderOperationReponseJson
(
IN
const
char
*
json
)
{
int
status_code
=
0
,
status
=
0
;
std
::
string
msg
,
status_desc
;
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
json
);
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
LOG
(
ERROR
)
<<
"_convertToOrderOperationReponseJson JSON parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
}
else
{
std
::
string
code
=
document
[
"code"
].
GetString
();
status_code
=
atoi
(
code
.
c_str
());
rapidjson
::
Value
&
message
=
document
[
"message"
];
if
(
!
message
.
IsNull
()){
msg
=
message
.
GetString
();
}
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
();
}
}
}
rapidjson
::
StringBuffer
buffer
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
writer
.
Key
(
"status_code"
);
writer
.
Int
(
status_code
);
writer
.
Key
(
"msg"
);
writer
.
String
(
msg
.
c_str
());
writer
.
Key
(
"status"
);
writer
.
Int
(
status
);
writer
.
Key
(
"status_desc"
);
writer
.
String
(
status_desc
.
data
());
writer
.
EndObject
();
return
buffer
.
GetString
();
}
bool
JsonModule
::
getOdsResponseData
(
int
status_code
,
const
std
::
string
&
msg
,
std
::
string
&
result
)
{
/*
...
...
@@ -728,7 +858,11 @@ bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result
LOG
(
INFO
)
<<
"fm_cmd:"
<<
fm_cmd
;
bool
rlt
=
false
;
if
(
fm_cmd
==
OPERATION_POS_CONFIRM
||
fm_cmd
==
OPERATION_POS_CANCEL
||
fm_cmd
==
OPERATION_POS_REFUND_AGREE
||
fm_cmd
==
OPERATION_POS_REFUND_DISAGREE
){
if
(
fm_cmd
==
OPERATION_POS_CONFIRM
||
fm_cmd
==
OPERATION_POS_CANCEL
||
fm_cmd
==
OPERATION_POS_REFUND_AGREE
||
fm_cmd
==
OPERATION_POS_REFUND_DISAGREE
)
{
orderOperationObj
obj
;
if
(
_getPOSOperationObj
(
data
.
data
(),
obj
)){
result
=
_convertPosOperationToOdsJson
(
obj
);
...
...
@@ -739,7 +873,7 @@ bool JsonModule::convertDataPos2Ods(const std::string &data, std::string &result
}
else
if
(
fm_cmd
==
REQUEST_TYPE_POS_PRIORITY
){
rlt
=
_convertPOSReqPriorityJson
(
data
,
result
);
}
...
...
@@ -1393,53 +1527,53 @@ std::string JsonModule::_convertPosOperationToOdsJson(orderOperationObj &operati
return
buffer
.
GetString
();
}
bool
JsonModule
::
_getServerResponseData
(
IN
const
char
*
json
,
OUT
serverResponseOperationObj
&
obj
)
{
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
json
);
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
// 可通过GetParseError()取得出错代码,
// 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
// 函数GetErrorOffset()返回出错发生的位置
LOG
(
ERROR
)
<<
"_getServerResponseData JSON parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
}
else
{
rapidjson
::
Value
&
code
=
document
[
"code"
];
obj
.
code
=
code
.
GetString
();
rapidjson
::
Value
&
message
=
document
[
"message"
];
obj
.
message
=
message
.
GetString
();
rapidjson
::
Value
&
ver
=
document
[
"ver"
];
obj
.
ver
=
ver
.
GetString
();
return
true
;
}
return
false
;
}
//
bool JsonModule::_getServerResponseData(IN const char* json,OUT serverResponseOperationObj &obj)
//
{
//
rapidjson::Document document; // 定义一个Document对象
//
document.Parse(json); // 解析,Parse()无返回值,也不会抛异常
//
if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
//
{
//
// 可通过GetParseError()取得出错代码,
//
// 注意GetParseError()返回的是一个rapidjson::ParseErrorCode类型的枚举值
//
// 使用函数rapidjson::GetParseError_En()得到错误码的字符串说明,这里的En为English简写
//
// 函数GetErrorOffset()返回出错发生的位置
//
LOG(ERROR)<<"_getServerResponseData JSON parse error:"<<document.GetParseError()<<":"<<document.GetErrorOffset();
//
}
//
else
//
{
//
rapidjson::Value& code = document["code"];
//
obj.code=code.GetString();
//
rapidjson::Value& message = document["message"];
//
obj.message=message.GetString();
//
rapidjson::Value& ver = document["ver"];
//
obj.ver=ver.GetString();
//
return true;
//
}
//
return false;
//
}
std
::
string
JsonModule
::
_convertServerResponseToJson
(
serverResponseOperationObj
&
response_obj
)
{
rapidjson
::
StringBuffer
buffer
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
//
std::string JsonModule::_convertServerResponseToJson(serverResponseOperationObj &response_obj)
//
{
//
rapidjson::StringBuffer buffer;
//
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
//
writer.StartObject();
writer
.
Key
(
"status_code"
);
writer
.
Int
(
atoi
(
response_obj
.
code
.
data
()));
//
writer.Key("status_code");
//
writer.Int(atoi(response_obj.code.data()));
writer
.
Key
(
"msg"
);
writer
.
String
(
""
);
//
writer.Key("msg");
//
writer.String("");
writer
.
Key
(
"status"
);
writer
.
Int
(
0
);
//
writer.Key("status");
//
writer.Int(0);
writer
.
Key
(
"status_desc"
);
writer
.
String
(
""
);
//
writer.Key("status_desc");
//
writer.String("");
writer
.
EndObject
();
//
writer.EndObject();
return
buffer
.
GetString
();
}
//
return buffer.GetString();
//
}
void
JsonModule
::
_getStockWarnObj
(
IN
const
char
*
json
,
OUT
stockWarnObj
&
warn_obj
)
{
...
...
@@ -1742,4 +1876,41 @@ int JsonModule::_getODSStatusByPOSReq(int fm_cmd)
break
;
}
return
rlt
;
}
bool
JsonModule
::
_convertPOSReqPriorityJson
(
IN
const
std
::
string
&
posReq
,
OUT
const
std
::
string
&
odsJson
)
{
std
::
string
pos_id
;
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
posReq
.
data
());
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
LOG
(
ERROR
)
<<
"JSON _convertPOSReqPriorityJson parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
return
false
;
}
else
{
rapidjson
::
Value
&
vPosId
=
document
[
"pos_id"
];
pos_id
=
vPosId
.
GetString
();
}
// rapidjson::StringBuffer buffer;
// rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
// writer.StartObject();
// writer.Key("fm_cmd");
// writer.Int(REQUEST_TYPE_COMMON_WARN);
// writer.Key("notifyDate");
// writer.String(notifyDate.c_str());
// writer.Key("notifyDesc");
// writer.String(notifyDesc.c_str());
// writer.EndObject();
//odsJson = buffer.GetString();
return
true
;
}
\ No newline at end of file
src/JsonModule.h
View file @
151657fa
...
...
@@ -40,13 +40,19 @@ public:
* */
bool
checkInitData
(
IN
const
std
::
string
&
data
,
OUT
int
&
posListenPort
);
/* 功能:转换POS初始化数据格式到中台数据格式
* 参数:[1]POS数据格式[2]转换后数据
* 返回:是否转换成功
* */
bool
convertInitDataPos2Ods
(
IN
const
std
::
string
&
data
,
OUT
std
::
string
&
result
);
/* 功能:获取POS请求的返回数据
* 参数:[1]状态码[2]消息[3]转换后数据
* 重载:[1]ODS返回数据[2]转换后数据
* 返回:是否转换成功
* */
bool
getPosResponseData
(
IN
int
status
,
IN
const
std
::
string
&
msg
,
OUT
std
::
string
&
result
);
bool
getPosResponseData
(
IN
const
std
::
string
&
odsResponse
,
OUT
std
::
string
&
result
);
bool
getPosResponseData
(
IN
const
std
::
string
&
odsResponse
,
IN
const
std
::
string
&
posReq
,
OUT
std
::
string
&
result
);
/* 功能:获取ODS推送的返回数据
* 参数:[1]状态码[2]消息[3]转换后数据
...
...
@@ -100,14 +106,16 @@ private:
std
::
string
_convertPosOperationToOdsJson
(
orderOperationObj
&
operation_obj
);
//服务器返回值(针对pos操作订单请求)
bool
_getServerResponseData
(
IN
const
char
*
json
,
OUT
serverResponseOperationObj
&
obj
);
std
::
string
_convertServerResponseToJson
(
serverResponseOperationObj
&
operation_obj
);
//
bool _getServerResponseData(IN const char* json,OUT serverResponseOperationObj &obj);
//
std::string _convertServerResponseToJson(serverResponseOperationObj &operation_obj);
std
::
string
_convertToNewOrderJson
(
orderObj
&
obj
);
std
::
string
_convertToOrderStatusJson
(
orderStatusObj
&
obj
);
std
::
string
_convertToRefundJson
(
refundObj
&
obj
);
std
::
string
_convertToStockWarnJson
(
stockWarnObj
&
obj
);
std
::
string
_convertToCommonWarnJson
(
IN
const
char
*
json
);
std
::
string
_convertToOrderOperationReponseJson
(
IN
const
char
*
json
);
bool
_convertPOSReqPriorityJson
(
IN
const
std
::
string
&
posReq
,
OUT
const
std
::
string
&
odsJson
);
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
,
IN
const
std
::
string
&
odsData
,
OUT
std
::
string
&
result
);
...
...
src/main.cpp
View file @
151657fa
...
...
@@ -77,8 +77,9 @@ void* listen_pos_func(void* arg)
bInitDone
=
false
;
if
(
jsonTool
.
checkInitData
(
posRequestData
,
pos_listen_port
)
)
{
g_init_data
=
posRequestData
;
jsonTool
.
getPosResponseData
(
100
,
"successful!"
,
responseData
);
//g_init_data = posRequestData;
jsonTool
.
convertInitDataPos2Ods
(
posRequestData
,
g_init_data
);
//jsonTool.getPosResponseData(100, "successful!", responseData);
LOG
(
INFO
)
<<
"POS init data:"
<<
g_init_data
.
c_str
();
//等待ods返回初始化结果
while
(
true
){
...
...
@@ -111,7 +112,7 @@ void* listen_pos_func(void* arg)
std
::
string
tmp
;
if
(
ods
.
receive
(
tmp
)
)
{
jsonTool
.
getPosResponseData
(
tmp
,
responseData
);
jsonTool
.
getPosResponseData
(
tmp
,
posRequestData
,
responseData
);
}
else
{
jsonTool
.
getPosResponseData
(
101
,
"receive data from [ODS] failed!"
,
responseData
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment