TG-PlatformPlus/user.py

62 lines
1.5 KiB
Python

#!/opt/homebrew/bin/python3
# -*- coding:utf-8 -*-
import json
from PyQt6.QtCore import *
from models import User, Session
from logs import log
class UserBackend(QObject):
def __init__(self):
super().__init__()
# 用户验证
def validate_user(self, json_str):
json_dict = json.loads(json_str)
name = json_dict['name']
password = json_dict['password']
ok, data = Session.queryByName(User, name)
if ok:
if data['password'] == password:
return True, data
else:
return False, None
return False, None
# 登录
def login(self, json_str):
try:
ok, user = self.validate_user(json_str)
if ok and user:
return True, user
else:
return False, None
except Exception as e:
log.error(e)
return False, None
# 添加用户
def add_user(self, json_str):
json_dict = json.loads(json_str)
return Session.addByClass(User, json_dict)
# 更新用户
def update_user(self, json_str):
json_dict = json.loads(json_str)
id = json_dict.get('id')
return Session.updateById(User, id, json_dict)
# 删除用户
def delete_user(self, id):
return Session.deleteById(User, id)
# 获取所有用户
def get_users(self):
return Session.queryByAll(User)
# 获取某个用户
def get_user(self, id):
return Session.queryById(User, id)
userBackend = UserBackend()