Skip to content

Commit

Permalink
新增头像修改功能
Browse files Browse the repository at this point in the history
  • Loading branch information
tonquer committed May 14, 2021
1 parent 63b4516 commit e9fa777
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 30 deletions.
30 changes: 7 additions & 23 deletions src/qt/chat/qtchatroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from conf import config
from src.qt.chat.chat_ws import ChatWebSocket
from src.qt.chat.qtchatroommsg import QtChatRoomMsg
from src.qt.com.qtbubblelabel import QtBubbleLabel
from src.qt.com.qticon import IconList
from src.qt.com.qtloading import QtLoading
from src.qt.util.qttask import QtTask
Expand Down Expand Up @@ -310,29 +311,12 @@ def SendMsg(self, imageData=None):

def OpenPicture(self):
try:
filename = QFileDialog.getOpenFileName(self, "Open Image", self.cachePath, "Image Files(*.jpg *.png)")
if filename and len(filename) > 1:
name = filename[0]
picFormat = filename[1]
baseName = os.path.basename(name)
if baseName[-3:] == "png":
picFormat = "png"
elif baseName[-3:] == "jpg":
picFormat = "jpeg"
elif baseName[-3:] == "gif":
picFormat = "gif"
else:
return

if os.path.isfile(name):
self.cachePath = os.path.dirname(name)

f = open(name, "rb")
data = f.read()
f.close()
imgData = base64.b64encode(data).decode("utf-8")
imgData = "data:image/" + picFormat + ";base64," + imgData
self.SendMsg(imgData)
data, name, picFormat = QtBubbleLabel.OpenPicture(self, self.cachePath)
if data:
self.cachePath = os.path.dirname(name)
imgData = base64.b64encode(data).decode("utf-8")
imgData = "data:image/" + picFormat + ";base64," + imgData
self.SendMsg(imgData)
except Exception as ex:
Log.Error(ex)
return
Expand Down
34 changes: 33 additions & 1 deletion src/qt/com/qtbubblelabel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os

from PySide2.QtCore import Qt, QPoint, QPropertyAnimation, QEasingCurve, QParallelAnimationGroup, QRectF, Property
from PySide2.QtGui import QPen, QPainterPath, QPainter, QColor
from PySide2.QtWidgets import QWidget, QVBoxLayout, QLabel, QApplication
from PySide2.QtWidgets import QWidget, QVBoxLayout, QLabel, QApplication, QFileDialog

from src.util import Log


class QtBubbleLabel(QWidget):
Expand Down Expand Up @@ -116,3 +120,31 @@ def ShowErrorEx(owner, text):
data.setText(text)
data.setStyleSheet("color:red")
data.show()

@staticmethod
def OpenPicture(self, path="."):
try:
filename = QFileDialog.getOpenFileName(self, "Open Image", path, "Image Files(*.jpg *.png)")
if filename and len(filename) > 1:
name = filename[0]
picFormat = filename[1]
baseName = os.path.basename(name)
if baseName[-3:] == "png":
picFormat = "png"
elif baseName[-3:] == "jpg":
picFormat = "jpeg"
elif baseName[-3:] == "gif":
picFormat = "gif"
else:
return

if os.path.isfile(name):
self.cachePath = os.path.dirname(name)

f = open(name, "rb")
data = f.read()
f.close()
return data, name, picFormat
except Exception as ex:
Log.Error(ex)
return None, None, None
6 changes: 5 additions & 1 deletion src/qt/user/qtlogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ def LoginBack(self, msg):
if msg == Status.Ok:
# self.close()
self.owner().stackedWidget.setCurrentIndex(1)
self.owner().qtTask.AddHttpTask(lambda x: User().UpdateUserInfo(x), self.UpdateUserBack)
self.InitUser()
self.owner().searchForm.InitKeyWord()
self.owner().indexForm.Init()
else:
# QtWidgets.QMessageBox.information(self, '登陆失败', msg, QtWidgets.QMessageBox.Yes)
self.owner().msgForm.ShowError("登陆失败, " + msg)

def InitUser(self):
self.owner().qtTask.AddHttpTask(lambda x: User().UpdateUserInfo(x), self.UpdateUserBack)
return

def UpdateUserBack(self, msg):
self.owner().userForm.UpdateLabel(User().name, User().level, User().exp, User().isPunched)
if not User().avatar:
Expand Down
20 changes: 18 additions & 2 deletions src/qt/user/qtuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from PySide2.QtWidgets import QListWidgetItem

from resources import resources
from src.qt.com.qtbubblelabel import QtBubbleLabel
from src.qt.com.qtimg import QtImgMgr
from src.server import Server, req
from src.user.user import User
from src.util.status import Status
from ui.user import Ui_User
Expand Down Expand Up @@ -90,11 +92,25 @@ def UpdateLabel(self, name, level, exp, sign):
self.signButton.setText("签到")
self.update()

