This commit is contained in:
shuaikangzhou 2023-01-21 13:59:37 +08:00
parent a130f4ba00
commit f5989290f9
6 changed files with 350 additions and 143 deletions

129
TEST.py
View File

@ -1,94 +1,45 @@
# -*- coding: utf-8 -*-
"""
Created on Sat May 9 17:14:37 2020
class img_viewed(QWidget):
@author: Giyn
"""
def __init__(self, parent=None):
super(img_viewed, self).__init__(parent)
self.parent = parent
self.width = 960
self.height = 500
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextBrowser, QVBoxLayout, QHBoxLayout, QMessageBox
from PyQt5.QtGui import QIcon
self.scroll_ares_images = QScrollArea(self)
self.scroll_ares_images.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self)
self.scrollAreaWidgetContents.setObjectName('scrollAreaWidgetContends')
# 进行网络布局
self.gridLayout = QGridLayout(self.scrollAreaWidgetContents)
self.scroll_ares_images.setWidget(self.scrollAreaWidgetContents)
self.scroll_ares_images.setGeometry(20, 50, self.width, self.height)
self.vertocal1 = QVBoxLayout()
# self.meanbar = QMenu(self)
# self.meanbar.addMenu('&菜单')
# self.openAct = self.meanbar.addAction('&Open',self.open)
# self.startAct =self.meanbar.addAction('&start',self.start_img_viewer)
self.open_file_pushbutton = QPushButton(self)
self.open_file_pushbutton.setGeometry(150, 10, 100, 30)
self.open_file_pushbutton.setObjectName('open_pushbutton')
self.open_file_pushbutton.setText('打开文件夹...')
self.open_file_pushbutton.clicked.connect(self.open)
self.start_file_pushbutton = QPushButton(self)
self.start_file_pushbutton.setGeometry(750, 10, 100, 30)
self.start_file_pushbutton.setObjectName('start_pushbutton')
self.start_file_pushbutton.setText('开始')
self.start_file_pushbutton.clicked.connect(self.start_img_viewer)
self.vertocal1.addWidget(self.scroll_ares_images)
class Simple_Window(QWidget):
def __init__(self):
super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
self.setWindowTitle("记事本")
self.setWindowIcon(QIcon('NoteBook.png')) # 设置窗口图标
self.resize(412, 412)
self.text_browser = QTextBrowser(self) # 实例化一个QTextBrowser对象
# self.text_browser.setText("<h1>Hello World!</h1>") # 设置编辑框初始化时显示的文本
# self.text_browser.setReadOnly(False) # 调用setReadOnly方法并传入False参数即可编辑文本浏览框编辑框也可以变成只读
self.save_button = QPushButton("Save", self)
self.clear_button = QPushButton("Clear", self)
self.add_button = QPushButton("Add", self)
self.save_button.clicked.connect(lambda: self.button_slot(self.save_button))
self.clear_button.clicked.connect(lambda: self.button_slot(self.clear_button))
self.add_button.clicked.connect(self.add_text)
self.h_layout = QHBoxLayout()
self.v_layout = QVBoxLayout()
self.h_layout.addWidget(self.save_button)
self.h_layout.addWidget(self.clear_button)
self.h_layout.addWidget(self.add_button)
self.v_layout.addWidget(self.text_browser)
self.v_layout.addLayout(self.h_layout)
self.setLayout(self.v_layout)
def button_slot(self, button):
if button == self.save_button:
choice = QMessageBox.question(self, "Question", "Do you want to save it?", QMessageBox.Yes | QMessageBox.No)
if choice == QMessageBox.Yes:
with open('Second text.txt', 'w') as f:
f.write(self.text_browser.toPlainText())
self.close()
elif choice == QMessageBox.No:
self.close()
elif button == self.clear_button:
self.text_browser.clear()
def add_text(self):
# self.text_browser.append("<h1>Hello World!</h1>") # 调用append方法可以向文本浏览框中添加文本
html = """
<div class="user-group" style="padding: 6px;
display: flex;
display: -webkit-flex;
justify-content: flex-end;
-webkit-justify-content: flex-end;
">
<div class="user-msg" style="text-align: right;
width: 75%;
position: relative;">
<span class="user-reply" style="display: inline-block;
padding: 8px;
border-radius: 4px;
margin:0 15px 12px;
text-align: left;
background-color: #9EEA6A;
box-shadow: 0px 0px 2px #bbb;
">我要抢楼</span>
<i style="width: 0;
height: 0;
position: absolute;
top: 10px;
display: inline-block;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
right: 4px;
border-left: 12px solid #9EEA6A;"></i>
</div>
<img class="user-img" src="./app/data/avatar/2a/42/user_2a427a26f96058921da245444ab542f5.png" width="45px" height="45px";/>
</div>
"""
self.text_browser.insertHtml(html)
if __name__ == "__main__":
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Simple_Window()
window.show()
sys.exit(app.exec())
windo = img_viewed()
windo.show()
sys.exit(app.exec_())

