forked from lxyyxl638/cgi_web
/
get_friends_by_team.cpp
105 lines (95 loc) · 2.86 KB
/
get_friends_by_team.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fcgiapp.h>
#include <string>
#include <cstring>
#include <unordered_map>
#include <fcgi_stdio.h>
#include "include/database.h"
#include "json/json.h"
#include "include/session.h"
#include "include/public.h"
int main() {
Database *db = Database::getInstance();
Session *session = Session::getInstance();
while (FCGI_Accept() >= 0) {
Json::FastWriter fw;
Json::Value root;
string result("fail");
string detail("");
session->sessionInit();
vector<unordered_map<string,string> > query_result;
if(session->checkSession() == false){
detail = detail + "unlogin";
}else{
unordered_map<string,string> ans;
char * method = getenv("REQUEST_METHOD");
if ( strcmp(method,"POST") == 0) {
char *contentLength = getenv("CONTENT_LENGTH");
int len;
if (contentLength != NULL) {
len = strtol(contentLength, NULL, 10);
} else {
len = 0;
}
int i, ch;
string post_val="";
for (i = 0; i < len; i++) {
if ((ch = getchar()) < 0) {
break;
}
post_val = post_val + (char) ch ;
}
ParseParam(post_val,ans);
} else if(strcmp(method,"GET")==0){
char* str = getenv("QUERY_STRING");
string Param(str);
ParseParam(Param,ans);
}
int argu_count = 0;
if(ans.find("team_id") != ans.end())
argu_count++;
if(argu_count < 1) {
detail = "参数错误!";
}
else
{
char query_buf[1024] = {0};
string user_id,team_id;
unordered_map<string,string>::iterator it;
it = ans.find("team_id");
team_id = it->second;
user_id = session->getValue("user_id");
snprintf(query_buf,sizeof(query_buf),"select friend_id,username,nickname,sex from friendship inner join users on friend_id = users.user_id where friendship.user_id = %d and team_id = %d",atoi(user_id.c_str()),atoi(team_id.c_str()));
string query(query_buf);
int flag = db->dbQuery(query,query_result);
if(flag ){
result = "success" ;
}else{
detail = "用户名密码错误!";
}
}
}
root["result"] = Json::Value(result);
if(strcmp(result.c_str(),"success") == 0){
Json::Value friend_list;
for(size_t i = 0 ; i != query_result.size(); i++ ){
Json::Value friend_info;
friend_info["friend_id"] = Json::Value(query_result[i]["friend_id"]);
friend_info["friend_name"] = Json::Value(query_result[i]["username"]);
friend_info["friend_nickname"] = Json::Value(query_result[i]["nickname"]);
friend_info["friend_sex"] = Json::Value(query_result[i]["sex"]);
friend_info["online"] = Json::Value(session->getOnline(atoi(query_result[i]["friend_id"].c_str()))?"1":"0");
root["friend_list"].append(friend_info);
}
}else{
root["detail"] = Json::Value(detail);
}
string str = fw.write(root);
FCGI_printf("Content-type: application/json\r\n"
"\r\n"
"%s",str.c_str());
}
return 0;
}