0507 19:40 db service 分离 添加好友进度一丢丢
This commit is contained in:
@@ -9,6 +9,7 @@ aux_source_directory(tools DIR_TOOLS)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/libevent)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mp)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/ini)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/rapidjson)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mysql++)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mysql++/mysql)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MDB/imm_mysqldb)
|
||||
|
||||
@@ -50,7 +50,7 @@ void MS::listener() {
|
||||
|
||||
void MS::link(struct evconnlistener *e, int fd, struct sockaddr *addr, int socklen, void *arg) {
|
||||
if (!pool) {
|
||||
perror("en: Prohibit manual static invocation MS::link()\n");
|
||||
perror("en: Prohibit manual static invocation MS::safe_grab()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ agreement_response::agreement_response() : agreement() {}
|
||||
agreement_response::~agreement_response() {}
|
||||
|
||||
|
||||
void agreement_response::set(mp::MP_TYPE type, mp::sri* sri, bufferevent* bev) {
|
||||
void agreement_response::set(mp::sri* sri, bufferevent* bev) {
|
||||
auto resp = std::make_shared<mp::response>(mp::response());
|
||||
|
||||
m_sri = resp->sri();
|
||||
|
||||
@@ -45,7 +45,7 @@ public:
|
||||
~agreement_response() override;
|
||||
|
||||
public:
|
||||
void set (mp::MP_TYPE type, mp::sri* sri, bufferevent* bev);
|
||||
void set (mp::sri* sri, bufferevent* bev);
|
||||
public:
|
||||
std::shared_ptr<mp::mph> m_mph;
|
||||
mp::sri m_sri;
|
||||
|
||||
@@ -63,7 +63,14 @@ void handler::remove_user(const std::shared_ptr<agreement_request>& request) {
|
||||
user_fd.erase(request->m_body.account());
|
||||
}
|
||||
|
||||
auto handler::find_fd(uint64_t account) {
|
||||
bool handler::is_user(uint64_t account) {
|
||||
if( user_fd.find(account) == user_fd.cend() ){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::map<uint64_t, userinfo*>::iterator handler::find_user_fd(uint64_t account) {
|
||||
return user_fd.find(account);
|
||||
}
|
||||
/// end curr mem user curd
|
||||
@@ -76,3 +83,5 @@ handler::ccp2p(const std::shared_ptr<agreement_request> &request, const std::sha
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -22,12 +22,14 @@ public:
|
||||
static void add_user(const std::shared_ptr<agreement_request>& request);
|
||||
static void remove_user(const std::shared_ptr<agreement_request>& request);
|
||||
static void remove_user(bufferevent* bev);
|
||||
static auto find_fd(uint64_t account);
|
||||
static bool is_user(uint64_t account);
|
||||
static std::map<uint64_t, userinfo*>::iterator find_user_fd(uint64_t account);
|
||||
|
||||
static void resp(const std::shared_ptr<agreement_request>& request, const std::shared_ptr<agreement_response>& response);
|
||||
static void send(const std::shared_ptr<agreement_request>& request, const std::shared_ptr<agreement_response>& response);
|
||||
|
||||
static void ccp2p(const std::shared_ptr<agreement_request>& request, const std::shared_ptr<agreement_response>& response);
|
||||
|
||||
protected:
|
||||
static std::map<uint64_t, userinfo*> user_fd;
|
||||
};
|
||||
|
||||
@@ -15,10 +15,15 @@ mapping::mapping() {
|
||||
// ini.LoadFile("/home/dongl/code/网络编程/IMS/config/db.ini");
|
||||
// auto key_value = ini.GetSection("server-mapping");
|
||||
|
||||
auto userController = new UserController();
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGIN, userController));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_REGISTER, userController));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGOUT, userController));
|
||||
// 用户验证证明
|
||||
auto userProve = new UserProveController();
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGIN, userProve));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_REGISTER, userProve));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGOUT, userProve));
|
||||
|
||||
// 用户添加好友群组类操作
|
||||
auto improve = new IMProveController();
|
||||
map.insert({mp::MP_REQUEST_IM_ADD, improve});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ project(works)
|
||||
aux_source_directory(controller DIR_WORKS_CONTROLLER)
|
||||
aux_source_directory(service DIR_WORKS_SERVICE)
|
||||
aux_source_directory(db DIR_WORKS_DB)
|
||||
aux_source_directory(db/po DIR_WORKS_DB_PO)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MS/mmm)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MDB/imm_mysqldb)
|
||||
@@ -11,4 +12,5 @@ add_library(works
|
||||
${DIR_WORKS_CONTROLLER}
|
||||
${DIR_WORKS_SERVICE}
|
||||
${DIR_WORKS_DB}
|
||||
${DIR_WORKS_DB_PO}
|
||||
)
|
||||
10
MS/works/controller/IMProveController.cpp
Normal file
10
MS/works/controller/IMProveController.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 23-5-4.
|
||||
//
|
||||
|
||||
#include "IMProveController.h"
|
||||
|
||||
void IMProveController::run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) {
|
||||
auto sri = service.imProve(&request->m_body);
|
||||
response->set(sri, request->m_bev);
|
||||
}
|
||||
22
MS/works/controller/IMProveController.h
Normal file
22
MS/works/controller/IMProveController.h
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by dongl on 23-5-4.
|
||||
//
|
||||
|
||||
#ifndef IM2_IMPROVECONTROLLER_H
|
||||
#define IM2_IMPROVECONTROLLER_H
|
||||
|
||||
|
||||
#include "../../mmm/handler.h"
|
||||
#include "service/UserService.h"
|
||||
#include "service/UserFriendsService.h"
|
||||
|
||||
class IMProveController : public handler{
|
||||
public:
|
||||
void run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) override;
|
||||
|
||||
protected:
|
||||
UserFriendsService service = UserFriendsService();
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_IMPROVECONTROLLER_H
|
||||
@@ -1,24 +0,0 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#include "UserController.h"
|
||||
|
||||
void UserController::run(std::shared_ptr<agreement_request> agreement_request, std::shared_ptr<agreement_response> agreement_response) {
|
||||
if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_LOGIN) {
|
||||
auto sri = service.login(agreement_request->m_body.account(), agreement_request->m_body.password());
|
||||
agreement_response->set(mp::MP_RESPONSE_LOGIN, sri, agreement_request->m_bev);
|
||||
handler::add_user(agreement_request);
|
||||
|
||||
} else if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_REGISTER) {
|
||||
auto sri = service.register_(agreement_request->m_body.account(), agreement_request->m_body.password());
|
||||
agreement_response->set(mp::MP_RESPONSE_REGISTER, sri, agreement_request->m_bev);
|
||||
|
||||
} else if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_LOGOUT) {
|
||||
auto sri = service.logout(agreement_request->m_body.account());
|
||||
agreement_response->set(mp::MP_RESPONSE_LOGOUT, sri, agreement_request->m_bev);
|
||||
handler::remove_user(agreement_request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
//
|
||||
// Created by dongl on 23-4-25.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERCONTROLLER_H
|
||||
#define IM2_USERCONTROLLER_H
|
||||
|
||||
#include "../../mmm/handler.h"
|
||||
#include "service/UserService.h"
|
||||
|
||||
class UserController : public handler {
|
||||
public:
|
||||
void run(std::shared_ptr<agreement_request> agreement_request, std::shared_ptr<agreement_response> response) override;
|
||||
private:
|
||||
UserService service = UserService();
|
||||
};
|
||||
|
||||
#endif //IM2_USERCONTROLLER_H
|
||||
33
MS/works/controller/UserProveController.cpp
Normal file
33
MS/works/controller/UserProveController.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#include "UserProveController.h"
|
||||
|
||||
void UserProveController::run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) {
|
||||
if (request->m_mph->mp_type() == mp::MP_REQUEST_LOGIN) {
|
||||
auto sri = service.login(request->m_body.account(), request->m_body.password());
|
||||
response->set(sri, request->m_bev);
|
||||
handler::add_user(request);
|
||||
|
||||
} else if (request->m_mph->mp_type() == mp::MP_REQUEST_REGISTER) {
|
||||
auto sri = service.register_(request->m_body.account(), request->m_body.password());
|
||||
response->set(sri, request->m_bev);
|
||||
|
||||
} else if (request->m_mph->mp_type() == mp::MP_REQUEST_LOGOUT) {
|
||||
// 查看当前用户是否在线
|
||||
bool state = handler::is_user(request->m_body.account());
|
||||
|
||||
// current user list used redis 在优化
|
||||
if (state) {
|
||||
handler::remove_user(request);
|
||||
auto sri = service.logout(request->m_body.account(), state);
|
||||
response->set(sri, request->m_bev);
|
||||
} else {
|
||||
auto sri = service.logout(request->m_body.account(), state);
|
||||
response->set(sri, request->m_bev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
MS/works/controller/UserProveController.h
Normal file
19
MS/works/controller/UserProveController.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by dongl on 23-4-25.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERPROVECONTROLLER_H
|
||||
#define IM2_USERPROVECONTROLLER_H
|
||||
|
||||
#include "../../mmm/handler.h"
|
||||
#include "service/UserService.h"
|
||||
|
||||
class UserProveController : public handler {
|
||||
public:
|
||||
void run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) override;
|
||||
|
||||
protected:
|
||||
UserService service = UserService();
|
||||
};
|
||||
|
||||
#endif //IM2_USERPROVECONTROLLER_H
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef IM2_WORKS_H
|
||||
#define IM2_WORKS_H
|
||||
|
||||
#include "UserController.h"
|
||||
#include "UserProveController.h"
|
||||
#include "IMProveController.h"
|
||||
|
||||
#endif //IM2_WORKS_H
|
||||
|
||||
20
MS/works/db/DB.h
Normal file
20
MS/works/db/DB.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by dongl on 23-5-4.
|
||||
//
|
||||
|
||||
#ifndef IM2_DB_H
|
||||
#define IM2_DB_H
|
||||
|
||||
|
||||
#include <mysql++.h>
|
||||
|
||||
class DB {
|
||||
public:
|
||||
DB() {}
|
||||
virtual ~DB() {delete conn;}
|
||||
protected:
|
||||
mysqlpp::Connection* conn = nullptr;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_DB_H
|
||||
@@ -4,91 +4,89 @@
|
||||
|
||||
#include "UserDB.h"
|
||||
#include "template_table/im_user.h"
|
||||
#include "proto/mp.sri.pb.h"
|
||||
|
||||
UserDB::~UserDB() {
|
||||
delete sri;
|
||||
}
|
||||
/// 0505 19:34 im_user 重定义
|
||||
|
||||
// 登陆
|
||||
mp::sri* UserDB::login(uint64_t account, const std::string& password) {
|
||||
// select user info
|
||||
std::tuple<bool, PoUser> UserDB::select_user(uint64_t account) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("select * from im_user where account=%0:account;");
|
||||
query.parse();
|
||||
|
||||
std::vector<im_user> v;
|
||||
query.storein(v, account);
|
||||
std::vector<im_user> user;
|
||||
query.storein(user, account);
|
||||
|
||||
sri = new mp::sri();
|
||||
// 无账户
|
||||
if (v.empty()) {
|
||||
sri->set_sri_msg("无此账户");
|
||||
sri->set_sri_code(mp::MP_LOGIN_ACCOUNT_NOT);
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 判断密码
|
||||
im_user user = v[0];
|
||||
std::string source = std::to_string(account) + password + user.password_salt;
|
||||
size_t password_hash = std::hash<std::string>()(source);
|
||||
// hash_password(&password_hash, source.c_str(), source.size());
|
||||
|
||||
if (user.password == std::to_string(password_hash)) {
|
||||
printf("登陆成功\n");
|
||||
sri->set_sri_msg("登陆成功!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_SUCCESS);
|
||||
sri->set_sri_token("token");
|
||||
sri->set_sri_username(user.username);
|
||||
} else {
|
||||
printf("登陆失败\n");
|
||||
sri->set_sri_msg("登陆失败!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_FAIL);
|
||||
}
|
||||
|
||||
return sri;
|
||||
return {user.empty(), user.empty() ? PoUser() :
|
||||
PoUser(user[0].account, user[0].username, user[0].password, user[0].password_salt, user[0].client_info.c_str())};
|
||||
}
|
||||
|
||||
// 注册
|
||||
mp::sri *UserDB::register_(uint64_t account, const std::string &password, const std::string &password_salt,
|
||||
const std::string& client_info) {
|
||||
// select key account is existed
|
||||
bool UserDB::select_user_exist(uint64_t account) {
|
||||
auto [exist, PoUser] = select_user(account);
|
||||
return exist;
|
||||
}
|
||||
|
||||
// insert user info
|
||||
bool UserDB::insert_user(uint64_t account, const std::string &password, const std::string &password_salt,
|
||||
const std::string &client_info) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query();
|
||||
mysqlpp::String info(client_info);
|
||||
if (!sri) {
|
||||
sri = new mp::sri();
|
||||
}
|
||||
|
||||
// 检验重复账户
|
||||
auto query = conn->query("select account from im_user where account=%0:account;");
|
||||
query.parse();
|
||||
auto is_exist = query.store(account);
|
||||
|
||||
if (!is_exist.empty()) {
|
||||
sri->set_sri_msg("帐号已经注册!");
|
||||
sri->set_sri_code(mp::MP_REGISTER_EXIST);
|
||||
|
||||
printf("帐号已经注册!\n");
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 添加账户
|
||||
query.insert(im_user(account, "用户"+ std::to_string(account), password, password_salt, info));
|
||||
auto ret = query.exec();
|
||||
if (ret) {
|
||||
sri->set_sri_msg("注册成功!");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SUCCESS);
|
||||
printf("注册成功!\n");
|
||||
} else {
|
||||
sri->set_sri_msg("注册失败!");
|
||||
printf("注册失败!\n");
|
||||
}
|
||||
|
||||
return sri;
|
||||
return query.exec();
|
||||
}
|
||||
|
||||
// 注销登陆
|
||||
mp::sri *UserDB::logout(uint64_t account) {
|
||||
return nullptr;
|
||||
// insert user friends info
|
||||
bool UserDB::insert_user_friends(uint64_t account) {
|
||||
// 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
||||
conn = LinkDB::safe_grab();
|
||||
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
||||
"values (%1:account, JSON_OBJECT(%2:key, "
|
||||
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
||||
|
||||
q.template_defaults[1] = account;
|
||||
q.template_defaults[2] = account;
|
||||
q.template_defaults[3] = time(nullptr);
|
||||
q.parse();
|
||||
|
||||
return q.exec();
|
||||
}
|
||||
|
||||
bool UserDB::remove_user(uint64_t account) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("delete from im_user where account=%0:account");
|
||||
query.template_defaults["account"] = account;
|
||||
query.parse();
|
||||
|
||||
return query.exec();
|
||||
}
|
||||
|
||||
|
||||
bool UserDB::test(uint64_t account) {
|
||||
// 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
||||
conn = LinkDB::safe_grab();
|
||||
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
||||
"values (%1:account, JSON_OBJECT(%2:key, "
|
||||
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
||||
|
||||
q.template_defaults[1] = account;
|
||||
q.template_defaults[2] = account;
|
||||
q.template_defaults[3] = time(nullptr);
|
||||
q.parse();
|
||||
|
||||
return q.exec();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,21 +5,22 @@
|
||||
#ifndef IM2_USERDB_H
|
||||
#define IM2_USERDB_H
|
||||
|
||||
|
||||
#include "linkDB.h"
|
||||
#include "Sri.h"
|
||||
#include "DB.h"
|
||||
#include "po/po.h"
|
||||
|
||||
class UserDB {
|
||||
class UserDB : public DB {
|
||||
/// login ...
|
||||
public:
|
||||
virtual ~UserDB();
|
||||
std::tuple<bool, PoUser> select_user(uint64_t account);
|
||||
bool select_user_exist(uint64_t account);
|
||||
bool insert_user(uint64_t account, const std::string &password, const std::string &password_salt,
|
||||
const std::string& client_info = "");
|
||||
bool insert_user_friends(uint64_t account);
|
||||
bool test(uint64_t account);
|
||||
|
||||
public:
|
||||
virtual mp::sri* login(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* register_(uint64_t account, const std::string& password,
|
||||
const std::string& password_salt, const std::string& client_info = "");
|
||||
virtual mp::sri* logout(uint64_t account);
|
||||
private:
|
||||
mysqlpp::Connection* conn = DB::link();
|
||||
mp::sri* sri{};
|
||||
bool remove_user(uint64_t account);
|
||||
};
|
||||
|
||||
|
||||
|
||||
123
MS/works/db/UserFriendsDB.cpp
Normal file
123
MS/works/db/UserFriendsDB.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
//
|
||||
// Created by dongl on 23-5-4.
|
||||
//
|
||||
|
||||
#include "UserFriendsDB.h"
|
||||
#include "linkDB.h"
|
||||
|
||||
|
||||
// 查询好友添加权限类型
|
||||
char UserFriendsDB::select_add_type(uint64_t account) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("select restrictions from im_user_friends where account=%0:account");
|
||||
query.parse();
|
||||
|
||||
// 查看添加授权类型 0 直接添加 1 验证问题 2 账户审核
|
||||
return query.store(account)[0][0][0]; // 因为 account 唯一 所以结果 至多一个
|
||||
}
|
||||
|
||||
// 修改好友列表数据 添加好友
|
||||
/// friends :{
|
||||
/// uid :info { }
|
||||
/// }
|
||||
bool UserFriendsDB::add_friends(uint64_t account, uint64_t friends) {
|
||||
conn = LinkDB::safe_grab();
|
||||
auto query = conn->query("update im_user_friends set friends="
|
||||
"JSON_SET(friends, '$.\"%2:friends\"', "
|
||||
"JSON_OBJECT('belong_grouping', 1, 'add_time', 10000, 'add_source', 1)"
|
||||
")"
|
||||
"where account=%1:account");
|
||||
|
||||
query.template_defaults[1] = account;
|
||||
query.template_defaults[2] = friends;
|
||||
query.parse();
|
||||
return query.exec();
|
||||
}
|
||||
|
||||
std::vector<uint64_t> UserFriendsDB::select_friends_all(uint64_t account) {
|
||||
conn = LinkDB::safe_grab();
|
||||
|
||||
auto q = conn->query("select friends from im_user_friends where account=783556037");
|
||||
|
||||
auto ret = q.store();
|
||||
std::vector<uint64_t> v;
|
||||
return v;
|
||||
}
|
||||
|
||||
rapidjson::Document UserFriendsDB::select_friends_info(uint64_t account, uint64_t friends) {
|
||||
conn = LinkDB::safe_grab();
|
||||
rapidjson::Document document;
|
||||
|
||||
auto q = conn->query("select JSON_EXTRACT(friends, '$.\"%2:friends\"') as friend_info "
|
||||
"from im_user_friends where account=%1:account");
|
||||
q.template_defaults[1] = account;
|
||||
q.template_defaults[2] = friends;
|
||||
q.parse();
|
||||
|
||||
// 查库
|
||||
auto ret = q.store();
|
||||
|
||||
printf("%zu\n", ret.num_rows());
|
||||
|
||||
if (ret.num_rows() < 1) {
|
||||
return document;
|
||||
}
|
||||
|
||||
// 取json字符串
|
||||
std::string friend_info;
|
||||
ret[0][0].to_string(friend_info);
|
||||
|
||||
// 解析json
|
||||
document.Parse(friend_info.c_str());
|
||||
|
||||
printf("%d\n", document["add_time"].GetInt());
|
||||
printf("user[%ld] friend_info-> %ld : %s", account, friends, friend_info.c_str());
|
||||
// printf("%u\n", document.Size());
|
||||
return document;
|
||||
}
|
||||
|
||||
|
||||
//std::tuple<mp::MP_SRI, mp::MP_SRI, std::string> UserFriendsDB::update_add_info(char type, mp::body *body) {
|
||||
// // 如果是0 直接添加
|
||||
// if (type == '0') {
|
||||
// auto temp_query = conn->query("update im_user_friends set friends=%1q:groups where account=%0:account");
|
||||
// temp_query.parse();
|
||||
//
|
||||
// std::vector<uint64_t> friends;
|
||||
// friends.push_back(body->source());
|
||||
// auto ret1 = temp_query.execute(body->target(), vectorSerialization(friends));
|
||||
//
|
||||
// friends.clear();
|
||||
// friends.push_back(body->target());
|
||||
// auto ret2 = temp_query.execute(body->source(), vectorSerialization(friends));
|
||||
//
|
||||
// if (ret1.rows() < 1 || ret2.rows() < 1) {
|
||||
// return std::make_tuple(mp::MP_ADD_FRIENDS_ERR, mp::MP_ADD_FRIENDS_SQL_ERR, "定位之于此 sql可能执行结果出错");
|
||||
// }
|
||||
// return std::make_tuple(mp::MP_ADD_FRIENDS, mp::MP_ADD_FRIENDS_0, "添加成功");
|
||||
// }
|
||||
//
|
||||
// // 1 回答问题
|
||||
// else if (type == '1') {
|
||||
//
|
||||
// return std::make_tuple(mp::MP_ADD_FRIENDS, mp::MP_ADD_FRIENDS, "添加成功");
|
||||
// }
|
||||
// // 2 被添加账户审核
|
||||
// else if (type == '2') {
|
||||
//// auto temp_query = conn->query("select");
|
||||
//
|
||||
// return std::make_tuple(mp::MP_ADD_FRIENDS, mp::MP_ADD_FRIENDS_2, "待同意");
|
||||
// }
|
||||
//
|
||||
// return std::make_tuple(mp::MP_ADD_FRIENDS_ERR, mp::MP_ADD_FRIENDS_NOT_TYPE, "找不到此类型的添加决策");
|
||||
//}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
23
MS/works/db/UserFriendsDB.h
Normal file
23
MS/works/db/UserFriendsDB.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by dongl on 23-5-4.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERFRIENDSDB_H
|
||||
#define IM2_USERFRIENDSDB_H
|
||||
|
||||
|
||||
#include "DB.h"
|
||||
#include "proto/mp.sri.pb.h"
|
||||
#include "proto/mp.body.pb.h"
|
||||
#include "document.h"
|
||||
|
||||
class UserFriendsDB : public DB {
|
||||
public:
|
||||
char select_add_type(uint64_t account);
|
||||
bool add_friends(uint64_t source, uint64_t target);
|
||||
std::vector<uint64_t> select_friends_all(uint64_t account);
|
||||
rapidjson::Document select_friends_info(uint64_t account, uint64_t friends);
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_USERFRIENDSDB_H
|
||||
@@ -3,14 +3,14 @@
|
||||
//
|
||||
#include "linkDB.h"
|
||||
|
||||
DB::DB() {}
|
||||
DB::~DB() {
|
||||
LinkDB::LinkDB() {}
|
||||
LinkDB::~LinkDB() {
|
||||
delete pool;
|
||||
}
|
||||
|
||||
ConnectionPool* DB::pool = nullptr;
|
||||
ConnectionPool* LinkDB::pool = nullptr;
|
||||
|
||||
mysqlpp::Connection* DB::link() {
|
||||
mysqlpp::Connection* LinkDB::safe_grab() {
|
||||
if (pool == nullptr) {
|
||||
pool = new ConnectionPool("124.221.152.192", "ims", "IMS", "WimTFC8N58kznx2k");
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@
|
||||
|
||||
#include "ConnectionPool.h"
|
||||
|
||||
class DB {
|
||||
class LinkDB {
|
||||
public:
|
||||
static mysqlpp::Connection* link();
|
||||
static mysqlpp::Connection* safe_grab();
|
||||
private:
|
||||
DB();
|
||||
LinkDB();
|
||||
|
||||
virtual ~DB();
|
||||
virtual ~LinkDB();
|
||||
private:
|
||||
static ConnectionPool* pool;
|
||||
};
|
||||
|
||||
16
MS/works/db/po/Po.cpp
Normal file
16
MS/works/db/po/Po.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by dongl on 23-5-6.
|
||||
//
|
||||
|
||||
#include "po.h"
|
||||
|
||||
|
||||
PoUser::PoUser() {
|
||||
|
||||
}
|
||||
|
||||
PoUser::PoUser(uint64_t account, const std::string& username, const std::string& password,
|
||||
const std::string& password_salt,const std::string& client_info) :
|
||||
account(account), username(username), password(password), password_salt(password_salt), client_info(client_info) {
|
||||
|
||||
}
|
||||
7
MS/works/db/po/PoJson.cpp
Normal file
7
MS/works/db/po/PoJson.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
//
|
||||
// Created by dongl on 23-5-7.
|
||||
//
|
||||
|
||||
#include "PoJson.h"
|
||||
|
||||
|
||||
71
MS/works/db/po/PoJson.h
Normal file
71
MS/works/db/po/PoJson.h
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Created by dongl on 23-5-7.
|
||||
//
|
||||
|
||||
#ifndef IM2_POJSON_H
|
||||
#define IM2_POJSON_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "rapidjson.h"
|
||||
|
||||
// uint8_t belong_grouping; // 所属分组 默认好友列表
|
||||
// time_t add_time; // 添加时间
|
||||
// uint8_t add_source; // 添加来源
|
||||
|
||||
class PoJsonFriends {
|
||||
public:
|
||||
PoJsonFriends() {};
|
||||
virtual ~PoJsonFriends() {}
|
||||
public:
|
||||
PoJsonFriends* append(uint64_t account, uint8_t belong_grouping, time_t add_time, uint8_t add_source) {
|
||||
temp.insert({"belong_grouping", belong_grouping});
|
||||
temp.insert({"add_time", add_time});
|
||||
temp.insert({"add_source", add_source});
|
||||
|
||||
friends.insert({account, temp});
|
||||
|
||||
temp.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public:
|
||||
std::map<uint64_t, std::map<std::string, uint64_t>> to_map() {
|
||||
return friends;
|
||||
}
|
||||
|
||||
std::string to_string() {
|
||||
|
||||
std::string ret;
|
||||
ret.append("{ \n");
|
||||
|
||||
for (const auto &item: friends) {
|
||||
ret.append(std::to_string(item.first));
|
||||
ret.append(" : { ");
|
||||
|
||||
for (const auto &it: item.second) {
|
||||
ret.append(it.first);
|
||||
ret.push_back(':');
|
||||
ret.append(std::to_string(it.second));
|
||||
ret.append(", ");
|
||||
}
|
||||
ret.pop_back();
|
||||
ret.pop_back();
|
||||
|
||||
ret.append(" }\n");
|
||||
}
|
||||
|
||||
|
||||
ret.append("}");
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
// uid : userinfo
|
||||
std::map<uint64_t, std::map<std::string, uint64_t>> friends;
|
||||
std::map<std::string, uint64_t> temp;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_POJSON_H
|
||||
24
MS/works/db/po/po.h
Normal file
24
MS/works/db/po/po.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by dongl on 23-5-6.
|
||||
//
|
||||
|
||||
#ifndef IM2_PO_H
|
||||
#define IM2_PO_H
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
class PoUser {
|
||||
public:
|
||||
PoUser(uint64_t account, const std::string& username, const std::string& password,
|
||||
const std::string& password_salt,const std::string& client_info);
|
||||
PoUser();
|
||||
public:
|
||||
uint64_t account;
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string password_salt;
|
||||
std::string client_info;
|
||||
};
|
||||
|
||||
#endif //IM2_PO_H
|
||||
29
MS/works/service/Service.h
Normal file
29
MS/works/service/Service.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by dongl on 23-5-5.
|
||||
//
|
||||
|
||||
#ifndef IM2_SERVICE_H
|
||||
#define IM2_SERVICE_H
|
||||
|
||||
#include <mysql++/connection.h>
|
||||
#include <proto/mp.sri.pb.h>
|
||||
|
||||
class Service {
|
||||
public:
|
||||
Service(){
|
||||
sri = new mp::sri();
|
||||
}
|
||||
|
||||
protected:
|
||||
mp::sri* sri;
|
||||
|
||||
protected:
|
||||
virtual void sri_clear(){
|
||||
sri->clear_sri_code();
|
||||
sri->clear_sri_msg();
|
||||
sri->clear_sri_token();
|
||||
sri->clear_sri_username();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //IM2_SERVICE_H
|
||||
60
MS/works/service/UserFriendsService.cpp
Normal file
60
MS/works/service/UserFriendsService.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by dongl on 23-5-5.
|
||||
//
|
||||
|
||||
#include "UserFriendsService.h"
|
||||
|
||||
UserFriendsService::~UserFriendsService() {
|
||||
delete sri;
|
||||
}
|
||||
|
||||
mp::sri *UserFriendsService::imProve(mp::body* body) {
|
||||
if (body->target() == 0 || body->source() == 0) {
|
||||
printf("请求数据有缺\n");
|
||||
sri->set_sri_msg("请求数据有缺");
|
||||
return sri;
|
||||
}
|
||||
|
||||
auto subcommand = body->subcommand();
|
||||
|
||||
// 添加
|
||||
if (subcommand == mp::MP_SUB_TYPE::MP_REQUEST_ADD_CONTACT_PERSON) {
|
||||
add_contact_person(body);
|
||||
}
|
||||
// 拉黑
|
||||
else if (subcommand == mp::MP_SUB_TYPE::MP_REQUEST_BLACK_LIST_CONTACT_PERSON) {
|
||||
|
||||
}
|
||||
// 删除
|
||||
else if (subcommand == mp::MP_SUB_TYPE::MP_REQUEST_REMOVE_CONTACT_PERSON) {
|
||||
|
||||
}
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
mp::sri* UserFriendsService::add_contact_person(mp::body *body) {
|
||||
// 查看添加目标的权限类型
|
||||
char type = imProveDb.select_add_type(body->target());
|
||||
|
||||
// 直接添加
|
||||
if (type == '0') {
|
||||
|
||||
}
|
||||
// 回答问题
|
||||
else if (type == '1') {
|
||||
|
||||
}
|
||||
// 账户审核
|
||||
else if (type == '2') {
|
||||
|
||||
}
|
||||
|
||||
// auto [is_succ, ret_type, msg] = imProveDb.update_add_info(type, body);
|
||||
|
||||
// sri->set_sri_code(ret_type);
|
||||
// sri->set_sri_msg(msg);
|
||||
return sri;
|
||||
}
|
||||
|
||||
|
||||
29
MS/works/service/UserFriendsService.h
Normal file
29
MS/works/service/UserFriendsService.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by dongl on 23-5-5.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERFRIENDSSERVICE_H
|
||||
#define IM2_USERFRIENDSSERVICE_H
|
||||
|
||||
#include "Service.h"
|
||||
#include "proto/mp.body.pb.h"
|
||||
#include "db/UserFriendsDB.h"
|
||||
|
||||
class UserFriendsService : public Service {
|
||||
public:
|
||||
virtual ~UserFriendsService();
|
||||
|
||||
public:
|
||||
// 唯一对开接口
|
||||
mp::sri* imProve(mp::body* body);
|
||||
|
||||
private:
|
||||
mp::sri* add_contact_person(mp::body* body);
|
||||
|
||||
private:
|
||||
mp::sri* sri = nullptr;
|
||||
UserFriendsDB imProveDb = UserFriendsDB();
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_USERFRIENDSSERVICE_H
|
||||
@@ -6,10 +6,53 @@
|
||||
#include <experimental/random>
|
||||
|
||||
mp::sri* UserService::login(uint64_t account, const std::string& password) {
|
||||
return userDb.login(account, password);
|
||||
sri_clear();
|
||||
|
||||
auto exist = userDb.select_user_exist(account);
|
||||
|
||||
// 无账户
|
||||
if (!exist) {
|
||||
sri->set_sri_msg("无此账户");
|
||||
sri->set_sri_code(mp::MP_LOGIN_ACCOUNT_NOT);
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 判断密码
|
||||
auto [sql_exist, user] = userDb.select_user(account);
|
||||
std::string source = std::to_string(account) + password + user.password_salt;
|
||||
size_t password_hash = std::hash<std::string>()(source);
|
||||
|
||||
if (user.password == std::to_string(password_hash)) {
|
||||
printf("登陆成功\n");
|
||||
sri->set_sri_msg("登陆成功!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_SUCCESS);
|
||||
sri->set_sri_token("token");
|
||||
sri->set_sri_username(user.username);
|
||||
|
||||
// 这里redis 更新帐号信息
|
||||
|
||||
|
||||
} else {
|
||||
printf("登陆失败\n");
|
||||
sri->set_sri_msg("登陆失败!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_FAIL);
|
||||
}
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
|
||||
mp::sri* UserService::register_(uint64_t account, const std::string &password) {
|
||||
sri_clear();
|
||||
|
||||
// 查看账户是否存在
|
||||
auto exist = userDb.select_user_exist(account);
|
||||
if (!exist) {
|
||||
sri->set_sri_msg("账户已经存在");
|
||||
sri->set_sri_code(mp::MP_REGISTER_EXIST);
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 生成salt
|
||||
std::string password_salt;
|
||||
for (int i = 0; i < std::experimental::randint(5, 8); ++i) {
|
||||
@@ -20,9 +63,55 @@ mp::sri* UserService::register_(uint64_t account, const std::string &password)
|
||||
std::string source = std::to_string(account) + password + password_salt;
|
||||
size_t password_hash = std::hash<std::string>()(source);
|
||||
|
||||
return userDb.register_(account, std::to_string(password_hash), password_salt);
|
||||
// 注册 插入用户
|
||||
auto add_user = userDb.insert_user(account, std::to_string(password_hash), password_salt);
|
||||
if (!add_user) {
|
||||
sri->set_sri_msg("注册失败, 用户表, sql");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SQL_ERR);
|
||||
return sri;
|
||||
}
|
||||
|
||||
auto add_friends_table = userDb.insert_user_friends(account);
|
||||
if (!add_friends_table) {
|
||||
if (!userDb.remove_user(account)) {
|
||||
sri->set_sri_msg("注册失败, 附属好友表生成失败,但用户已添加, sql");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SQL_ERR);
|
||||
}
|
||||
|
||||
sri->set_sri_msg("注册失败, 附属好友表, sql");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SQL_ERR);
|
||||
return sri;
|
||||
}
|
||||
|
||||
sri->set_sri_msg("注册成功");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SUCCESS);
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
mp::sri* UserService::logout(uint64_t account) {
|
||||
mp::sri *UserService::logout(uint64_t account, bool state) {
|
||||
// return userDb.logout(account, state);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,14 +4,18 @@
|
||||
|
||||
#ifndef IM2_USERSERVICE_H
|
||||
#define IM2_USERSERVICE_H
|
||||
|
||||
#include "proto/mp.body.pb.h"
|
||||
#include "Service.h"
|
||||
#include "db/UserDB.h"
|
||||
|
||||
class UserService {
|
||||
class UserService : public Service {
|
||||
|
||||
public:
|
||||
virtual mp::sri* login(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* register_(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* logout(uint64_t account);
|
||||
mp::sri* login(uint64_t account, const std::string& password);
|
||||
mp::sri* register_(uint64_t account, const std::string& password);
|
||||
mp::sri* logout(uint64_t account, bool state);
|
||||
|
||||
private:
|
||||
UserDB userDb = UserDB();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user