Skip to content

Commit

Permalink
添加好友
Browse files Browse the repository at this point in the history
  • Loading branch information
lixing9363 committed Dec 9, 2019
1 parent 69979f4 commit a79bed5
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 19 deletions.
11 changes: 11 additions & 0 deletions TCP_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ def find_all_info_by_account(account):
user.__dict__ = dict_user
return user

def search_user(account):
message = "SEARCH_USER %s" % account
sockfd.send(message.encode()) # 发送字节串

def add_friend(self_account,friend_account):
message = "ADD_FRIEND %s %s" % (self_account,friend_account)
sockfd.send(message.encode()) # 发送字节串

def agree_add_friend(self_account,friend_account):
message = "AGREE_ADD_FRIEND %s %s" % (self_account, friend_account)
sockfd.send(message.encode()) # 发送字节串

def download_head_img_by_account(img):
"""
Expand Down
52 changes: 43 additions & 9 deletions TCP_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,31 @@ def handle(connfd):
find_all_friend(connfd, list_data)
elif list_data[0] == "EXIT": # list_data = ["EXIT","account"]
do_exit(list_data)
elif list_data[
0] == "SEND_CHAT": # list_data = ['SEND_CHAT', 'admin admin1 李行 2019-12-02-13:09:28: 111111']
elif list_data[0] == "SEND_CHAT": # list_data = ['SEND_CHAT', 'admin admin1 李行 2019-12-02-13:09:28: 111111']
send_chat(list_data)
elif list_data[0] == "SEND_IMG": # list_data = ['SEND_IMG', 'file_name size recv_account send_account']
send_img(connfd, list_data)
elif list_data[0] == "UPLOAD_HEAD_IMG":
upload_head_img(connfd, list_data)
elif list_data[0] == "UPDATE_USER_INFO":
info_list = list_data[1].split(" ",3)
account = info_list[0]
img = info_list[1]
nickname = info_list[2]
sex = info_list[3]
result = db.update_user_info(account,img,nickname,sex)
update_user_info(list_data)
elif list_data[0] == "SEARCH_USER":
search_user(connfd, list_data)
elif list_data[0] == "ADD_FRIEND":
list_account = list_data[1].split(" ",1)
self_account = list_account[0]
friend_account = list_account[1]
for acco, connfd1 in online_user_connfd_dict.items():
message = "FLUSH_USER_INFO %s"
if acco == friend_account:
message = "REQUEST_ADD_FRIEND %s" % self_account
connfd1.send(message.encode())
elif list_data[0] == "AGREE_ADD_FRIEND":
list_account = list_data[1].split(" ", 1)
self_account = list_account[0]
friend_account = list_account[1]
db.add_friend(self_account,friend_account)
for acco, connfd1 in online_user_connfd_dict.items():
message = "FLUSH_FRIEND_LIST "
connfd1.send(message.encode())
sleep(0.2)

Expand All @@ -187,6 +196,31 @@ def handle(connfd):
print("接收消息出错")


def search_user(connfd, list_data):
account = list_data[1]
user = db.find_all_info_by_account(account)
message = "SEARCH_USER "
if user == False:
message += "NO"
connfd.send(message.encode())
else:
message += user.nickname
connfd.send(message.encode())


def update_user_info(list_data):
info_list = list_data[1].split(" ", 3)
account = info_list[0]
img = info_list[1]
nickname = info_list[2]
sex = info_list[3]
result = db.update_user_info(account, img, nickname, sex)
for acco, connfd1 in online_user_connfd_dict.items():
message = "FLUSH_USER_INFO %s"
connfd1.send(message.encode())
sleep(0.2)


def upload_head_img(connfd, list_data):
img_name = list_data[1]
filepath = "res/server_head_image/%s" % img_name
Expand Down
2 changes: 2 additions & 0 deletions database_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ def add_friend(self, account, friend_acc):
f_id = self.find_pid_by_account(friend_acc)

sql = "insert into user_friend_table values(%s, %s);"
sql2 = "insert into user_friend_table values(%s, %s);"
try:
self.cur.execute(sql, (u_id, f_id))
self.cur.execute(sql, (f_id, u_id))
self.db.commit()
except Exception:
self.db.rollback()
Expand Down
Binary file added res/chat_img/timg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/chat_img_client/timg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion res/database_sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ insert into user_table(pid,account_id,nickname,password,sex) values(100003,'admi
insert into user_table(pid,account_id,nickname,password,sex) values(100004,'admin4','管理员','admin','m');
insert into user_table(pid,account_id,nickname,password,sex) values(100005,'admin5','管理员','admin','m');
insert into user_table(account_id,nickname,password) values('admin6','管理员','admin');

update user_table set password=123456 where account_id='pengqi333';
update user_table set nickname=123456,sex='w',img = 'admin.jpg' where account_id='pengqi333';
select * from user_table;
update user_table set online_status=0 where account_id='admin4';

Expand Down
File renamed without changes
Binary file added res/login/组 3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/server_head_image/timg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 9 additions & 8 deletions view_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ def initUI(self):
# 设置右上角X
self.close_button = QPushButton(self)
self.close_button.setStyleSheet(
"QPushButton{background-image: url(res/login/关闭悬浮.png)} QPushButton{border:none;}")
"QPushButton{border-image: url(res/login/guanbi.png)} QPushButton{border:none;}")
self.close_button.setGeometry(616, 0, 34, 34)
self.close_button.pressed.connect(self.close)
# 设置最小化窗口
self.min_button = QPushButton(self)
self.min_button.setStyleSheet(
"QPushButton{background-image: url(res/login/最小化悬浮.png)} QPushButton{border:none;}")
"QPushButton{background-image: url(res/login/最小化.png)} QPushButton{border:none;}")
self.min_button.setGeometry(582, 0, 34, 34)
self.min_button.pressed.connect(self.showMinimized)
# 设置头像动图
self.gif = QMovie("res/login/dongtu.gif")
# self.gif = QMovie("res/login/dongtu.gif")
self.lable = QLabel(self)
self.lable.setGeometry(444, 46, 88, 88)
self.lable.setMovie(self.gif)
self.gif.start()
self.lable.setStyleSheet("border-image: url(res/main/logo.png)")
# self.lable.setMovie(self.gif)
# self.gif.start()
# 设置用户名图标
self.user_name_botton = PyqtTools.set_button(self, "res/login/用户名.png", 16, 16, 360, 166)
self.passwd_botton = PyqtTools.set_button(self, "res/login/密码.png", 16, 16, 359, 210)
Expand Down Expand Up @@ -80,7 +81,7 @@ def initUI(self):
# 设置左边蓝色框
self.left_button = QLabel(self)
self.left_button.setStyleSheet(
"background-image: url(res/login/矩形 1 拷贝.png)")
"background-image: url(res/login/组 3.png)")
self.left_button.setGeometry(0, 0, 325, 350)

# 设置按钮的事件
Expand Down Expand Up @@ -137,14 +138,14 @@ def eventFilter(self, object, event):
"QPushButton{background-image: url(res/login/关闭点击.png)} QPushButton{border:none;}")
if event.type() == QEvent.Leave:
self.close_button.setStyleSheet(
"QPushButton{background-image: url(res/login/关闭悬浮.png)} QPushButton{border:none;}")
"QPushButton{background-image: url(res/login/guanbi.png)} QPushButton{border:none;}")
if object == self.min_button:
if event.type() == QEvent.Enter:
self.min_button.setStyleSheet(
"QPushButton{background-image: url(res/login/最小化点击.png)} QPushButton{border:none;}")
if event.type() == QEvent.Leave:
self.min_button.setStyleSheet(
"QPushButton{background-image: url(res/login/最小化悬浮.png)} QPushButton{border:none;}")
"QPushButton{background-image: url(res/login/最小化.png)} QPushButton{border:none;}")
if object == self.login_button:
if event.type() == QEvent.Enter:
self.login_button.setStyleSheet(
Expand Down
34 changes: 33 additions & 1 deletion view_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QLabel

from TCP_client import *
from model_user import User
from view_friend_list import ViewFriendList
from view_search import Ui_SearchView
from view_user_info import Ui_ViewUserInfo
from view_widget import Widget
from multiprocessing import Process
Expand All @@ -19,6 +21,7 @@ class Ui_ViewMain(Widget):
flush_msg = pyqtSignal(object)
recv_chat_msg = pyqtSignal(object)
recv_chat_img = pyqtSignal(object)
search_nickname_signal = pyqtSignal(object)

def __init__(self, account,sockfd):
super().__init__()
Expand All @@ -27,6 +30,7 @@ def __init__(self, account,sockfd):
self.user = find_all_info_by_account(account)
self.friend_list = find_all_friends(account)
self.user_info_window = Ui_ViewUserInfo(self.user)
self.search_window = Ui_SearchView(account)
self.setupUi()

def setupUi(self):
Expand Down Expand Up @@ -139,6 +143,15 @@ def setupUi(self):
self.pushButton_add.setStyleSheet("background-image: url(res/main/按钮.png);")
self.pushButton_add.setText("")
self.pushButton_add.setObjectName("pushButton_add")
self.pushButton_add.clicked.connect(self.open_search_window)
self.gif = QMovie("res/main/红点.gif")
self.lable_message = QLabel(self.frame_2)
self.lable_message.move(256, 103)
self.lable_message.setMovie(self.gif)
self.gif.start()
# self.gif.stop()
self.lable_message.hide()


# 天气显示
self.label_weather = QtWidgets.QLabel(self.frame_2)
Expand Down Expand Up @@ -174,6 +187,7 @@ def setupUi(self):
self.flush_msg.connect(self.friends.singla_flush)
self.recv_chat_msg.connect(self.friends.singla_recv_chat_message)
self.recv_chat_img.connect(self.friends.singla_recv_chat_image)
self.search_nickname_signal.connect(self.search_window.show_result)

self.thread = threading.Thread(target=self.recv_message)
self.thread.setDaemon(True)
Expand All @@ -182,6 +196,10 @@ def setupUi(self):
# self.process.daemon = True
# self.process.start()

def open_search_window(self):
self.search_window.show()
self.lable_message.hide()

def open_info_window(self):
self.user_info_window.show()

Expand All @@ -201,7 +219,21 @@ def recv_message(self):
data = self.do_send_chat_img(data, list_data)
elif list_data[0] == "FLUSH_USER_INFO":
self.do_flush_user_info()
elif list_data[0] == "SEARCH_USER":
self.search_nickname = list_data[1]
self.search_result(self.search_nickname)
elif list_data[0] == "REQUEST_ADD_FRIEND":
request_add_account = list_data[1]
self.search_window.label.setText("%s请求添加好友"%request_add_account)
self.search_window.pushButton_ignore.show()
self.search_window.pushButton_agree.show()
self.search_window.search_account = request_add_account
self.lable_message.show()
elif list_data[0] == "FLUSH_FRIEND_LIST":
self.do_friend_online(list_data)

def search_result(self,msg):
self.search_nickname_signal.emit(msg)

def do_flush_user_info(self):
self.user = find_all_info_by_account(self.user.account_id)
Expand Down
111 changes: 111 additions & 0 deletions view_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication

from TCP_client import find_all_info_by_account, search_user, add_friend, agree_add_friend
from view_widget import Widget


class Ui_SearchView(Widget):
def __init__(self,account):
super().__init__()
self.account = account
self.search_account=""
self.setupUi()
def setupUi(self):
self.setObjectName("SearchView")
self.resize(291, 514)
self.lineEdit = QtWidgets.QLineEdit(self)
self.lineEdit.setGeometry(QtCore.QRect(10, 80, 200, 28))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(240, 80, 41, 25))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.search)
self.frame = QtWidgets.QFrame(self)
self.frame.setGeometry(QtCore.QRect(0, 140, 288, 71))
self.frame.setStyleSheet("border:none;")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(20, 25, 100, 17))
self.label.setText("")
self.label.setObjectName("label")
self.pushButton_2 = QtWidgets.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(230, 20, 41, 25))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.hide()
self.pushButton_2.clicked.connect(self.do_add)

self.pushButton_agree = QtWidgets.QPushButton(self.frame)
self.pushButton_agree.setGeometry(QtCore.QRect(230, 20, 41, 25))
self.pushButton_agree.setObjectName("pushButton_2")
self.pushButton_agree.setText("同意")
self.pushButton_agree.hide()
self.pushButton_agree.clicked.connect(self.agree_add)

self.pushButton_ignore = QtWidgets.QPushButton(self.frame)
self.pushButton_ignore.setGeometry(QtCore.QRect(166, 20, 51, 25))
self.pushButton_ignore.setObjectName("pushButton_2")
self.pushButton_ignore.setText("忽略")
self.pushButton_ignore.hide()

self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(260, 0, 34, 34))
self.pushButton_3.setText("")
self.pushButton_3.setStyleSheet(
"QPushButton{border-image: url(res/login/guanbi.png)} QPushButton{border:none;}")
self.pushButton_3.clicked.connect(self.close)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(225, 0, 34, 34))
self.pushButton_4.setText("")
self.pushButton_4.setStyleSheet(
"QPushButton{background-image: url(res/login/最小化.png)} QPushButton{border:none;}")
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.showMinimized)
self.label_2 = QtWidgets.QLabel(self)
self.label_2.setGeometry(QtCore.QRect(10, 10, 67, 17))
self.label_2.setObjectName("label_2")

self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)

def agree_add(self):
agree_add_friend(self.account,self.search_account)
self.label.setText("")
self.pushButton_agree.hide()
self.pushButton_ignore.hide()
self.close()

def show_result(self,msg):
if msg == "NO":
self.label.setText("没有查找到此用户")
else:
self.label.setText(msg)
self.pushButton_2.show()

def do_add(self):
add_friend(self.account,self.search_account)
self.label.setText("")
self.pushButton_2.hide()
self.close()

def search(self):
self.search_account = self.lineEdit.text()
search_user(self.search_account)

def retranslateUi(self, SearchView):
_translate = QtCore.QCoreApplication.translate
SearchView.setWindowTitle(_translate("SearchView", "Form"))
self.pushButton.setText(_translate("SearchView", "搜索"))
self.pushButton_2.setText(_translate("SearchView", "添加"))
self.label_2.setText(_translate("SearchView", "搜索好友"))

if __name__ == '__main__':
app = QApplication(sys.argv)
login = Ui_SearchView("admin")
login.show()
sys.exit(app.exec_())

0 comments on commit a79bed5

Please sign in to comment.