diff --git a/ChatRoomPlan.emmx b/ChatRoomPlan.emmx index 238a58b..76dc9ac 100644 Binary files a/ChatRoomPlan.emmx and b/ChatRoomPlan.emmx differ diff --git a/main/__pycache__/mysql.cpython-310.pyc b/main/__pycache__/mysql.cpython-310.pyc new file mode 100644 index 0000000..a53ba04 Binary files /dev/null and b/main/__pycache__/mysql.cpython-310.pyc differ diff --git a/main/client/Client.py b/main/client/Client.py index 7450daf..a0857db 100644 --- a/main/client/Client.py +++ b/main/client/Client.py @@ -46,17 +46,11 @@ def stop_loop(self): @abstractmethod def on_connect(self, client, userdata, flags, rc): - if rc == 0: - print("Connected successfully") - else: - print("Failed to connect, return code %d\n", rc) + pass @abstractmethod def on_message(self, client, userdata, msg): - # 规定传入数据均为dict的形式 - data = eval(msg.payload.decode('utf-8')) - print(data) - return data + pass def clientStart(self):#启动进程,使用threading(python自带进程管理库)进行管理 loopThread = threading.Thread(target=self.start_loop) loopThread.start() @@ -93,11 +87,6 @@ def on_message(self,client, userdata, msg):#接受数据 return data def publishRegister(self): - T1 = time.time() # 记录发布时间 - message = f"Hello World | {T1}" - topic = 'test/topic' - self.client.publish(topic, message,2) - returnTopic=self.userName+"register" #数据发送特定格式 data = {'userName': self.userName, 'userPwd': self.userPwd, 'returnTopic': returnTopic} @@ -110,14 +99,15 @@ class Login(Client): def __init__(self,userName,userPwd): super().__init__(userName,userPwd) self.clientStart() - self.publishRegister() + self.publishLogin() + def on_connect(self, client, userdata, flags, rc):#链接 if rc == 0: print("Connected successfully") - returnRegister=self.userName+"register" - client.subscribe(returnRegister) # 订阅 return 主题 + returnLogin=self.userName+"login" + client.subscribe(returnLogin) # 订阅 return 主题 else: print("Failed to connect, return code %d\n", rc) @@ -128,22 +118,29 @@ def on_message(self,client, userdata, msg):#接受数据 #读取数据 code=data.get('code') message=data.get('message') - if code==0: + if code==0: #表示登入失败 print(message) - if code==1: + if code==1: #表示登入成功 print(message) return data def publishLogin(self): - pass + returnTopic=self.userName+"login" + #数据发送特定格式 + data = {'userName': self.userName, 'userPwd': self.userPwd, 'returnTopic': returnTopic} + # qos1 + self.client.publish(publish_topic["login_topic"], str(data).encode(), 1) + # client.loop() + print('发布信息 ', publish_topic['login_topic'], ' 成功') + ###################################### #tao yu class Like(Client): - def __init__(self,userName,userPwd): - super().__init__(userName,userPwd) + def __init__(self,messageId): + super().__init__() self.clientStart() - self.publishRegister() + self.publish_like(messageId) @abstractmethod def on_connect(self, client, userdata, flags, rc): @@ -160,8 +157,8 @@ def publish_like(client, messageId): #################################################################### def main(): - r=Register("hhh","123456") - b=Register("jjj",'123') + #Register("hhhhh","123456") + Login("hhhhhh","123456") main() diff --git a/main/config/__pycache__/config.cpython-310.pyc b/main/config/__pycache__/config.cpython-310.pyc new file mode 100644 index 0000000..32b391d Binary files /dev/null and b/main/config/__pycache__/config.cpython-310.pyc differ diff --git a/main/mysql.py b/main/mysql.py index e0c4f46..c61d802 100644 --- a/main/mysql.py +++ b/main/mysql.py @@ -20,7 +20,7 @@ class Meta: class UserModel(BaseModel): id=AutoField() name = CharField(max_length=64) - password = CharField(max_length=32) + password = CharField(max_length=128) class Meta: @@ -36,11 +36,21 @@ class ChatModel(BaseModel): class Meta: db_table = 'chat' +#点赞类 +class LikeModel(BaseModel): + id = AutoField() + messageId = ForeignKeyField(ChatModel, backref='likes') + count = IntegerField(default=0) + + class Meta: + db_table = 'like' + class CreateTable: @staticmethod def create(): db.connect() - db.create_tables([UserModel, ChatModel]) + db.create_tables([UserModel, ChatModel,LikeModel]) + #用户操作 @@ -53,7 +63,6 @@ def hash_password(password): return hashed # 验证密码 - def check_password(hashed, password): # 验证密码 return bcrypt.checkpw(password.encode('utf-8'), hashed) @@ -73,7 +82,7 @@ def chackName(name):#检测用户是否存在,不存在则打印提示并返 print(f"An error occurred: {error}") return None @staticmethod - def chackPassword(password):#如果密码正确则 + def chackPassword(password):#如果密码正确则返回True # 密码加密 hashPassword = hash_password(password) return check_password(hashPassword,password) @@ -89,6 +98,13 @@ def addUser(name, password): print(error) return False + @staticmethod + def verifyUser(name, password): + user = UserManage.chackName(name) + if user is None: + return False + hashed_password_bytes = user.password.encode('utf-8') + return check_password(hashed_password_bytes, password) ################################################################ class ChatManage: @@ -123,7 +139,6 @@ def AddChat(message,senderId): return 'Fail' -CreateTable.create() ################################################################# #taoyu: @@ -174,3 +189,6 @@ def like_message(messageId): except Exception as error: print(f"An error occurred: {error}") return None + + + diff --git a/main/server/Server.py b/main/server/Server.py index 9203c98..747838f 100644 --- a/main/server/Server.py +++ b/main/server/Server.py @@ -65,15 +65,12 @@ class Register(Server): def __init__(self): super().__init__() self.serverStart() - - def on_connect(self, client, userdata, flags, rc): if rc == 0: print("Connected successfully") - client.subscribe(publish_topic['register_topic']) # 订阅 login 主题 + client.subscribe(publish_topic['register_topic']) # 订阅 register 主题 else: print("Failed to connect, return code %d\n", rc) - def on_message(self, client, userdata, msg): # 规定传入数据均为dict的形式 data = eval(msg.payload.decode('utf-8')) @@ -128,19 +125,18 @@ def on_message(self, client, userdata, msg): def login(self, userName, userPwd, returnTopic): user = UserManage.chackName(userName) if user == None: - print("用户不存在,允许注册") - if UserManage.addUser(userName, userPwd): - print("用户添加成功!") - data = {'code': 1, "message": "用户添加成功!"} + print("账号或密码错误") + data = {'code': 0, 'message':"账号或密码错误"} + self.client.publish(returnTopic, str(data).encode(), 1) + else: + if UserManage.verifyUser(userName,userPwd): + print(f"{userName}通过验证,欢迎") + data = {'code': 1, "message": f"验证通过,欢迎回来{userName}"} self.client.publish(returnTopic, str(data).encode(), 1) else: - print("用户添加失败,错误发生在服务器!") - data = {'code': 0, "message": "用户添加失败,错误发生在服务器!"} + print("账号或密码错误") + data = {'code': 0, 'message': "账号或密码错误"} self.client.publish(returnTopic, str(data).encode(), 1) - else: - print("用户存在,不允许注册") - data = {'code': 0, "message": "用户添加失败,用户存在"} - self.client.publish(returnTopic, str(data).encode(), 1) ################################################################### #taoyu