#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>
#include "tools.hpp"
#include "database.hpp"
#include "session.hpp"
#include "httplib.h"
#define REGUSER "insert into reg_userinfo (name, password, email) values(\"%s\", \"%s\", \"%s\")"
#define USERINFO "insert into stu_info (stu_name, stu_school, stu_major, stu_grade, stu_mobile) values(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\")"
#define UPDATE_INTERVIEW_TIME "update stu_info set stu_choice_score=%d, stu_program_score=%d, stu_total_score=%d, stu_interview_time=\"%s\" where stu_id=%d"
#define START_TRANSACTION "start transaction"
#define COMMIT "commit"
class AisSvr
{
public:
AisSvr()
{
svr_ip_.clear();
svr_port_ = -1;
db_ = NULL;
db_ip_.clear();
db_user_.clear();
db_passwd_.clear();
db_name_.clear();
db_port_ = -1;
all_session_ = NULL;
}
int OnInit(const std::string& config_filename)
{
//原因是:构造函数没有返回值
//1.加载配置文件
if(!Load(config_filename))
{
LOG(ERROR, "open config file failed") << std::endl;
return -1;
}
LOG(INFO, "open config file success") << std::endl;
//2.初始化数据库模块
db_ = new DataBaseSvr(db_ip_, db_user_, db_passwd_, db_name_, db_port_);
if(!db_)
{
LOG(ERROR, "create database failed") << std::endl;
return -2;
}
if(!db_->Connect2Mysql())
{
LOG(ERROR, "connect database failed") << std::endl;
return -3;
}
LOG(INFO, "connect database success") << std::endl;
all_session_ = new AllSessionInfo();
if(!all_session_)
{
LOG(ERROR, "create all session info failed") << std::endl;
return -4;
}
return 0;
}
void Start()
{
//注册请求, 用户名字,密码, 邮箱
http_svr_.Post("/register", [this](const httplib::Request& res, httplib::Response& resp){
std::unordered_map<std::string, std::string> parm;
UrlUtil::PraseBody(res.body, &parm);
//1.插入数据库当中
//1.1 针对注册的信息, 先插入注册信息表
std::string name = parm["name"];
std::string password = parm["password"];
std::string email = parm["email"];
std::string school = parm["school"];
std::string major = parm["major"];
std::string class_no = parm["class_no"];
std::string phone_num = parm["phone_num"];
//1.2 组织插入语句
Json::Value response_json;
Json::FastWriter writer;
//开启事务
db_->QuerySql(START_TRANSACTION);
//创建保存点
db_->QuerySql("savepoint aa");
char buf[1024] = {0};
snprintf(buf, sizeof(buf) - 1, REGUSER, name.c_str(),
password.c_str(), email.c_str());
//printf("buf:%s\n", buf);
bool ret = db_->QuerySql(buf);
if(!ret)
{
//1.第一个就插入失败了
// 1.1 结束事务
db_->QuerySql(COMMIT);
// 1.2 返回应答
response_json["is_insert"] = false;
resp.body = writer.write(response_json);
resp.set_header("Content-Type", "application/json");
// 1.3 结束这个函数
return;
}
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf) - 1, USERINFO, name.c_str(), school.c_str(), major.c_str(), class_no.c_str(), phone_num.c_str());
//printf("buf2 : %s\n", buf);
ret = db_->QuerySql(buf);
if(!ret)
{
//1.回滚
db_->QuerySql("rollback to aa");
//2.提交事务
db_->QuerySql(COMMIT);
//3.组织应答
response_json["is_insert"] = false;
resp.body = writer.write(response_json);
resp.set_header("Content-Type", "application/json");
//4.return
return;
}
//提交事务
db_->QuerySql(COMMIT);
//2.给浏览器响应一个应答, 需要是json格式
response_json["is_insert"] = true;
resp.body = writer.write(response_json);
resp.set_header("Content-Type", "application/json");
});
http_svr_.Post("/login", [this](const httplib::Request& res, httplib::Response& resp){
//1.解析提交的内容
std::unordered_map<std::string, std::string> parm;
UrlUtil::PraseBody(res.body, &parm);
Json::Value request_json;
request_json["email"] = parm["email"];
request_json["password"] = parm["password"];
//2.校验用户的邮箱和密码
// 2.1 如果校验失败, 则给浏览器返回false
// 2.2 如果校验成功,执行下面的第3步
//具体的操作步骤,需要在注册表当中进行查询,用提交上来的邮箱作为查询的依据
// 如果邮箱不存在,则登录失败
// 如果邮箱存在
// 密码正确,则登录成功
// 密码正确,则登录失败
Json::Value response_json;
bool ret = db_->QueryUserExist(request_json, &response_json);
if(!ret)
{
response_json["login_status"] = false;
}
else
{
std::cout<<"is login"<<std::endl;
response_json["login_status"] = true;
}
//3.前提是在登录正常的情况下
//返回sessionid, 用户标识当前用户
//3.1 获取指定用户的信息
Json::Value user_info;
db_->QueryOneStuInfo(response_json["stu_id"].asString(), &user_info);
//3.2 生成sessionid
Session sess(user_info);
std::string sessionid = sess.GetSessinId();
all_session_->SetSessionValue(sessionid, sess);
std::string tmp = "JSESSIONID=";
tmp += sessionid;
Json::FastWriter writer;
resp.body = writer.write(response_json);
//将session放到cookie当中返回给浏览器
resp.set_header("Set-Cookie", tmp.c_str());
resp.set_header("Content-Type", "application/json");
});
http_svr_.Get("/interview", [this](const httplib::Request& res, httplib::Response& resp){
//1.要根据请求头部当中的sessinid, 从all_session_,查询到对于的用户信息
std::string session_id;
没有合适的资源?快使用搜索试试~ 我知道了~
C++,线程互斥,ajax+jquery实现客户与服务器连接的面试预约.zip
共28个文件
html:5个
css:4个
hpp:3个
需积分: 2 0 下载量 160 浏览量
2024-01-20
15:33:47
上传
评论
收藏 905KB ZIP 举报
温馨提示
C++面试题库,大厂进阶之路; 一份涵盖大部分 C++ c程序员所需要掌握的核心知识;涉及C++面试常问题、设计模式、数据结构、操作系统及网络等
资源推荐
资源详情
资源评论
收起资源包目录
C++,线程互斥,ajax+jquery实现客户与服务器连接的面试预约.zip (28个子文件)
open_weixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxxxxxxxxxxxxcxvcvcv
makefile 201B
httplib.h 167KB
ais_system.cpp 15KB
session.hpp 3KB
config_ais.cfg 124B
ais_svr 1.17MB
Interview_appointment
www
interview.html 6KB
index.jsp 379B
js
jquery-2.1.0.min.js 82KB
post_interview.html 5KB
css
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap.css 143KB
bootstrap-theme.min.css 23KB
update.html 5KB
index.html 3KB
add.html 5KB
images
6.jpg 321KB
21.jpg 75KB
5.jpg 66KB
2.png 49KB
fonts
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.woff2 18KB
tools.hpp 4KB
database.hpp 7KB
共 28 条
- 1
资源评论
极致人生-010
- 粉丝: 2964
- 资源: 2825
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功