// // Created by dongl on 23-4-23. // #include "UserService.h" #include ///************************************************* 登陆 ***********************************************************/// // 登陆 按subtype 分类是帐号登陆还是手机登陆 mp::sri* UserService::login(mp::MP_SUB_TYPE subType, const std::string& account, const std::string& password) { sri_clear(); if (subType == mp::MP_REQUEST_LOGIN_EMAIL) { login_fun(account, password, "email"); } else if (subType == mp::MP_REQUEST_LOGIN_PHONE) { login_fun(account, password, "phone"); } else if (subType == mp::MP_REQUEST_LOGIN_ACCOUNT) { login_fun(account, password, "account"); } return sri; } // 以account登陆 void UserService::login_fun(const std::string& account, const std::string& password, const std::string& filed) { // 判断密码 auto [exist, user] = userDb.select_user(strtol(account.c_str(), nullptr, 0), filed); // 无账户 if (!exist) { sri->set_sri_msg("无此账户"); sri->set_sri_code(mp::MP_LOGIN_ACCOUNT_NOT); return; } std::string source = account + password + user.password_salt; size_t password_hash = std::hash()(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); sri->set_sri_email(user.email); sri->set_sri_phone(user.phone); sri->set_sri_account(user.account); // 这里redis 更新帐号信息 } else { printf("登陆失败\n"); sri->set_sri_msg("登陆失败!"); sri->set_sri_code(mp::MP_LOGIN_FAIL); } } ///********************************************** 注册 *****************************************************//// // 注册 邮箱注册 mp::sri *UserService::register_(mp::MP_SUB_TYPE subType, const std::string &phone_email, const std::string &password) { sri_clear(); // 号池取号 auto account = userDb.fetch_account(); if (account.has_value()) { // 注册 bool state = register_(account.value(), password); // 注册成功 if (state) { bool state1; /// 判断什么方式注册 // 邮箱方式 if (subType == mp::MP_REQUEST_REGISTER_EMAIL) { // 将邮箱绑定到帐号上 state1 = userDb.bind_email(account.value(), phone_email); } // 手机号方式 else { // 将手机绑定到帐号上 state1 = userDb.bind_phone(account.value(), phone_email); } if (!state1) { sri->set_sri_msg("sql:生成的帐号为绑定上本邮箱, 请重新注册"); sri->set_sri_code(mp::MP_REGISTER_SQL_ERR); } else { // 注册成功就将取出的号在号池内删除 userDb.remove_pool_account(account.value()); } } } // 号池无号 else { sri->set_sri_msg("暂无帐号可提供"); sri->set_sri_code(mp::MP_REGISTER_SQL_ERR); } return sri; } // 注册 基函数 bool UserService::register_(uint64_t account, const std::string &password) { // 查看账户是否存在 auto exist = userDb.select_user_exist(account); if (!exist) { sri->set_sri_msg("账户已经存在"); sri->set_sri_code(mp::MP_REGISTER_EXIST); return false; } // 生成salt std::string password_salt; for (int i = 0; i < std::experimental::randint(5, 8); ++i) { password_salt.push_back((char )std::experimental::randint(33, 126)); } // 加密密码 生成hash std::string source = std::to_string(account) + password + password_salt; size_t password_hash = std::hash()(source); // 注册 插入用户 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 false; } 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 false; } sri->set_sri_msg("注册成功"); sri->set_sri_code(mp::MP_REGISTER_SUCCESS); return true; } mp::sri *UserService::logout(const std::string& account, bool state) { // return userDb.logout(account, state); return nullptr; }