1
This commit is contained in:
@@ -57,87 +57,56 @@ class DailyFileHandler(logging.FileHandler):
|
|||||||
self._file_handler = logging.FileHandler(
|
self._file_handler = logging.FileHandler(
|
||||||
self.baseFilename, mode="a", encoding=self.encoding
|
self.baseFilename, mode="a", encoding=self.encoding
|
||||||
)
|
)
|
||||||
if self._file_handler:
|
self._file_handler.setFormatter(self.formatter)
|
||||||
self._file_handler.emit(record)
|
self._current_date = date_str
|
||||||
else:
|
self.stream = self._file_handler.stream
|
||||||
super().emit(record)
|
super().emit(record)
|
||||||
|
|
||||||
|
|
||||||
# Add daily file handler
|
_handler = DailyFileHandler(_log_dir)
|
||||||
_log_handler = DailyFileHandler(_log_dir)
|
_handler.setFormatter(logging.Formatter(
|
||||||
_log_formatter = logging.Formatter(
|
"[%(asctime)s] %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
|
||||||
"[%(asctime)s] %(levelname)s %(message)s",
|
))
|
||||||
datefmt="%Y-%m-%d %H:%M:%S"
|
if not logger.handlers:
|
||||||
)
|
logger.addHandler(_handler)
|
||||||
_log_handler.setFormatter(_log_formatter)
|
|
||||||
logger.addHandler(_log_handler)
|
|
||||||
|
|
||||||
# Monkey patch to fix alipay-sdk-python bytes/str issue
|
|
||||||
def _patch_alipay_sdk():
|
|
||||||
try:
|
|
||||||
from alipay.aop.api.util import WebUtils
|
|
||||||
|
|
||||||
# 直接用我们自己的安全实现替换 do_post
|
|
||||||
def safe_do_post(url, query_string, headers, params, charset, timeout):
|
|
||||||
import http.client
|
|
||||||
import socket
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
parse_result = urlparse(url)
|
|
||||||
if parse_result.scheme == 'https':
|
|
||||||
conn = http.client.HTTPSConnection(
|
|
||||||
parse_result.hostname,
|
|
||||||
parse_result.port or 443,
|
|
||||||
timeout=timeout
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
conn = http.client.HTTPConnection(
|
|
||||||
parse_result.hostname,
|
|
||||||
parse_result.port or 80,
|
|
||||||
timeout=timeout
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
body = query_string.encode(charset) if params is None else params
|
|
||||||
conn.request(
|
|
||||||
'POST',
|
|
||||||
parse_result.path + ('?' + parse_result.query if parse_result.query else ''),
|
|
||||||
body,
|
|
||||||
headers
|
|
||||||
)
|
|
||||||
response = conn.getresponse()
|
|
||||||
if response.status == 200:
|
|
||||||
response_body = response.read()
|
|
||||||
# 确保返回的是 str,而不是 bytes
|
|
||||||
if isinstance(response_body, bytes):
|
|
||||||
response_body = response_body.decode(charset)
|
|
||||||
return response_body
|
|
||||||
else:
|
|
||||||
response_body = response.read()
|
|
||||||
if isinstance(response_body, bytes):
|
|
||||||
response_body = response_body.decode(charset)
|
|
||||||
raise Exception(f"invalid http status {response.status}, detail body: {response_body}")
|
|
||||||
except socket.timeout:
|
|
||||||
raise Exception("timeout")
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
# Apply patch - 直接替换,避免原函数的问题
|
|
||||||
WebUtils.do_post = safe_do_post
|
|
||||||
logger.info("Successfully replaced alipay-sdk-python WebUtils.do_post with safe implementation")
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Failed to patch alipay-sdk-python: {e}")
|
|
||||||
|
|
||||||
# Apply the patch when module is loaded
|
|
||||||
_patch_alipay_sdk()
|
|
||||||
|
|
||||||
# Orders pending payment for longer than this are auto-cancelled
|
# Orders pending payment for longer than this are auto-cancelled
|
||||||
ORDER_EXPIRE_MINUTES = 5
|
ORDER_EXPIRE_MINUTES = 5
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Monkey-patch alipay-sdk-python WebUtils.do_post to fix bytes concatenation bug
|
||||||
|
# The SDK's error handling does: '...' + response.read()
|
||||||
|
# but response.read() returns bytes, causing TypeError on Python 3
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
def _patch_alipay_webutils():
|
||||||
|
try:
|
||||||
|
from alipay.aop.api.util import WebUtils
|
||||||
|
_original_do_post = WebUtils.do_post
|
||||||
|
|
||||||
|
def _patched_do_post(url, query_string, headers, params, charset, timeout=30):
|
||||||
|
try:
|
||||||
|
return _original_do_post(url, query_string, headers, params, charset, timeout)
|
||||||
|
except TypeError as e:
|
||||||
|
if "can only concatenate str (not 'bytes') to str" in str(e):
|
||||||
|
# Decode bytes response to string and retry
|
||||||
|
import http.client as _http
|
||||||
|
from urllib.parse import urlparse as _urlparse
|
||||||
|
parsed = _urlparse(url)
|
||||||
|
conn = _http.HTTPSConnection(parsed.hostname, context=__import__('ssl').create_default_context())
|
||||||
|
conn.request("POST", parsed.path + "?" + query_string, params, headers)
|
||||||
|
resp = conn.getresponse()
|
||||||
|
body = resp.read().decode("utf-8", errors="replace")
|
||||||
|
raise RuntimeError(f"Alipay API error (status {resp.status}): {body}") from e
|
||||||
|
raise
|
||||||
|
|
||||||
|
WebUtils.do_post = _patched_do_post
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
_patch_alipay_webutils()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Config helpers – read from system_configs table (admin panel)
|
# Config helpers – read from system_configs table (admin panel)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user