#include "AddFriendDialog.h" #include "TitleBar.h" #include "UiHelpers.h" #include "ImClient.h" #include "Protocol.h" #include "AvatarCache.h" #include "common/protocol_def.h" #include #include #include #include #include #include #include AddFriendDialog::AddFriendDialog(QWidget *parent) : QDialog(parent) { setObjectName(QStringLiteral("ChromeWindow")); setWindowTitle(QStringLiteral("添加好友")); setFixedSize(400, 460); UiHelpers::setupFramelessDialog(this); auto *root = new QVBoxLayout(this); root->setContentsMargins(0, 0, 0, 0); root->setSpacing(0); auto *bar = new TitleBar(this); bar->setTitle(QStringLiteral("添加好友")); bar->setTheme(TitleBar::Light); bar->setMaximizable(false); bar->bind(this, false); root->addWidget(bar); auto *page = new QWidget(this); auto *lay = new QVBoxLayout(page); lay->setContentsMargins(24, 18, 24, 22); lay->setSpacing(10); auto *row = new QHBoxLayout(); row->setSpacing(8); m_keyword = UiHelpers::lineEdit(QStringLiteral("输入账号搜索"), page); auto *search = UiHelpers::primaryButton(QStringLiteral("搜索"), page); search->setFixedWidth(72); search->setMinimumWidth(72); row->addWidget(m_keyword, 1); row->addWidget(search); m_error = UiHelpers::errorLabel(page); m_result = new QWidget(page); m_result->setObjectName(QStringLiteral("FriendResult")); m_result->setStyleSheet(QStringLiteral( "QWidget#FriendResult { background:#F7F7F7; border:none; }" "QLabel { background:transparent; border:none; }")); auto *rLay = new QVBoxLayout(m_result); rLay->setContentsMargins(12, 12, 12, 12); rLay->setSpacing(8); auto *top = new QHBoxLayout(); top->setSpacing(10); m_avatar = new QLabel(m_result); m_avatar->setFixedSize(44, 44); m_avatar->setStyleSheet(QStringLiteral("background:transparent; border:none;")); auto *info = new QVBoxLayout(); info->setSpacing(2); info->setContentsMargins(0, 0, 0, 0); m_name = new QLabel(m_result); m_name->setStyleSheet(QStringLiteral( "QLabel { font-size:14px; color:#1A1A1A; background:transparent; border:none; }")); m_account = new QLabel(m_result); m_account->setStyleSheet(QStringLiteral( "QLabel { font-size:12px; color:#9A9A9A; background:transparent; border:none; }")); info->addWidget(m_name); info->addWidget(m_account); m_addBtn = UiHelpers::primaryButton(QStringLiteral("添加"), m_result); m_addBtn->setFixedSize(72, 32); top->addWidget(m_avatar, 0, Qt::AlignTop); top->addLayout(info, 1); top->addWidget(m_addBtn, 0, Qt::AlignTop); m_status = new QLabel(m_result); m_status->setWordWrap(true); m_status->setStyleSheet(QStringLiteral( "QLabel { color:#FA5151; font-size:12px; background:transparent; border:none; }")); m_status->hide(); m_verify = UiHelpers::lineEdit(QStringLiteral("验证信息(选填)"), m_result); m_verify->setMaxLength(50); m_verify->hide(); rLay->addLayout(top); rLay->addWidget(m_status); rLay->addWidget(m_verify); m_result->hide(); lay->addLayout(row); lay->addWidget(m_error); lay->addWidget(m_result); lay->addStretch(); root->addWidget(page, 1); connect(search, &QPushButton::clicked, this, &AddFriendDialog::onSearch); connect(m_keyword, &QLineEdit::returnPressed, this, &AddFriendDialog::onSearch); connect(m_addBtn, &QPushButton::clicked, this, &AddFriendDialog::onAdd); m_keyword->setFocus(); } void AddFriendDialog::clearResult() { m_uid = 0; m_userAccount.clear(); m_result->hide(); m_status->hide(); m_status->clear(); m_verify->hide(); m_verify->clear(); m_addBtn->show(); } void AddFriendDialog::showResult(const QJsonObject &user) { m_uid = Proto::jsonI64(user, QStringLiteral("uid")); m_userAccount = user.value(QStringLiteral("account")).toString(); QString name = user.value(QStringLiteral("nickname")).toString(); if (name.isEmpty()) name = m_userAccount; m_name->setText(name); m_account->setText(QStringLiteral("账号 %1").arg(m_userAccount)); const int ver = static_cast(Proto::jsonI64(user, QStringLiteral("avatar_ver"))); AvatarCache::instance().applyTo(m_avatar, m_uid, ver, name, 44); const int rel = user.value(QStringLiteral("relation")).toInt(); const int allowAdd = user.value(QStringLiteral("allow_add")).toInt(1); QString status; bool canAdd = false; if (m_uid == ImClient::instance().uid()) status = QStringLiteral("这是你自己的账号"); else if (rel == im::FriendRel::Friend) status = QStringLiteral("已是好友"); else if (rel == im::FriendRel::Pending) status = QStringLiteral("已发送申请,等待对方处理"); else if (rel == im::FriendRel::Incoming) status = QStringLiteral("对方已申请添加你,请到「新的朋友」处理"); else if (allowAdd == 0) status = QStringLiteral("对方不允许添加好友"); else canAdd = true; m_addBtn->setVisible(canAdd); m_addBtn->setEnabled(canAdd); m_addBtn->setText(QStringLiteral("添加")); m_verify->setVisible(canAdd); if (canAdd) { m_status->hide(); m_status->clear(); m_verify->setFocus(); } else { m_status->setText(status); m_status->show(); } m_result->show(); } void AddFriendDialog::onSearch() { const QString key = m_keyword->text().trimmed(); UiHelpers::setError(m_error, QString()); clearResult(); if (key.isEmpty()) { UiHelpers::setError(m_error, QStringLiteral("请输入账号或手机号")); return; } QPointer self(this); ImClient::instance().searchUser(key, [self](const QJsonObject &reply) { if (!self) return; if (!ImClient::ok(reply)) { UiHelpers::setError(self->m_error, ImClient::messageOf(reply)); return; } self->showResult(reply); }); } void AddFriendDialog::onAdd() { if (m_uid <= 0) return; m_addBtn->setEnabled(false); const QString message = m_verify->text().trimmed(); QPointer self(this); ImClient::instance().addFriend(m_uid, message, [self](const QJsonObject &reply) { if (!self) return; if (!ImClient::ok(reply)) { self->m_addBtn->setEnabled(true); UiHelpers::setError(self->m_error, ImClient::messageOf(reply)); return; } self->m_addBtn->hide(); self->m_verify->hide(); self->m_status->setText(QStringLiteral("已发送好友申请,等待对方同意")); self->m_status->show(); UiHelpers::showToast(self, QStringLiteral("已发送好友申请,等待对方同意")); }); }