View File

@ -121,7 +121,11 @@ def get_conRemark(username):
else:
return result[1]
def get_nickname(username):
sql = 'select nickname,alias from rcontact where username=?'
cursor.execute(sql, [username])
result = cursor.fetchone()
return result
def avatar_md5(wxid):
m = hashlib.md5()
# 参数必须是byte类型否则报Unicode-objects must be encoded before hashing错误
@ -178,20 +182,31 @@ def download_emoji(imgPath, url):
f.write(resp.content)
return imgPath
def get_chatroom_displayname(chatroom,username):
sql = 'select memberlist,displayname from chatroom where chatroomname =?'
cursor.execute(sql,[chatroom])
result = cursor.fetchone()
wxids = result[0].split(';')
names = result[1].split('')
id = wxids.index(username)
print(result[0])
print(wxids)
for i in wxids:
print(get_conRemark(i))
return names[id]
def get_contacts():
sql = '''
select * from rcontact
where type=3 or type = 333
'''
cursor.execute(sql)
result = cursor.fetchall()
return result
if __name__ == '__main__':
# rconversation = get_rconversation()
# for i in rconversation:
# print(i)
timestamp2str(1632219794000)
conremark = get_conRemark('wxid_q3ozn70pweud22')
print(conremark)
print(get_avator('wxid_q3ozn70pweud22'))
me = get_myinfo()
print(me.__dict__)
# r = get_message('wxid_78ka0n92rzzj22', 0)
# r.reverse()
# for i in r:
# print(i)
# print(len(r), type(r))
print(get_emoji('f5f8a6116e177937ca9795c47f10113d'))
contacts = get_contacts()
for contact in contacts:
print(contact)

View File

