fix: 登录和续期请求统一转成中文错误
This commit is contained in:
@@ -36,6 +36,7 @@ from app.platform.sign import build_sign, wfs_sig
|
||||
|
||||
_EXECUTION = re.compile(r'name="execution"\s+value="([^"]+)"', re.I)
|
||||
_PUBLIC_KEY = re.compile(r'setPublicKey\("([^"]+)"\)')
|
||||
_GENERIC_LOGIN_FAILURE = "登录未成功,请检查验证码或短信验证码"
|
||||
POOL_CONNECTIONS = 300
|
||||
|
||||
|
||||
@@ -96,12 +97,15 @@ def sms_notice(payload: dict) -> str:
|
||||
return "短信发送已受理"
|
||||
|
||||
|
||||
def _transport_error(exc: Exception) -> None:
|
||||
if isinstance(exc, httpx.TimeoutException):
|
||||
raise NqiError("平台响应超时,请稍后重试") from exc
|
||||
if isinstance(exc, httpx.RequestError):
|
||||
raise NqiError("连接失败,请稍后重试") from exc
|
||||
raise exc
|
||||
def needs_captcha(message: str) -> bool:
|
||||
"""失败原因是图形验证码。短信验证码错误不在这里。"""
|
||||
if not message or message == _GENERIC_LOGIN_FAILURE:
|
||||
return False
|
||||
text = message.replace("短信验证码", "")
|
||||
if "验证码" in text:
|
||||
return True
|
||||
lowered = text.lower()
|
||||
return "captcha" in lowered and "sms" not in lowered
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -180,20 +184,24 @@ class NqiClient:
|
||||
}
|
||||
|
||||
async def open_login(self) -> LoginChallenge:
|
||||
try:
|
||||
response = await self.client.get(CAS_LOGIN, params={"service": PORTAL_SERVICE})
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
response = await self._request(
|
||||
"GET",
|
||||
CAS_LOGIN,
|
||||
check_auth=False,
|
||||
params={"service": PORTAL_SERVICE},
|
||||
)
|
||||
html = response.text
|
||||
execution = _EXECUTION.search(html)
|
||||
public_key = _PUBLIC_KEY.search(html)
|
||||
if not execution or not public_key:
|
||||
raise NqiError("登录页没有返回加密参数,请稍后重试")
|
||||
page_url = login_page_url(str(response.url))
|
||||
try:
|
||||
captcha = await self.client.get(CAS_CAPTCHA, headers={"Referer": page_url})
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
captcha = await self._request(
|
||||
"GET",
|
||||
CAS_CAPTCHA,
|
||||
check_auth=False,
|
||||
headers={"Referer": page_url},
|
||||
)
|
||||
if captcha.status_code != 200 or not captcha.content:
|
||||
raise NqiError("验证码获取失败")
|
||||
return LoginChallenge(
|
||||
@@ -205,14 +213,13 @@ class NqiClient:
|
||||
)
|
||||
|
||||
async def refresh_captcha(self, challenge: LoginChallenge) -> bytes:
|
||||
try:
|
||||
response = await self.client.get(
|
||||
CAS_CAPTCHA,
|
||||
params={"id": "refresh"},
|
||||
headers={"Referer": challenge.page_url},
|
||||
)
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
response = await self._request(
|
||||
"GET",
|
||||
CAS_CAPTCHA,
|
||||
check_auth=False,
|
||||
params={"id": "refresh"},
|
||||
headers={"Referer": challenge.page_url},
|
||||
)
|
||||
if response.status_code != 200 or not response.content:
|
||||
raise NqiError("验证码刷新失败")
|
||||
challenge.captcha_png = response.content
|
||||
@@ -228,14 +235,13 @@ class NqiClient:
|
||||
"password": rsa_encrypt(challenge.public_key, password),
|
||||
"captcha": challenge.captcha,
|
||||
}
|
||||
try:
|
||||
response = await self.client.post(
|
||||
CAS_CONFIG,
|
||||
json=payload,
|
||||
headers=self._ajax_headers(challenge),
|
||||
)
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
response = await self._request(
|
||||
"POST",
|
||||
CAS_CONFIG,
|
||||
check_auth=False,
|
||||
json=payload,
|
||||
headers=self._ajax_headers(challenge),
|
||||
)
|
||||
data = _json_or_error(response, "登录校验失败")
|
||||
code = _code(data)
|
||||
if code not in {"0", "1"}:
|
||||
@@ -251,17 +257,14 @@ class NqiClient:
|
||||
"loginId": rsa_encrypt(challenge.public_key, challenge.username),
|
||||
"password": rsa_encrypt(challenge.public_key, challenge.password),
|
||||
}
|
||||
try:
|
||||
response = await self.client.post(
|
||||
CAS_SMS,
|
||||
json=payload,
|
||||
headers=self._ajax_headers(challenge),
|
||||
timeout=httpx.Timeout(120.0, connect=15.0),
|
||||
)
|
||||
except httpx.TimeoutException as exc:
|
||||
raise NqiError("短信发送超时,请稍后重试") from exc
|
||||
except httpx.RequestError as exc:
|
||||
_transport_error(exc)
|
||||
response = await self._request(
|
||||
"POST",
|
||||
CAS_SMS,
|
||||
check_auth=False,
|
||||
json=payload,
|
||||
headers=self._ajax_headers(challenge),
|
||||
timeout=httpx.Timeout(120.0, connect=15.0),
|
||||
)
|
||||
data = _json_or_error(response, "短信发送失败")
|
||||
return sms_notice(data)
|
||||
|
||||
@@ -279,24 +282,27 @@ class NqiClient:
|
||||
("_eventId", "submit"),
|
||||
("geolocation", ""),
|
||||
]
|
||||
try:
|
||||
response = await self.client.post(
|
||||
challenge.page_url,
|
||||
content=urlencode(form).encode("utf-8"),
|
||||
headers={
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Referer": challenge.page_url,
|
||||
"Origin": ORIGIN,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
)
|
||||
except httpx.TimeoutException as exc:
|
||||
raise NqiError("登录超时,请稍后重试") from exc
|
||||
except httpx.RequestError as exc:
|
||||
_transport_error(exc)
|
||||
response = await self._request(
|
||||
"POST",
|
||||
challenge.page_url,
|
||||
check_auth=False,
|
||||
content=urlencode(form).encode("utf-8"),
|
||||
headers={
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Referer": challenge.page_url,
|
||||
"Origin": ORIGIN,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
},
|
||||
)
|
||||
if not self.ticket():
|
||||
raise NqiError(_login_failure_message(response))
|
||||
message = _login_failure_message(response)
|
||||
found = _EXECUTION.search(response.text or "")
|
||||
if found:
|
||||
challenge.execution = found.group(1)
|
||||
if needs_captcha(message):
|
||||
challenge.config_ready = False
|
||||
raise NqiError(message)
|
||||
return await self.current_user()
|
||||
|
||||
async def _cas_user_payload(self) -> dict:
|
||||
@@ -304,10 +310,10 @@ class NqiClient:
|
||||
headers = {"Referer": f"{ORIGIN}/pro-portal/"}
|
||||
if ticket:
|
||||
headers["tgc"] = ticket
|
||||
response = await self.client.get(CAS_USER, headers=headers)
|
||||
response = await self._request("GET", CAS_USER, headers=headers)
|
||||
data = _json_or_error(response, "读取登录用户失败")
|
||||
if not data.get("loginId") and not data.get("userName"):
|
||||
raise NqiError("登录状态已失效")
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
return data
|
||||
|
||||
async def current_user(self) -> dict:
|
||||
@@ -315,11 +321,7 @@ class NqiClient:
|
||||
|
||||
async def touch_session(self) -> dict:
|
||||
"""访问门户和图层接口,滑动服务端的登录有效期。"""
|
||||
try:
|
||||
data = await self._cas_user_payload()
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
raise
|
||||
data = await self._cas_user_payload()
|
||||
await self._touch_password()
|
||||
await self._touch_online()
|
||||
role_id = str(data.get("roleId") or "")
|
||||
@@ -342,27 +344,26 @@ class NqiClient:
|
||||
ticket = self.ticket()
|
||||
if ticket:
|
||||
headers["tgc"] = ticket
|
||||
try:
|
||||
response = await self.client.post(
|
||||
CAS_PASSWORD_CHECK,
|
||||
json={},
|
||||
headers=headers,
|
||||
timeout=httpx.Timeout(20.0, connect=10.0),
|
||||
)
|
||||
except (httpx.TimeoutException, httpx.RequestError) as exc:
|
||||
_transport_error(exc)
|
||||
if response.status_code in {401, 403}:
|
||||
raise NqiError("登录已失效,请重新登录")
|
||||
await self._request(
|
||||
"POST",
|
||||
CAS_PASSWORD_CHECK,
|
||||
json={},
|
||||
headers=headers,
|
||||
timeout=httpx.Timeout(20.0, connect=10.0),
|
||||
)
|
||||
|
||||
async def _touch_online(self) -> None:
|
||||
try:
|
||||
await self.client.get(
|
||||
await self._request(
|
||||
"GET",
|
||||
PORTAL_ONLINE,
|
||||
headers={"Referer": f"{ORIGIN}/pro-portal/"},
|
||||
timeout=httpx.Timeout(20.0, connect=10.0),
|
||||
follow_redirects=False,
|
||||
)
|
||||
except (httpx.TimeoutException, httpx.RequestError):
|
||||
except NqiAuthError:
|
||||
raise
|
||||
except NqiError:
|
||||
return
|
||||
|
||||
async def layer_code(self, *, city_code: str, month: str, network: str, grid_size: str = "GRID20") -> dict:
|
||||
@@ -652,7 +653,7 @@ def _login_failure_message(response: httpx.Response) -> str:
|
||||
detail = re.sub(r"\s+", " ", detail).strip()
|
||||
if detail:
|
||||
return detail
|
||||
return "登录未成功,请检查验证码或短信验证码"
|
||||
return _GENERIC_LOGIN_FAILURE
|
||||
|
||||
|
||||
def _media_type(response: httpx.Response) -> str:
|
||||
|
||||
Reference in New Issue
Block a user