2021-05-16 02:03:33 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-06-15 01:38:33 +08:00
|
|
|
import hashlib
|
2021-05-17 21:58:57 +08:00
|
|
|
from .restful import check_res, session_get, session_post, session_delete, session_put
|
2021-05-16 02:03:33 +08:00
|
|
|
|
|
|
|
from data import SessionModel
|
|
|
|
|
|
|
|
|
2021-06-15 01:38:33 +08:00
|
|
|
def calPassword(pwd: str):
|
|
|
|
sha = hashlib.sha256()
|
|
|
|
sha.update(bytes(pwd, encoding='utf8'))
|
|
|
|
return sha.hexdigest()
|
|
|
|
|
|
|
|
|
2021-05-16 20:10:12 +08:00
|
|
|
class UserService:
|
|
|
|
@staticmethod
|
2021-06-15 01:38:33 +08:00
|
|
|
def query_users(session: SessionModel, userid: int = None, name: str = None):
|
2021-05-23 02:04:29 +08:00
|
|
|
data = {}
|
|
|
|
if userid:
|
|
|
|
data["userid"] = userid
|
|
|
|
if name:
|
|
|
|
data["name"] = name
|
|
|
|
r = session_get(session, "user", "user", params=data) if data else session_get(session, "user", "user")
|
2021-05-16 20:10:12 +08:00
|
|
|
check_res(r)
|
2021-05-25 02:01:50 +08:00
|
|
|
data = r["data"]
|
|
|
|
return data if data is not None else []
|
2021-05-16 20:10:12 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_user(session: SessionModel, name: str, password: str):
|
2021-06-17 23:32:27 +08:00
|
|
|
r = session_post(session, "user", "user", json={"user": name, "password": calPassword(password)})
|
2021-05-16 20:10:12 +08:00
|
|
|
check_res(r)
|
|
|
|
return r
|
2021-05-17 02:11:11 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2021-05-17 21:58:57 +08:00
|
|
|
def remove_user(session: SessionModel, userid):
|
2021-05-17 02:11:11 +08:00
|
|
|
r = session_delete(session, "user", "user", json={"userid": userid})
|
|
|
|
check_res(r)
|
|
|
|
return r
|
2021-05-17 21:58:57 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reset_password(session: SessionModel, userid):
|
|
|
|
r = session_put(session, "user", "reset_password", json={"userid": userid})
|
|
|
|
check_res(r)
|
|
|
|
return r
|
2021-06-15 01:38:33 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def change_password(session: SessionModel, old_pwd, new_pwd, confirm_pwd):
|
|
|
|
r = session_put(
|
|
|
|
session,
|
|
|
|
"user",
|
|
|
|
"change_password",
|
|
|
|
json=dict(old=calPassword(old_pwd), new=calPassword(new_pwd), confirm=calPassword(confirm_pwd))
|
|
|
|
)
|
|
|
|
check_res(r)
|
|
|
|
return r
|