@ -12,21 +12,25 @@ import socket # 导入socket模块
import datetime
import json
import time
import xmltodict
from PIL import Image
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from .chatUi import *
from ...DataBase import data
from ...ImageBox.ui import MainDemo
class ChatController(QWidget, Ui_Dialog):
exitSignal = pyqtSignal()
urlSignal = pyqtSignal(QUrl)
# username = ''
def __init__(self, Me, parent=None):
super(ChatController, self).__init__(parent)
self.chatroomFlag = None
self.ta_avatar = None
self.setupUi(self)
self.setWindowTitle('WeChat')
@ -50,6 +54,12 @@ class ChatController(QWidget, Ui_Dialog):
self.showChat()
def initui(self):
self.qurl = QUrl('baidu.com')
# self.urlSignal.connect(self.hyperlink)
self.message.setOpenLinks(False)
self.message.setOpenExternalLinks(False)
# self.message.anchorClicked(self.hyperlink())
self.message.anchorClicked.connect(self.hyperlink)
with open('./wechat.html', 'r', encoding='utf-8') as f:
self.message.setHtml(f.read())
self.textEdit = myTextEdit(self.frame)
@ -87,10 +97,9 @@ class ChatController(QWidget, Ui_Dialog):
def showChat(self):
"""
显示聊天界面
显示联系人界面
:return:
"""
print('show')
if self.show_flag:
return
self.show_flag = True
@ -133,6 +142,10 @@ class ChatController(QWidget, Ui_Dialog):
self.message.clear()
self.message.append(talkerId)
self.ta_username = talkerId
if '@chatroom' in talkerId:
self.chatroomFlag = True
else:
self.chatroomFlag = False
self.ta_avatar = data.get_avator(talkerId)
self.textEdit.setFocus()
self.Thread.ta_u = talkerId
@ -213,20 +226,58 @@ class ChatController(QWidget, Ui_Dialog):
self.check_time(msg_time)
if msgType == '1':
# return
self.show_text(isSend, content)
elif msgType == '3':
self.show_img(isSend, imgPath)
# return
self.show_img(isSend, imgPath, content)
elif msgType == '47':
self.show_emoji(isSend, imgPath)
# quit()
# return
self.show_emoji(isSend, imgPath, content)
elif msgType == '268445456':
self.show_recall_information(content)
elif msgType == '922746929':
self.pat_a_pat(content)
# self.message.moveCursor(self.message.textCursor().End)
def show_emoji(self, isSend, imagePath):
def pat_a_pat(self, content):
pat_data = xmltodict.parse(content)
pat_data = pat_data['msg']['appmsg']['patMsg']['records']['record']
fromUser = pat_data['fromUser']
pattedUser = pat_data['pattedUser']
template = pat_data['template']
template = ''.join(template.split('${pattedusername@textstatusicon}'))
template = ''.join(template.split('${fromusername@textstatusicon}'))
template = template.replace(f'${{{fromUser}}}', data.get_conRemark(fromUser))
template = template.replace(f'${{{pattedUser}}}', data.get_conRemark(pattedUser))
print(template)
html = '''
<table align="center" style="vertical-align: middle;">
<tbody>
<tr>
<td>%s</td>
</tr>
</tbody>
</table>''' % template
self.message.insertHtml(html)
def show_recall_information(self, content):
html = '''
<table align="center" style="vertical-align: middle;">
<tbody>
<tr>
<td>%s</td>
</tr>
</tbody>
</table>''' % content
self.message.insertHtml(html)
def show_emoji(self, isSend, imagePath, content):
imgPath = data.get_emoji(imagePath)
image = Image.open(imgPath)
imagePixmap = image.size # 宽高像素
# 设置最大宽度
if imagePixmap[0]<150:
if imagePixmap[0] < 150:
size = ""
else:
size = '''height="150" width="150"'''
@ -234,38 +285,82 @@ class ChatController(QWidget, Ui_Dialog):
<td style="border: 1px #000000 solid;" height="150">
<img src="{0}" {1} >
</td>
'''.format(imgPath,size)
'''.format(imgPath, size)
style = 'vertical-align: top'
if isSend:
self.right(html, style=style)
else:
if self.chatroomFlag:
username = content.split(':')[0]
self.chatroom_left(html, username=username, style=style)
self.left(html, style=style)
def show_img(self, isSend, imgPath):
def show_img(self, isSend, imgPath, content):
'THUMBNAIL_DIRPATH://th_29cd0f0ca87652943be9ede365aabeaa'
imgPath = imgPath.split('th_')[1]
imgPath = f'./app/data/image2/{imgPath[0:2]}/{imgPath[2:4]}/th_{imgPath}'
html = '''
<td style="border: 1px #000000 solid"><img align="right" src="%s"/></td>
''' % imgPath
<td style="border: 1px #000000 solid">
<a href="%s" target="_blank">
<img herf= "baidu.com" align="right" src="%s"/>
</a>
</td>
''' % (imgPath,imgPath)
style = 'vertical-align: top'
if isSend:
self.right(html, style=style)
else:
self.left(html, style=style)
if self.chatroomFlag:
username = content.split(':')[0]
self.chatroom_left(html, username=username, style=style)
else:
self.left(html, style=style)
def show_text(self, isSend, content):
if isSend:
html = '''
<td style="background-color: #9EEA6A;border-radius: 40px;">&nbsp;%s&nbsp;</td>
''' % (content)
''' % content
self.right(html)
else:
html = '''
<td style="background-color: #fff;border-radius: 4px;">&nbsp;%s&nbsp;</td>
''' % (content)
self.left(html)
if self.chatroomFlag:
# print(content)
'wxid_mv4jjhc0w0w521:'
username = content.split(':')[0]
msg = ''.join(content.split(':')[1:])
# avatar = data.get_avator(username)
html = '''
<td max-width = 300 style="background-color: #fff;border-radius: 4px;">
%s
</td>
''' % (msg)
# self.left(html, avatar=avatar)
self.chatroom_left(html, username=username)
else:
html = '''
<td max-width = 300 style="background-color: #fff;border-radius: 4px;">&nbsp;%s&nbsp;</td>
''' % (content)
self.left(html)
def clearImagePath(self,imgpath):
path = imgpath.split('/')
newPath = '/'.join(path[:-1])+'/'+path[-1][3:]+'.jpg'
if os.path.exists(newPath):
return newPath
newPath = '/'.join(path[:-1]) + '/' + path[-1][3:] + '.png'
if os.path.exists(newPath):
return newPath
newPath = '/'.join(path[:-1]) + '/' + path[-1]+ 'hd'
if os.path.exists(newPath):
return newPath
return imgpath
def hyperlink(self, url:QUrl):
path = self.clearImagePath(url.path())
print(url.path(),path)
self.imagebox = MainDemo()
self.imagebox.show()
self.imagebox.box.set_image(path)
def right(self, content, style='vertical-align: middle'):
html = '''
@ -286,21 +381,75 @@ class ChatController(QWidget, Ui_Dialog):
# print(html)
self.message.insertHtml(html)
def left(self, content, style='vertical-align: middle'):
def left(self, content, avatar=None, style='vertical-align: middle'):
if not avatar:
avatar = self.ta_avatar
if self.chatroomFlag == 5:
try:
username, msg = content.split('\n')
avatar = data.get_avator(username)
html = '''
<div>
<table align="left" style="%s;">
<tbody>
<tr>
<td width="15"></td>
<td rowspan="2" style="border: 1px #000000 solid"><img align="right" src="%s" width="45" height="45"/></td>
<td></td>
<td></td>
</tr>
<tr>
<td width="15"></td>
<td></td>
%s
</tr>
</tbody>
</table>
</div>
''' % (style, avatar, msg)
except:
return
else:
html = '''
<div>
<table align="left" style="%s;">
<tbody>
<tr>
<td width="15"></td>
<td style="border: 1px #000000 solid"><img align="right" src="%s" width="45" height="45"/></td>
<td></td>
%s
</tr>
</tbody>
</table>
</div>
''' % (style, avatar, content)
self.message.insertHtml(html)
def chatroom_left(self, content, username, style='vertical-align: middle'):
# if username:
avatar = data.get_avator(username)
# conRemark = data.get_conRemark(username)
conRemark = data.get_conRemark(username)
html = '''
<div>
<table align="left" style="%s;">
<tbody>
<tr>
<td width="15"></td>
<td style="border: 1px #000000 solid"><img align="right" src="%s" width="45" height="45"/></td>
<td></td>
%s
</tr>
</tbody>
</table>
</div>
''' % (style, self.ta_avatar, content)
<div>
<table align="left" style="%s;">
<tbody>
<tr>
<td width="15"></td>
<td rowspan="2" style="border: 1px #000000 solid"><img align="right" src="%s" width="45" height="45"/></td>
<td></td>
<td>%s</td>
</tr>
<tr>
<td width="15"></td>
<td></td>
%s
</tr>
</tbody>
</table>
</div>
''' % (style, avatar, conRemark, content)
self.message.insertHtml(html)
def destroy_me(self):

