Spaces:
Sleeping
Sleeping
update
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- app.py +28 -0
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import urllib.parse # 导入 urllib.parse 用于 URL 编码
|
|
| 11 |
from fastapi.staticfiles import StaticFiles # 导入 StaticFiles
|
| 12 |
from passlib.context import CryptContext # 导入 CryptContext
|
| 13 |
import httpx # 导入 httpx 用于异步 HTTP 请求
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI(max_upload_size=10 * 1024 * 1024) # 设置最大上传大小为 10MB
|
| 16 |
|
|
@@ -117,6 +118,7 @@ class EnrollmentIndividualRequest(BaseModel):
|
|
| 117 |
WECHAT_APP_ID = os.getenv("WECHAT_APP_ID", "YOUR_WECHAT_APP_ID")
|
| 118 |
WECHAT_APP_SECRET = os.getenv("WECHAT_APP_SECRET", "YOUR_WECHAT_APP_SECRET") # Add App Secret
|
| 119 |
WECHAT_REDIRECT_URI = os.getenv("WECHAT_REDIRECT_URI", "http://localhost:7861/api/auth/wechat/callback") # This should be your frontend callback URL
|
|
|
|
| 120 |
|
| 121 |
class Course(BaseModel):
|
| 122 |
course_id: Optional[int] = None
|
|
@@ -284,6 +286,32 @@ async def wechat_callback(code: str, state: str):
|
|
| 284 |
}
|
| 285 |
}
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
@api_router.post("/enrollment/individual")
|
| 288 |
async def enroll_individual(request: EnrollmentIndividualRequest):
|
| 289 |
enrollment_id = str(uuid.uuid4())
|
|
|
|
| 11 |
from fastapi.staticfiles import StaticFiles # 导入 StaticFiles
|
| 12 |
from passlib.context import CryptContext # 导入 CryptContext
|
| 13 |
import httpx # 导入 httpx 用于异步 HTTP 请求
|
| 14 |
+
import hashlib # 导入 hashlib 用于 SHA1 哈希
|
| 15 |
|
| 16 |
app = FastAPI(max_upload_size=10 * 1024 * 1024) # 设置最大上传大小为 10MB
|
| 17 |
|
|
|
|
| 118 |
WECHAT_APP_ID = os.getenv("WECHAT_APP_ID", "YOUR_WECHAT_APP_ID")
|
| 119 |
WECHAT_APP_SECRET = os.getenv("WECHAT_APP_SECRET", "YOUR_WECHAT_APP_SECRET") # Add App Secret
|
| 120 |
WECHAT_REDIRECT_URI = os.getenv("WECHAT_REDIRECT_URI", "http://localhost:7861/api/auth/wechat/callback") # This should be your frontend callback URL
|
| 121 |
+
WECHAT_TOKEN = os.getenv("WECHAT_TOKEN", "YOUR_WECHAT_VERIFICATION_TOKEN") # Add WeChat verification token
|
| 122 |
|
| 123 |
class Course(BaseModel):
|
| 124 |
course_id: Optional[int] = None
|
|
|
|
| 286 |
}
|
| 287 |
}
|
| 288 |
|
| 289 |
+
@api_router.get("/wechat/verify")
|
| 290 |
+
async def wechat_verify(signature: str, timestamp: str, nonce: str, echostr: str):
|
| 291 |
+
"""
|
| 292 |
+
微信服务器配置验证接口。
|
| 293 |
+
用于验证微信服务器的有效性。
|
| 294 |
+
"""
|
| 295 |
+
# 1. 将 token、timestamp、nonce 三个参数进行字典序排序
|
| 296 |
+
# 2. 将三个参数字符串拼接成一个字符串进行 sha1 加密
|
| 297 |
+
# 3. 获得加密后的字符串可与 signature 对比,标识该请求来源于微信
|
| 298 |
+
|
| 299 |
+
# Note: WECHAT_TOKEN should be the token you set in WeChat Official Account/Mini Program backend.
|
| 300 |
+
|
| 301 |
+
data = [WECHAT_TOKEN, timestamp, nonce]
|
| 302 |
+
data.sort()
|
| 303 |
+
temp_str = "".join(data)
|
| 304 |
+
|
| 305 |
+
sha1 = hashlib.sha1(temp_str.encode('utf-8')).hexdigest()
|
| 306 |
+
|
| 307 |
+
if sha1 == signature:
|
| 308 |
+
return echostr
|
| 309 |
+
else:
|
| 310 |
+
raise HTTPException(
|
| 311 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 312 |
+
detail="微信验证失败"
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
@api_router.post("/enrollment/individual")
|
| 316 |
async def enroll_individual(request: EnrollmentIndividualRequest):
|
| 317 |
enrollment_id = str(uuid.uuid4())
|