107 lines
2.8 KiB
Python
107 lines
2.8 KiB
Python
|
|
#!/opt/homebrew/bin/python3
|
||
|
|
# -*- coding:utf-8 -*-
|
||
|
|
import json
|
||
|
|
import uuid
|
||
|
|
from PyQt6 import *
|
||
|
|
from PyQt6.QtCore import *
|
||
|
|
from logs import log
|
||
|
|
from typing import Union
|
||
|
|
from models import User, Session
|
||
|
|
|
||
|
|
class UserManager(QObject):
|
||
|
|
loginSignal = pyqtSignal()
|
||
|
|
logoutSignal = pyqtSignal()
|
||
|
|
def __init__(self, parent=None):
|
||
|
|
super().__init__()
|
||
|
|
self.currentUser = {}
|
||
|
|
|
||
|
|
@pyqtSlot(str,str,int, result=str)
|
||
|
|
def create(self, name, password, role):
|
||
|
|
data = {
|
||
|
|
"id": str(uuid.uuid4()),
|
||
|
|
"name": name,
|
||
|
|
"password": password,
|
||
|
|
"role": role,
|
||
|
|
"valid": 0,
|
||
|
|
"role_name": self.roleName(role)
|
||
|
|
}
|
||
|
|
ok, res = Session.addByClass(User, data)
|
||
|
|
if ok:
|
||
|
|
return str(res["id"])
|
||
|
|
return ""
|
||
|
|
|
||
|
|
@pyqtSlot(result=QVariant)
|
||
|
|
def getCurrentUser(self):
|
||
|
|
return self.currentUser
|
||
|
|
|
||
|
|
@pyqtSlot(str, QVariant, result=bool)
|
||
|
|
def update(self, id, param=None):
|
||
|
|
if not isinstance(param, dict):
|
||
|
|
param = param.toVariant()
|
||
|
|
data = {}
|
||
|
|
if "name" in param:
|
||
|
|
data["name"] = param["name"]
|
||
|
|
if "password" in param:
|
||
|
|
data["password"] = param["password"]
|
||
|
|
if "role" in param:
|
||
|
|
data["role"] = param["role"]
|
||
|
|
data["role_name"] = self.roleName(param["role"])
|
||
|
|
ok,res = Session.updateById(User, id, data)
|
||
|
|
return ok
|
||
|
|
|
||
|
|
def roleName(self, role):
|
||
|
|
if role == 0:
|
||
|
|
return "管理员"
|
||
|
|
elif role == 1:
|
||
|
|
return "用户"
|
||
|
|
elif role == 2:
|
||
|
|
return "定制"
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=bool)
|
||
|
|
@pyqtSlot(list, result=bool)
|
||
|
|
def delete(self, id):
|
||
|
|
if isinstance(id, str):
|
||
|
|
ok,res = Session.deleteById(User, id)
|
||
|
|
return ok
|
||
|
|
else:
|
||
|
|
ok,res = Session.deleteByIds(User, id)
|
||
|
|
return ok
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=QVariant)
|
||
|
|
@pyqtSlot(str, result=list)
|
||
|
|
def getInfo(self, id = 'all'):
|
||
|
|
if id == 'all':
|
||
|
|
ok,res = Session.queryByAll(User)
|
||
|
|
if ok:
|
||
|
|
return res
|
||
|
|
else:
|
||
|
|
return []
|
||
|
|
else:
|
||
|
|
ok,res = Session.queryById(User, id)
|
||
|
|
if ok:
|
||
|
|
return res
|
||
|
|
else:
|
||
|
|
return None
|
||
|
|
|
||
|
|
# 用户验证
|
||
|
|
@pyqtSlot(str,str, result=bool)
|
||
|
|
def login(self, name, password):
|
||
|
|
ok, data = Session.queryByName(User, name)
|
||
|
|
if ok:
|
||
|
|
if data['password'] == password:
|
||
|
|
self.currentUser = data
|
||
|
|
self.loginSignal.emit()
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
return False
|
||
|
|
return False
|
||
|
|
|
||
|
|
@pyqtSlot()
|
||
|
|
def logOut(self):
|
||
|
|
self.logoutSignal.emit()
|
||
|
|
self.currentUser = {}
|
||
|
|
|
||
|
|
userManager = UserManager()
|
||
|
|
|
||
|
|
|