| 109 | |
| 110 | |
| 111 | class KuroApiResp(BaseModel, Generic[T]): |
| 112 | model_config = ConfigDict(extra="ignore") |
| 113 | |
| 114 | code: int = Field(0, description="状态码") |
| 115 | msg: str = Field("", description="消息") |
| 116 | data: Optional[T] = Field(None, description="数据") |
| 117 | |
| 118 | @computed_field |
| 119 | @property |
| 120 | def success(self) -> bool: |
| 121 | return self.code in (RespCode.OK_ZERO, RespCode.OK_HTTP) |
| 122 | |
| 123 | @classmethod |
| 124 | def ok( |
| 125 | cls, |
| 126 | data: Optional[T] = None, |
| 127 | msg: str = "请求成功", |
| 128 | code: int = RespCode.OK_ZERO, |
| 129 | ) -> "KuroApiResp[T]": |
| 130 | return cls(code=code, msg=msg, data=data) |
| 131 | |
| 132 | @classmethod |
| 133 | def err(cls, msg: str, code: int = RespCode.BAD_REQUEST) -> "KuroApiResp[T]": |
| 134 | return cls(code=code, msg=msg, data=None) |
| 135 | |
| 136 | @property |
| 137 | def is_token_invalid(self) -> bool: |
| 138 | if self.code == RespCode.TOKEN_INVALID.value: |
| 139 | return True |
| 140 | return self.msg in ("重新登录", "登录已过期", "登录已过期,请重新登录") |
| 141 | |
| 142 | @property |
| 143 | def is_bat_token_invalid(self) -> bool: |
| 144 | if self.code == RespCode.BAT_TOKEN_INVALID.value: |
| 145 | return True |
| 146 | return self.msg in ("数据令牌已失效") |
| 147 | |
| 148 | @model_validator(mode="after") |
| 149 | def _post_validate(self) -> "KuroApiResp[T]": |
| 150 | if check_send_master_info(self.code, self.msg, self.data): |
| 151 | pass |
| 152 | return self |
| 153 | |
| 154 | async def mark_cookie_invalid(self, uid: str, cookie: str): |
| 155 | if not self.is_token_invalid: |
| 156 | return |
| 157 | from ...utils.database.models import WavesUser |
| 158 | |
| 159 | await WavesUser.mark_cookie_invalid(uid, cookie, "无效") |
| 160 | |
| 161 | def throw_msg(self) -> str: |
| 162 | if isinstance(self.msg, str): |
| 163 | return self.msg |
| 164 | return ThrowMsg.SYSTEM_BUSY |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected