mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-13 16:41:48 +08:00
add updateFriendList from network
This commit is contained in:
@ -25,6 +25,7 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent)
|
||||
avatarBtn = new QPushButton();
|
||||
avatarBtn->setFixedSize(75, 75);
|
||||
avatarBtn->setIconSize(QSize(75, 75));
|
||||
//avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
|
||||
avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
|
||||
avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
|
||||
|
||||
|
||||
2
debug.h
2
debug.h
@ -1,7 +1,7 @@
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
#define TEST_UI 1
|
||||
#define TEST_UI 0
|
||||
|
||||
#define TEST_SKIP_LOGIN 1
|
||||
|
||||
|
||||
@ -87,7 +87,8 @@ void MainWidget::initLeftWindow()
|
||||
userAvatar = new QPushButton();
|
||||
userAvatar->setFixedSize(45, 45);
|
||||
userAvatar->setIconSize(QSize(45, 45));
|
||||
userAvatar->setIcon(QIcon(":/resource/image/defaultAv.png"));
|
||||
//由于要从网络获取头像信息,所以这里就不能再设置默认的头像了,;避免头像意外变化
|
||||
//userAvatar->setIcon(QIcon(":/resource/image/defaultAv.png"));
|
||||
userAvatar->setStyleSheet("QPushButton {background-color: transparent; }");
|
||||
layout->addWidget(userAvatar, 1, Qt::AlignTop | Qt::AlignCenter);
|
||||
|
||||
@ -138,7 +139,7 @@ void MainWidget::initMidWindow()
|
||||
QPushButton::pressed { background-color: rgb(240, 240, 240); })";
|
||||
addFriendBtn->setStyleSheet(style);
|
||||
|
||||
SessionFriendArea* sessionFriendArea = new SessionFriendArea();
|
||||
sessionFriendArea = new SessionFriendArea();
|
||||
|
||||
//为了更加灵活的控制边距,只影响搜索框和按钮的这一行,
|
||||
//创建空白的widget填充到布局管理器上
|
||||
@ -292,6 +293,11 @@ void MainWidget::initSignalSlot()
|
||||
userAvatar->setIcon(myself->avatar);
|
||||
});
|
||||
dataCenter->getMyselfAsync();
|
||||
|
||||
/////////////////////////////////////
|
||||
// 获取好友列表
|
||||
/////////////////////////////////////
|
||||
loadFriendList();
|
||||
}
|
||||
|
||||
void MainWidget::switchTabToSession()
|
||||
@ -339,7 +345,22 @@ void MainWidget::loadSessionList()
|
||||
//加载好友列表
|
||||
void MainWidget::loadFriendList()
|
||||
{
|
||||
//TODO
|
||||
//好友列表数据是在DataCenter中存储的
|
||||
//首先需要判定DataCenter中是否已经有数据了,如果有,则直接加载本地数据
|
||||
//如果没有,则从服务器读取
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
if (dataCenter->getFriendList() != nullptr) {
|
||||
//从内存中加载数据
|
||||
LOG() << "本地已存在好友列表数据,更新加载本地列表";
|
||||
updateFriendList();
|
||||
}
|
||||
else {
|
||||
//通过网络加载数据 通过这个参数确保信号槽不会别重复绑定
|
||||
LOG() << "本地好友列表数据不存在,正在从网络获取...";
|
||||
connect(dataCenter, &DataCenter::getFriendListDone, this, &MainWidget::updateFriendList, Qt::UniqueConnection);
|
||||
dataCenter->getFriendListAsync();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//加载申请列表
|
||||
@ -348,4 +369,24 @@ void MainWidget::loadApplyList()
|
||||
//TODO
|
||||
}
|
||||
|
||||
void MainWidget::updateFriendList()
|
||||
{
|
||||
if (activeTab != FRIEND_LIST) {
|
||||
//当前的标签页不是好友列表,就不渲染任何数据到界面上
|
||||
return;
|
||||
}
|
||||
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
QList<UserInfo>* friendList = dataCenter->getFriendList();
|
||||
|
||||
//清空一下之前的界面的数据
|
||||
sessionFriendArea->clear();
|
||||
|
||||
//遍历好友列表,添加到界面上
|
||||
for (const auto& f : *friendList) {
|
||||
sessionFriendArea->addItem(FriendItemType, f.userId, f.avatar, f.nickname, f.description);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -59,6 +59,9 @@ private:
|
||||
//添加好友按钮
|
||||
QPushButton* addFriendBtn;
|
||||
|
||||
//
|
||||
SessionFriendArea* sessionFriendArea;
|
||||
|
||||
//显示会话详情按钮
|
||||
QPushButton* extraBtn;
|
||||
|
||||
@ -90,5 +93,7 @@ private:
|
||||
void loadSessionList();
|
||||
void loadFriendList();
|
||||
void loadApplyList();
|
||||
|
||||
void updateFriendList();
|
||||
};
|
||||
#endif // MAINWIDGET_H
|
||||
|
||||
@ -163,4 +163,30 @@ namespace model
|
||||
myself->load(userInfo);
|
||||
}
|
||||
|
||||
QList<UserInfo>* DataCenter::getFriendList()
|
||||
{
|
||||
return friendList;
|
||||
}
|
||||
|
||||
void DataCenter::getFriendListAsync()
|
||||
{
|
||||
netClient.getFriendList(loginSessionId);
|
||||
}
|
||||
|
||||
void DataCenter::resetFriendList(std::shared_ptr<bite_im::GetFriendListRsp> resp)
|
||||
{
|
||||
if (friendList == nullptr) {
|
||||
friendList = new QList<UserInfo>();
|
||||
}
|
||||
friendList->clear();
|
||||
|
||||
QList<bite_im::UserInfo> friendListPB = resp->friendList();
|
||||
for (auto& f : friendListPB) {
|
||||
UserInfo userInfo;
|
||||
userInfo.load(f);
|
||||
friendList->push_back(userInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} //end namespace model
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <qstandardpaths.h>
|
||||
#include <QDir>
|
||||
#include <QJsonObject>
|
||||
//#include <QList>
|
||||
|
||||
#include "data.h"
|
||||
#include "../network/netclient.h"
|
||||
@ -101,8 +102,14 @@ namespace model
|
||||
//
|
||||
void resetMyself(std::shared_ptr<bite_im::GetUserInfoRsp> resp);
|
||||
|
||||
//通过网络获取好友列表
|
||||
QList<UserInfo>* getFriendList();
|
||||
void getFriendListAsync();
|
||||
void resetFriendList(std::shared_ptr<bite_im::GetFriendListRsp> resp);
|
||||
|
||||
signals:
|
||||
//自定义信号
|
||||
void getMyselfDone();
|
||||
void getFriendListDone();
|
||||
};
|
||||
} //end namespace model
|
||||
|
||||
@ -143,4 +143,36 @@ namespace network {
|
||||
LOG() << "获取个人信息->响应处理完毕 requestId" << req.requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::getFriendList(const QString loginSessionId)
|
||||
{
|
||||
//通过protobuf构造body
|
||||
bite_im::GetFriendListReq req;
|
||||
req.setRequestId(makeRequestId());
|
||||
req.setSessionId(loginSessionId);
|
||||
QByteArray body = req.serialize(&serializer);
|
||||
LOG() << "获取好友列表->发送请求 requestId=" << req.requestId() << ", loginSessionId= " << loginSessionId;
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* httpResp = this->sendHttpRequest("/service/friend/get_friend_list", body);
|
||||
|
||||
//处理响应
|
||||
connect(httpResp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto friendListResp = this->handleHttpResponse<bite_im::GetFriendListRsp>(httpResp, &ok, &reason);
|
||||
|
||||
if (!ok) {
|
||||
LOG() << "获取好友列表->失败 requestId= " << req.requestId() << ", reason= " << reason;
|
||||
}
|
||||
LOG() << "获取好友列表->成功";
|
||||
|
||||
//把结果保存在DataCenter中
|
||||
dataCenter->resetFriendList(friendListResp);
|
||||
|
||||
// 发送信号
|
||||
emit dataCenter->getFriendListDone();
|
||||
});
|
||||
}
|
||||
} //end namespace network
|
||||
@ -75,10 +75,12 @@ namespace network {
|
||||
|
||||
//释放httpResp对象
|
||||
httpResp->deleteLater();
|
||||
*ok = true;
|
||||
return respObj;
|
||||
}
|
||||
|
||||
void getMyself(const QString& loginSessionId);
|
||||
void getFriendList(const QString loginSessionId);
|
||||
private:
|
||||
model::DataCenter* dataCenter;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user