IM_Client/api/user/UserOperation.cpp
dongl a67f301e79 现在只能添加直接添加的 待和server修改
已修改
请求包添加了几个函数
add修改
待完善
addfriendswindow 界面
添加好友后好友列表立马回显
2023-06-18 18:42:58 +08:00

187 lines
5.6 KiB
C++

//
// Created by dongl on 23-5-10.
//
#include <regex>
#include "UserOperation.h"
UserOperation::UserOperation(Client *client) {
this->client = client;
}
UserOperation::~UserOperation() {
}
//// packet
std::string UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string &account,
const std::string &password) {
auto request = new Request(type, subType, account, password);
std::string temp = request->packet();
delete request;
return temp;
}
std::string
UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string &account, const std::string &password,
const std::string &data) {
auto request = new Request(type, subType, account, password, data);
std::string temp = request->packet();
delete request;
return temp;
}
std::string UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string& account) {
auto request = new Request(type,subType,account);
std::string temp = request->packet();
delete request;
return temp;
}
std::string UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t source, uint64_t target) {
auto request = new Request(type,subType, target, source);
std::string temp = request->packet();
delete request;
return temp;
}
//// packet end
//// login
std::string UserOperation::login_packet(mp::MP_SUB_TYPE subType, const std::string &account, const std::string &password) {
return packet_base(mp::MP_REQUEST_LOGIN, subType, account, password);
}
//// register
std::string
UserOperation::register_packet(mp::MP_SUB_TYPE subType, const std::string &account, const std::string &password,
const std::string &code) {
return packet_base(mp::MP_REQUEST_REGISTER, subType, account, password, code);
}
std::optional<agreement_response> UserOperation::login(const std::string &account, const std::string &password) {
std::regex pattern_email(R"(\w+@(\w+\.)+\w+)");
std::regex pattern_account("[1-9](\\d{5,11})");
std::regex pattern_phone("1(3\\d|47|5([0-3]|[5-9])|8(0|2|[5-9]))\\d{8}$");
std::string packet;
if (std::regex_match(account, pattern_email)) {
packet = login_packet(LOGIN_EMAIL, account, password);
} else if (std::regex_match(account, pattern_account)) {
packet = login_packet(LOGIN_ACCOUNT, account, password);
} else if (std::regex_match(account, pattern_phone)) {
packet = login_packet(LOGIN_PHONE, account, password);
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
std::optional<agreement_response> UserOperation::register_(const std::string &account, const std::string &password, const std::string& code) {
std::regex pattern_email(R"(\w+@(\w+\.)+\w+)");
std::regex pattern_phone("1(3\\d|47|5([0-3]|[5-9])|8(0|2|[5-9]))\\d{8}$");
std::string packet;
if (std::regex_match(account, pattern_email)) {
packet = register_packet(REGISTER_EMAIL, account, password, code);
} else if (std::regex_match(account, pattern_phone)) {
packet = register_packet(REGISTER_PHONE, account, password, code);
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
std::optional<agreement_response> UserOperation::get_code(const std::string& account) {
std::regex pattern_email(R"(\w+@(\w+\.)+\w+)");
std::regex pattern_phone("1(3\\d|47|5([0-3]|[5-9])|8(0|2|[5-9]))\\d{8}$");
std::string packet;
if (std::regex_match(account, pattern_email)) {
packet = packet_base(mp::MP_TYPE::MP_REQUEST_CODE, mp::MP_SUB_TYPE::MP_CODE_EMAIL, account);
} else if (std::regex_match(account, pattern_phone)) {
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
std::optional<agreement_response> UserOperation::fetch_friends(uint64_t account) {
std::regex pattern_account("[1-9](\\d{5,11})");
std::string packet;
if (std::regex_match(std::to_string(account), pattern_account)) {
packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_GET_FRIENDS, std::to_string(account));
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
// 查询用户
std::optional<agreement_response> UserOperation::fetch_user(uint64_t user, uint64_t account) {
std::regex pattern_account("[1-9](\\d{5,11})");
std::string packet;
if (std::regex_match(std::to_string(user), pattern_account)
&& std::regex_match(std::to_string(account), pattern_account)) {
packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT,
std::to_string(account), "", std::to_string(user));
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
// 添加用户
std::optional<agreement_response> UserOperation::add_user(uint64_t account, uint64_t user) {
std::regex pattern_account("[1-9](\\d{5,11})");
std::string packet;
if (std::regex_match(std::to_string(account), pattern_account) &&
std::regex_match(std::to_string(user), pattern_account) ) {
packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_ADD_FRIENDS_ACCOUNT,
account, user);
} else {
return std::nullopt;
}
client->send_data(packet);
return management::fetch_packet();
}
//// login