View File

@ -17,6 +17,7 @@ from PyQt5.QtGui import *
from .mainviewUi import *
from app.DataBase import data
from .chat import chat
from .contact import contact
class MainWinController(QMainWindow, Ui_Dialog):
@ -31,7 +32,8 @@ class MainWinController(QMainWindow, Ui_Dialog):
self.Me = data.get_myinfo()
self.chatView = chat.ChatController(self.Me, parent=self.frame_main)
self.chatView.setVisible(False)
self.contactView = contact.ContactController(self.Me, parent=self.frame_main)
self.contactView.setVisible(False)
self.btn_chat.clicked.connect(self.chat_view) # 聊天按钮
self.btn_contact.clicked.connect(self.contact_view)
self.btn_myinfo.clicked.connect(self.myInfo)
@ -40,9 +42,11 @@ class MainWinController(QMainWindow, Ui_Dialog):
self.btn_about.setContextMenuPolicy(Qt.CustomContextMenu)
self.btn_about.customContextMenuRequested.connect(self.create_rightmenu) # 连接到菜单显示函数
self.last_btn = None
self.lastView = None
self.show_avatar()
# 创建右键菜单函数
def create_rightmenu(self):
# 菜单对象
self.groupBox_menu = QMenu(self)
@ -59,6 +63,7 @@ class MainWinController(QMainWindow, Ui_Dialog):
# self.actionB.triggered.connect(self.button_2)
self.groupBox_menu.popup(QCursor.pos()) # 声明当鼠标在groupBox控件上右击时在鼠标位置显示右键菜单 ,exec_,popup两个都可以
def show_avatar(self):
avatar = data.get_avator(self.Me.username)
pixmap = QPixmap(avatar).scaled(80, 80) # 按指定路径找到图片
@ -83,6 +88,8 @@ class MainWinController(QMainWindow, Ui_Dialog):
self.last_btn.setStyleSheet("QPushButton {background-color: rgb(240,240,240);}"
"QPushButton:hover{background-color: rgb(209,209,209);}\n")
self.last_btn = self.btn_contact
self.setviewVisible(self.contactView)
self.contactView.showContact()
def myInfo(self):
self.now_btn = self.btn_myinfo
@ -95,10 +102,11 @@ class MainWinController(QMainWindow, Ui_Dialog):
def about(self):
QMessageBox.about(self, "关于",
"关于作者\n姓名:周帅康\n学号2020303457"
"关于作者\n姓名:周帅康\n联系方式863909694"
)
def setviewVisible(self, view):
view.setVisible(True)
if view != self.chatView:
self.chatView.setVisible(False)
if view != self.lastView and self.lastView:
self.lastView.setVisible(False)
self.lastView = view

