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