This commit is contained in:
2026-07-06 13:39:08 +08:00
parent 16b3d223c1
commit c68215c177
33 changed files with 3250 additions and 732 deletions
+123 -91
View File
@@ -5,8 +5,14 @@ from typing import Any, Dict, Optional, Tuple
import httpx
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from datetime import datetime, timezone, timedelta
from datetime import datetime, timedelta
# 【改动1:引入自定义北京时间工具类】
from app.utils.datetime_util import (
BEIJING_TZ,
datetime_to_db_tz_str,
db_tz_str_to_datetime
)
from app.models.user_oauth import UserOAuth
from app.models.user_oauth_app import UserOAuthApp
from app.models.base import async_session
@@ -40,50 +46,75 @@ class DouyinRequest:
self._client = None
async def _get_redis_token(self, oauth_id: str) -> Optional[str]:
"""
【核心改动:北京时间解析+脏缓存自动清理】
1. 缓存格式异常/字段缺失:直接删除脏缓存
2. Token过期则不返回,交由上层判断refresh是否过期决定是否删除缓存
"""
redis = get_redis()
if not redis:
raise ValueError("Redis连接未配置")
try:
cache_str = await redis.hget(self._redis_key, oauth_id)
if cache_str:
cache = json.loads(cache_str)
token = cache.get("token")
expired_at_str = cache.get("expired_at")
if token and expired_at_str:
expired_at = datetime.fromisoformat(expired_at_str).replace(tzinfo=timezone.utc)
if expired_at > datetime.now(timezone.utc):
return token
except Exception as e:
raise ValueError(f"获取Redis缓存失败: {e}")
if not cache_str:
return None
return None
cache = json.loads(cache_str)
token = cache.get("token")
expired_at_str = cache.get("expired_at")
# 脏缓存:关键字段缺失,直接删除
if not (token and expired_at_str):
await redis.hdel(self._redis_key, oauth_id)
logger.warning(f"oauth_id:{oauth_id} Redis缓存字段缺失,已清理脏数据")
return None
# 【改动3:使用工具类解析带时区时间,不再强制覆盖UTC】
expired_at = db_tz_str_to_datetime(expired_at_str)
now_beijing = datetime.now(tz=BEIJING_TZ)
if expired_at > now_beijing:
return token
# AccessToken已过期,返回None,上层会校验refresh_token状态决定是否清理缓存
return None
except json.JSONDecodeError:
# JSON格式损坏,清理脏缓存
await redis.hdel(self._redis_key, oauth_id)
logger.error(f"oauth_id:{oauth_id} Redis缓存JSON格式异常,已清理脏数据")
return None
except Exception as e:
raise ValueError(f"获取Redis缓存失败: {str(e)}")
async def _set_redis_token(self, oauth_id: str, token: str, expired_at: datetime):
redis = get_redis()
if not redis:
raise ValueError("Redis连接未配置")
# 【改动4:统一转为北京时间序列化存入Redis,和数据库时区对齐】
cache = {
"token": token,
"expired_at": datetime_to_db_tz_str(expired_at),
}
try:
cache = {
"token": token,
"expired_at": expired_at.isoformat(),
}
await redis.hset(self._redis_key, oauth_id, json.dumps(cache))
await redis.hset(self._redis_key, oauth_id, json.dumps(cache, ensure_ascii=False))
except Exception as e:
raise ValueError(f"设置Redis缓存失败: {e}")
raise ValueError(f"设置Redis缓存失败: {str(e)}")
async def _delete_redis_token(self, oauth_id: str):
"""删除单个oauth_id缓存(授权彻底失效时调用)"""
redis = get_redis()
if not redis:
raise ValueError("Redis连接未配置")
try:
await redis.hdel(self._redis_key, oauth_id)
logger.info(f"oauth_id:{oauth_id} 授权失效,已清理Redis缓存脏数据")
except Exception as e:
raise ValueError(f"删除Redis缓存失败: {e}")
raise ValueError(f"删除Redis缓存失败: {str(e)}")
async def get_access_token(self, oauth_id: str, force_refresh: bool = False) -> str:
# 优先读取缓存
if not force_refresh:
token = await self._get_redis_token(oauth_id)
if token:
@@ -101,7 +132,17 @@ class DouyinRequest:
if not oauth_data:
raise ValueError("无效的oauth_id")
# 【改动5:统一北京时间当前时间】
now_beijing = datetime.now(tz=BEIJING_TZ)
if force_refresh:
# 强制刷新:先检查refresh_token是否为空
if not oauth_data.refresh_token:
# RefreshToken为空 → 授权已失效,清理Redis脏缓存
await self._delete_redis_token(oauth_id)
raise ValueError("授权已过期,请重新授权登录")
# 清空同条件下所有账号缓存与数据库token
where_cond = UserOAuth.deleted_at.is_(None)
if oauth_data.appid:
where_cond = where_cond & (UserOAuth.appid == oauth_data.appid)
@@ -118,9 +159,7 @@ class DouyinRequest:
)
await db.commit()
related_oauth_ids = await db.execute(
select(UserOAuth.id).where(where_cond)
)
related_oauth_ids = await db.execute(select(UserOAuth.id).where(where_cond))
related_oauth_ids = [row[0] for row in related_oauth_ids.all()]
for related_id in related_oauth_ids:
await self._delete_redis_token(related_id)
@@ -128,12 +167,14 @@ class DouyinRequest:
new_token, new_expired_at = await self.refresh_access_token(db, oauth_id, oauth_data.appid, oauth_data.refresh_token)
return new_token
if oauth_data.access_token_expired and oauth_data.access_token_expired > datetime.now(timezone.utc):
# 数据库AccessToken未过期,写入缓存直接返回
if oauth_data.access_token_expired and oauth_data.access_token_expired > now_beijing:
token = oauth_data.access_token
expired_at = oauth_data.access_token_expired
await self._set_redis_token(oauth_id, token, expired_at)
return token
# 查找同账号下未过期有效token复用
where_cond = UserOAuth.deleted_at.is_(None)
if oauth_data.appid:
where_cond = where_cond & (UserOAuth.appid == oauth_data.appid)
@@ -146,7 +187,7 @@ class DouyinRequest:
select(UserOAuth.access_token, UserOAuth.access_token_expired).where(
where_cond,
UserOAuth.access_token_expired.is_not(None),
UserOAuth.access_token_expired > datetime.now(timezone.utc),
UserOAuth.access_token_expired > now_beijing,
).limit(1)
)
related_oauth = related_oauths.first()
@@ -155,9 +196,18 @@ class DouyinRequest:
await self._set_redis_token(oauth_id, token, expired_at)
return token
if oauth_data.refresh_token_expired and oauth_data.refresh_token_expired < datetime.now(timezone.utc):
raise ValueError("授权已过期,请重新授权")
# =========【核心业务规则实现:判断RefreshToken是否过期】=========
if not oauth_data.refresh_token:
# RefreshToken为空 → 授权已失效,清理Redis脏缓存
await self._delete_redis_token(oauth_id)
raise ValueError("授权已过期,请重新授权登录")
if oauth_data.refresh_token_expired and oauth_data.refresh_token_expired < now_beijing:
# RefreshToken过期 → 授权彻底失效,清理Redis脏缓存
await self._delete_redis_token(oauth_id)
raise ValueError("授权已过期,请重新授权登录")
# RefreshToken有效,执行刷新并更新缓存
new_token, new_expired_at = await self.refresh_access_token(db, oauth_id, oauth_data.appid, oauth_data.refresh_token)
return new_token
@@ -171,7 +221,7 @@ class DouyinRequest:
oauth_info = oauth_info.first()
if not oauth_info:
raise ValueError("无效的oauth_id")
account_username, account_userid = oauth_info
result = await db.execute(
@@ -203,6 +253,7 @@ class DouyinRequest:
code = data.get('code', 0)
if code != 0:
#刷新token
raise ValueError(f"刷新access_token失败,接口返回:{data}")
data = data.get('data', {})
@@ -211,7 +262,10 @@ class DouyinRequest:
expires_in = data.get('expires_in', 0)
refresh_token_expires_in = data.get('refresh_token_expires_in', 0)
new_expired_at = datetime.now(timezone.utc) + timedelta(seconds=expires_in)
# 【改动6:北京时间计算过期时间】
now_beijing = datetime.now(tz=BEIJING_TZ)
new_expired_at = now_beijing + timedelta(seconds=expires_in)
new_refresh_expired = now_beijing + timedelta(seconds=refresh_token_expires_in)
where_cond = UserOAuth.deleted_at.is_(None)
if appid:
@@ -222,20 +276,16 @@ class DouyinRequest:
where_cond = where_cond & (UserOAuth.account_userid == account_userid)
await db.execute(
update(UserOAuth).where(
where_cond,
).values(
update(UserOAuth).where(where_cond).values(
access_token=new_access_token,
refresh_token=new_refresh_token,
access_token_expired=new_expired_at,
refresh_token_expired=datetime.now(timezone.utc) + timedelta(seconds=refresh_token_expires_in),
refresh_token_expired=new_refresh_expired,
)
)
await db.commit()
related_oauth_ids = await db.execute(
select(UserOAuth.id).where(where_cond)
)
related_oauth_ids = await db.execute(select(UserOAuth.id).where(where_cond))
related_oauth_ids = [row[0] for row in related_oauth_ids.all()]
for related_id in related_oauth_ids:
@@ -243,92 +293,79 @@ class DouyinRequest:
return new_access_token, new_expired_at
# 有token请求
async def request_with_token_with_context(
self,
oauth_id: str,
url: str,
method: str = 'GET',
options: any = None,
options: Optional[Dict[str, Any]] = None,
request_count: int = 1,
) -> Any:
options = options or {}
token = await self.get_access_token(oauth_id)
resp_data: Optional[Dict[str, Any]] = None
for i in range(1, request_count+1):
# 有一些错误是触发频次管理的,需要重试
for i in range(1, request_count + 1):
try:
headers = options.get('headers', {}).copy()
headers['Access-Token'] = token
has_files = 'files' in options
if has_files:
# 移除可能错误设置的 Content-Type,让库自动生成 multipart 头
headers.pop('Content-Type', None)
else:
headers.setdefault('Content-Type', 'application/json')
options['headers'] = headers
response = await self.client.request(method, url, **options)
response.raise_for_status()
try:
data = response.json()
except json.JSONDecodeError:
data = {'code': 0, 'data': response.text, 'msg':'JSON解析失败'}
if 'code' not in data:
try:
resp_data = response.json()
except json.JSONDecodeError:
resp_data = {'code': 0, 'data': response.text, 'msg': 'JSON解析失败'}
if 'code' not in resp_data:
await asyncio.sleep(i * 5)
continue
code = data.get('code', 0)
code = resp_data.get('code', 0)
if code in [40102, 40104]:
await asyncio.sleep(i * 5)
token = await self.get_access_token(oauth_id, force_refresh=True)
continue
if code in [40100, 40110]:
wait_time = min(2 * (2 ** (i - 1)), 10)
await asyncio.sleep(wait_time)
continue
if code == 50000:
await asyncio.sleep(i * 10)
continue
return data
return resp_data
except httpx.HTTPStatusError as e:
await asyncio.sleep(i * 10)
continue
except httpx.RequestError as e:
except (httpx.HTTPStatusError, httpx.RequestError):
await asyncio.sleep(i * 10)
continue
# 重试耗尽,日志记录
options_log = {}
if options:
for key, value in options.items():
if key == 'files':
options_log[key] = {k: (v[0], 'bytes_content', v[2]) for k, v in value.items()}
else:
options_log[key] = value
res = json.dumps(data, ensure_ascii=False) if 'data' in locals() else ''
for key, value in options.items():
if key == 'files':
options_log[key] = {k: (v[0], 'bytes_content', v[2]) for k, v in value.items()}
else:
options_log[key] = value
res = json.dumps(resp_data, ensure_ascii=False) if resp_data else ''
logger.error(
f'DouYin API request failed after {request_count} retries. '
f'url:{url};method:{method};oauth_id:{oauth_id};options:{json.dumps(options_log, ensure_ascii=False)};response:{res}'
)
if 'data' in locals() and data.get('code', 0) != 0:
raise ValueError(f'接口返回错误[code:{data.get("code", "接口编码")}]{data.get("message", "接口返回错误")}')
else:
raise ValueError('网络错误,稍后重试。')
if resp_data and resp_data.get("code", 0) != 0:
raise ValueError(f'接口返回错误[code:{resp_data.get("code")}]{resp_data.get("message", "接口异常")}')
raise ValueError("网络请求失败,请稍后重试")
# 无token请求
async def request_with_context(
self,
url: str,
@@ -336,8 +373,9 @@ class DouyinRequest:
options: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
options = options or {}
resp_data: Optional[Dict[str, Any]] = None
for i in range(1, 2):
for i in range(1, 3):
try:
headers = options.get('headers', {}).copy()
headers.setdefault('Content-Type', 'application/json')
@@ -347,30 +385,24 @@ class DouyinRequest:
response.raise_for_status()
try:
data = response.json()
resp_data = response.json()
except json.JSONDecodeError:
data = {'code': 0, 'data': response.text}
resp_data = {'code': 0, 'data': response.text}
if 'code' not in data:
if 'code' not in resp_data:
await asyncio.sleep(i * 5)
continue
code = data.get('code', 0)
if code >= 50000:
if resp_data.get("code", 0) >= 50000:
await asyncio.sleep(i * 10)
continue
return resp_data
return data
except httpx.HTTPStatusError as e:
await asyncio.sleep(i * 10)
continue
except httpx.RequestError as e:
except (httpx.HTTPStatusError, httpx.RequestError):
await asyncio.sleep(i * 10)
continue
res = json.dumps(data) if 'data' in locals() else ''
res = json.dumps(resp_data, ensure_ascii=False) if resp_data else ''
raise RuntimeError(
f'DouYin API request failed after 5 retries. '
f'url:{url};options:{json.dumps(options)};response:{res}'
f'DouYin API request failed after 2 retries. '
f'url:{url};options:{json.dumps(options, ensure_ascii=False)};response:{res}'
)