7
7
#include " msg_controller.h"
8
8
using namespace drogon ;
9
9
10
- typedef std::string (*HandlerFunc)(const Json::Value&);
10
+ typedef void (*HandlerFunc)(const Json::Value&, std::string*, int * );
11
11
12
12
void Handle (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback, HandlerFunc handler)
13
13
{
@@ -24,9 +24,15 @@ void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
24
24
}
25
25
26
26
Json::FastWriter writer;
27
- std::string msg = handler (req_json);
27
+
28
+ std::string msg;
29
+ int code;
28
30
31
+ handler (req_json, &msg, &code);
32
+
29
33
res_json[" msg" ] = msg;
34
+ res_json[" code" ] = code;
35
+
30
36
if (msg.find (" Success" )!= std::string::npos)
31
37
{
32
38
res_json[" token" ] = jwtGen (req_json);
@@ -60,21 +66,33 @@ std::string sha256(const std::string str)
60
66
return ss.str ();
61
67
}
62
68
63
- std::string registerUser (const Json::Value& req_json)
69
+ void registerUser (const Json::Value& req_json, std::string* msg, int * code )
64
70
{
65
71
if (sql_check (req_json[" username" ].asString ()))
66
72
{
67
73
sql_add (req_json[" username" ].asString (), sha256 (req_json[" password" ].asString ()), req_json[" avatar" ].asInt ());
68
- return " Sign up Success" ;
74
+ *msg = " Sign up Success" ;
75
+ *code = 200 ;
76
+ }
77
+ else
78
+ {
79
+ *msg = " User already exist" ;
80
+ *code = 409 ;
69
81
}
70
- return " User already exist" ;
71
82
}
72
83
73
- std::string loginUser (const Json::Value& req_json)
74
- {
84
+ void loginUser (const Json::Value& req_json, std::string* msg, int * code )
85
+ {
75
86
if (sql_check (req_json[" username" ].asString (), sha256 (req_json[" password" ].asString ())))
76
- return " Login Success" ;
77
- return " Login Failed" ;
87
+ {
88
+ *msg = " Login Success" ;
89
+ *code = 200 ;
90
+ }
91
+ else
92
+ {
93
+ *msg = " Login Failed" ;
94
+ *code = 401 ;
95
+ }
78
96
}
79
97
80
98
void avatar (const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
0 commit comments