View File

@ -7,3 +7,35 @@
@Version : Python3.10
@comment : ···
"""
Type = {
'1': '文字',
'3': '图片',
'43': '视频',
'-1879048185': '微信运动排行榜',
'5': '',
'47': '表情包',
'268445456': '撤回的消息',
'34': '语音',
'419430449': '转账',
'50': '语音电话',
'100001': '领取红包',
'10000': '消息已发出,但被对方拒收了。',
'822083633': '回复消息',
'922746929': '拍一拍',
'1090519089': '发送文件',
'318767153': '付款成功',
'436207665': '发红包',
}
'''
wxid_ewi8gfgpp0eu22:
<?xml version="1.0"?>
<msg>
<videomsg aeskey="83df0143b64616922a12ba14635d572b"
cdnvideourl="3057020100044b30490201000204828e296a02034c53d9020425cc6ddf020463a93395042465653734303464352d636431642d346234322d383434632d3938623138316365646263360204010400040201000405004c53d900" cdnthumbaeskey="83df0143b64616922a12ba14635d572b"
cdnthumburl="3057020100044b30490201000204828e296a02034c53d9020425cc6ddf020463a93395042465653734303464352d636431642d346234322d383434632d3938623138316365646263360204010400040201000405004c53d900" length="100104377" playlength="603" cdnthumblength="13085" cdnthumbwidth="306" cdnthumbheight="540" fromusername="wxid_ewi8gfgpp0eu22" md5="f337bdc19fc2ce00a36660487792975d" newmd5="166392b4f9445f4772922a48e4762c6d" isplaceholder="0" rawmd5="" rawlength="0" cdnrawvideourl="" cdnrawvideoaeskey="" overwritenewmsgid="0" isad="0" />
</msg>
'''
'''
<template><![CDATA["${wxid_ip8kl703gar522}${fromusername@textstatusicon}" 拍了拍 "${wxid_i6zgnagsvrsk22}${pattedusername@textstatusicon}" 并请她喝奶茶]]></template>
'''

View File

@ -18,22 +18,74 @@
<table align="right" style="vertical-align: middle;">
<tbody>
<tr>
<td style="background-color: #9EEA6A;right: 4px;border-radius: 4px;">%s 13245:</td>
<td>
%s 13245:
</td>
<td></td>
<td rowspan="2" style="border: 1px #000000 solid">
<a href="D:\Project\Python\WeChatMsg\app\data\icon.png" target="_blank">
<img align="right" src="./app/data/avatar/2a/42/user_2a427a26f96058921da245444ab542f5.png" width="45" height="45"/>
</a>
</td>
<td rowspan="2">ADC</td>
</tr>>
<tr>
<td style="background-color: #9EEA6A;right: 4px;border-radius: 4px;">
%s 13245:
</td>
<td width="55" style=""> </td>
<td style="border: 1px #000000 solid"><img align="right" src="./app/data/avatar/2a/42/user_2a427a26f96058921da245444ab542f5.png" width="45" height="45"/></td>
<!-- <td style="border: 1px #000000 solid"><img align="right" src="./app/data/avatar/2a/42/user_2a427a26f96058921da245444ab542f5.png" width="45" height="45"/></td>-->
<td width="15"></td>
</tr>
</tbody>
</table>
</div>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table height="300" width="300" border="1" >
<caption>我是标题</caption>
<thead>
<tr>
<th>英雄</th>
<th>性别</th>
<th>职业</th>
</tr>
</thead>
<tbody>
<tr>
<td>亚索</td>
<td></td>
<td >中单</td>
</tr>
<tr>
<td>卡莎</td>
<td></td>
<td rowspan="2">ADC</td>
</tr>
<tr>
<td>寒冰</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
<!-- <body>
<div class="mobile-page">
<div class="user-group" style="padding: 6px;
display: flex;
display: -webkit-flex;
justify-content: flex-end;
-webkit-justify-content: flex-end;
-webkit-justify-content: flex-end;
">
<div class="user-msg" style="text-align: right;
width: 75%;