Files
IM/MC/api/user/UserOperation.cpp

59 lines
1.5 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;
}
//// 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);
}
std::optional<std::string> 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 packet;
}
//// login