def UpdatePictureData(self):
data, name, picFormat = QtBubbleLabel.OpenPicture(self)
if not data:
return
self.icon.setPixmap(None)
self.icon.setText("头像上传中......")
self.owner().qtTask.AddHttpTask(lambda x: Server().Send(req.SetAvatarInfoReq(data, picFormat), bakParam=x), self.UpdatePictureDataBack)
return

def UpdatePictureDataBack(self, msg):
if msg == Status.Ok:
self.owner().loginForm.InitUser()
else:
QtBubbleLabel().ShowMsg(msg)

def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress:
if event.button() == Qt.LeftButton:
if self.pictureData:
QtImgMgr().ShowImg(self.pictureData)
self.UpdatePictureData()
return True
else:
return False
Expand Down
25 changes: 25 additions & 0 deletions src/server/req.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64

from src.util import ToolUtil
from conf import config
from urllib.parse import quote
Expand Down Expand Up @@ -69,6 +71,27 @@ def __init__(self):
{}, method)


class SetAvatarInfoReq(ServerReq):
def __init__(self, data, picFormat="png"):
url = config.Url + "users/avatar"
method = "PUT"
if picFormat[-3:] == "png":
picFormat = "png"
else:
picFormat = "jpeg"

imgData = base64.b64encode(data).decode("utf-8")
imgData = "data:image/" + picFormat + ";base64," + imgData
super(self.__class__, self).__init__(url, ToolUtil.GetHeader(url, method), {"avatar": imgData}, method)


class SetTitleReq(ServerReq):
def __init__(self, userId, title):
url = config.Url + "users/{}/title".format(userId)
method = "PUT"
super(self.__class__, self).__init__(url, ToolUtil.GetHeader(url, method), {"title": title}, method)


# 签到
class PunchIn(ServerReq):
def __init__(self):
Expand Down Expand Up @@ -275,3 +298,5 @@ def __init__(self):
url = config.Url + "comics/random"
method = "GET"
super(self.__class__, self).__init__(url, ToolUtil.GetHeader(url, method), {}, method)


17 changes: 15 additions & 2 deletions src/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __DealHeaders(self, request, token):
else:
request.url = request.url.replace(host, self.imageServer)

if request.method.lower() == "post":
if request.method.lower() in ["post", "put"]:
request.headers["Content-Type"] = "application/json; charset=UTF-8"

def Send(self, request, token="", bakParam="", isASync=True):
Expand All @@ -113,6 +113,8 @@ def _Send(self, task):
self.Post(task)
elif task.req.method.lower() == "get":
self.Get(task)
elif task.req.method.lower() == "put":
self.Put(task)
else:
return
except Exception as es:
Expand All @@ -133,11 +135,22 @@ def Post(self, task):
if request.headers == None:
request.headers = {}


r = self.session.post(request.url, proxies=request.proxy, headers=request.headers, data=json.dumps(request.params), timeout=task.timeout, verify=False)
task.res = res.BaseRes(r, request.isParseRes)
return task

def Put(self, task):
request = task.req
if request.params == None:
request.params = {}

if request.headers == None:
request.headers = {}

r = self.session.put(request.url, proxies=request.proxy, headers=request.headers, data=json.dumps(request.params), timeout=60, verify=False)
task.res = res.BaseRes(r, request.isParseRes)
return task

def Get(self, task):
request = task.req
if request.params == None:
Expand Down
22 changes: 21 additions & 1 deletion src/server/user_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def __call__(self, backData):
QtTask().taskBack.emit(backData.bakParam, st)


@handler(req.SetAvatarInfoReq)
class SetAvatarInfoReq(object):
def __call__(self, backData):
st = Status.Ok
if backData.res.code != 200:
st = Status.SetHeadError + backData.res.message
if backData.bakParam:
QtTask().taskBack.emit(backData.bakParam, st)


@handler(req.SetTitleReq)
class SetTitleReq(object):
def __call__(self, backData):
st = Status.Ok
if backData.res.code != 200:
st = Status.SetHeadError + backData.res.message
if backData.bakParam:
QtTask().taskBack.emit(backData.bakParam, st)


@handler(req.PunchIn)
class PunchIn(object):
def __call__(self, backData):
Expand Down Expand Up @@ -298,4 +318,4 @@ def __call__(self, backData):
class GetCollectionsReq(object):
def __call__(self, backData):
if backData.bakParam:
QtTask().taskBack.emit(backData.bakParam, backData.res.raw.text)
QtTask().taskBack.emit(backData.bakParam, backData.res.raw.text)
1 change: 1 addition & 0 deletions src/util/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class Status(object):
RegisterError = "注册失败"
NotFoundBook = "未找到书籍"
UnderReviewBook = "本子审核中"
SetHeadError = "头像设置出错了,"

0 comments on commit e9fa777

Please sign in to comment.