// // Created by dongl on 23-5-10. // #include #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; } //// 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 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 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 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 UserOperation::FetchFriends(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(); } //// login