Skip to content

Commit bf24050

Browse files
Merge pull request #34 from lglglglgy/main
🎨 res : change string to json
2 parents f184e3b + 7746869 commit bf24050

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

msg_controller.cc

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@ void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
3030
sql_addhistory(sender, receiver, content, "0");
3131

3232
std::string msg = req_json["content"].asString();
33+
res_json["code"] = 200;
34+
res_json["massage"] = "Massage Send Success";
3335
auto output = writer.write(res_json);
3436
res->setBody(output);
3537
}
3638
else
3739
{
38-
res->setBody("No Authorization");
40+
res_json["massage"] = "No Authorization";
41+
res_json["code"] = 401;
3942
}
43+
auto output = writer.write(res_json);
44+
res->setBody(output);
4045
callback(res);
4146
}
42-
// get message history
47+
// get message history or new message
4348
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
4449
{
4550
auto check_type = req->getParameter("type");
@@ -52,19 +57,27 @@ void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &
5257
if (jwtVerify(req))
5358
{
5459
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
55-
auto output = writer.write(sql_find_my_msg(me,check_type));
56-
res->setBody(output);
60+
res_json["massage"] = sql_find_my_msg(me,check_type);
61+
res_json["code"] = 200;
62+
5763
}
5864
else
5965
{
60-
res->setBody("No Authorization");
66+
//res->setBody("No Authorization");
67+
res_json["code"] = 401;
68+
6169
}
70+
auto output = writer.write(res_json);
71+
res->setBody(output);
72+
6273
callback(res);
6374
}
6475
// request new friend or cancel request
6576
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
6677
{
6778
auto res = HttpResponse::newHttpResponse();
79+
Json::Value res_json;
80+
Json::FastWriter writer;
6881
res->addHeader("Access-Control-Allow-Origin", "*");
6982
if (jwtVerify(req))
7083
{
@@ -77,22 +90,34 @@ void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpRe
7790
if (sql_findexist(receiver))
7891
{
7992
sql_addrequest(sender, receiver);
80-
res->setBody("Success");
93+
res_json["code"] = 200;
94+
res_json["massage"] = "Friend Add Success";
95+
//res->setBody("Success");
8196
}
8297
else
83-
res->setBody("No this body");
98+
{
99+
res_json["code"] = 404;
100+
res_json["massage"] = "No this user";
101+
//res->setBody("No this body");
102+
}
103+
//res->setBody("No this body");
84104
}
85105
else
86106
{
87107
sql_delete_operation(sender, receiver);
88-
89-
res->setBody("Success");
108+
res_json["code"] = 200;
109+
res_json["massage"] = " Delete Operation Success";
110+
//res->setBody("Success");
90111
}
91112
}
92113
else
93114
{
94-
res->setBody("No Authorization");
115+
res_json["code"] = 401;
116+
res_json["massage"] = "No Authorization ";
117+
//res->setBody("No Authorization");
95118
}
119+
auto output = writer.write(res_json);
120+
res->setBody(output);
96121
callback(res);
97122
}
98123
// handle new friend request

0 commit comments

Comments
 (0)