跳转至

login_utils

登录相关 Utils

对登录Api的进一步封装

Login

Login()

Bases: ABC

登录基类

ATTRIBUTE DESCRIPTION
credential

用户凭证

TYPE: Credential | None

Source code in qqmusic_api/login_utils.py
def __init__(self) -> None:
    self.credential: Credential | None = None

QRCodeLogin

QRCodeLogin()

Bases: Login

二维码登录基类

Source code in qqmusic_api/login_utils.py
def __init__(self) -> None:
    super().__init__()
    self._state: QrCodeLoginEvents | None = None
    self._qrcode_data: bytes | None = None

get_qrcode abstractmethod async

get_qrcode() -> bytes

获取二维码

RETURNS DESCRIPTION
bytes

二维码二进制数据(QQ:PNG格式,WX:JEPG格式)

Source code in qqmusic_api/login_utils.py
@abstractmethod
async def get_qrcode(self) -> bytes:
    """获取二维码

    Returns:
        二维码二进制数据(QQ:PNG格式,WX:JEPG格式)
    """

check_qrcode_state abstractmethod async

check_qrcode_state() -> tuple[QrCodeLoginEvents, Credential | None]

检测二维码状态

RETURNS DESCRIPTION
tuple[QrCodeLoginEvents, Credential | None]

二维码状态,扫码成功返回凭证

Source code in qqmusic_api/login_utils.py
@abstractmethod
async def check_qrcode_state(self) -> tuple[QrCodeLoginEvents, Credential | None]:
    """检测二维码状态

    Returns:
        二维码状态,扫码成功返回凭证
    """

QQLogin

QQLogin()

Bases: QRCodeLogin

QQ 登录

Source code in qqmusic_api/login_utils.py
def __init__(self) -> None:
    super().__init__()
    self._qrsig = ""

get_qrcode async

get_qrcode() -> bytes

获取二维码

RETURNS DESCRIPTION
bytes

二维码二进制数据(QQ:PNG格式,WX:JEPG格式)

Source code in qqmusic_api/login_utils.py
@override
async def get_qrcode(self) -> bytes:
    if self._qrcode_data and self._state in (QrCodeLoginEvents.CONF, QrCodeLoginEvents.SCAN):
        return self._qrcode_data
    self._qrsig, self._qrcode_data = await QQLoginApi.get_qrcode()
    return self._qrcode_data

check_qrcode_state async

check_qrcode_state() -> tuple[QrCodeLoginEvents, Credential | None]

检测二维码状态

RETURNS DESCRIPTION
tuple[QrCodeLoginEvents, Credential | None]

二维码状态,扫码成功返回凭证

Source code in qqmusic_api/login_utils.py
@override
async def check_qrcode_state(self) -> tuple[QrCodeLoginEvents, Credential | None]:
    if self._state == QrCodeLoginEvents.DONE and self.credential:
        return self._state, self.credential
    if not self._qrsig:
        raise LoginError("[QQLogin] 请先获取二维码")
    self._state, self.credential = await QQLoginApi.check_qrcode_state(self._qrsig)
    return self._state, self.credential

WXLogin

WXLogin()

Bases: QRCodeLogin

微信 登录

Source code in qqmusic_api/login_utils.py
def __init__(self) -> None:
    super().__init__()
    self._uuid = ""
    self._code = ""

get_qrcode async

get_qrcode() -> bytes

获取二维码

RETURNS DESCRIPTION
bytes

二维码二进制数据(QQ:PNG格式,WX:JEPG格式)

Source code in qqmusic_api/login_utils.py
@override
async def get_qrcode(self) -> bytes:
    if self._qrcode_data and self._state in (QrCodeLoginEvents.CONF, QrCodeLoginEvents.SCAN):
        return self._qrcode_data
    self._uuid, self._qrcode_data = await WXLoginApi.get_qrcode()
    return self._qrcode_data

check_qrcode_state async

check_qrcode_state() -> tuple[QrCodeLoginEvents, Credential | None]

检测二维码状态

RETURNS DESCRIPTION
tuple[QrCodeLoginEvents, Credential | None]

二维码状态,扫码成功返回凭证

Source code in qqmusic_api/login_utils.py
@override
async def check_qrcode_state(self) -> tuple[QrCodeLoginEvents, Credential | None]:
    if self._state == QrCodeLoginEvents.DONE and self.credential:
        return self._state, self.credential
    if not self._uuid:
        raise LoginError("[WXLogin] 请先获取二维码")
    self._state, self.credential = await WXLoginApi.check_qrcode_state(self._uuid)
    return self._state, self.credential

PhoneLogin

PhoneLogin(phone: int, area_code: int = 86)

Bases: Login

手机号登录

ATTRIBUTE DESCRIPTION
phone

手机号

area_code

国家码

auth_url

验证链接

error_msg

错误信息

Source code in qqmusic_api/login_utils.py
def __init__(self, phone: int, area_code: int = 86) -> None:
    super().__init__()
    self.phone = phone
    self.area_code = area_code
    self.auth_url = ""
    self._state: PhoneLoginEvents | None = None
    self.error_msg = ""

send_authcode async

send_authcode() -> PhoneLoginEvents

发送验证码

Source code in qqmusic_api/login_utils.py
async def send_authcode(self) -> PhoneLoginEvents:
    """发送验证码"""
    self._state, data = await PhoneLoginApi.send_authcode(self.phone, self.area_code)
    if self._state == PhoneLoginEvents.CAPTCHA:
        self.auth_url = data
    elif self._state == PhoneLoginEvents.OTHER:
        self.error_msg = data
    return self._state

authorize async

authorize(auth_code: int) -> Credential

验证码鉴权

PARAMETER DESCRIPTION
auth_code

验证码

TYPE: int

Source code in qqmusic_api/login_utils.py
async def authorize(self, auth_code: int) -> Credential:
    """验证码鉴权

    Args:
        auth_code: 验证码
    """
    if self.credential:
        return self.credential
    self.credential = await PhoneLoginApi.authorize(self.phone, auth_code, self.area_code)
    return self.credential