diff --git a/app/api/endpoints/user.py b/app/api/endpoints/user.py index abdf8a3..1ac799f 100644 --- a/app/api/endpoints/user.py +++ b/app/api/endpoints/user.py @@ -20,6 +20,7 @@ from datetime import datetime, timedelta from sqlalchemy import text from app.models.community import CommunityDB from app.models.user_auth import UserAuthDB, UserAuthCreate, UserAuthInfo +from app.core.qcloud import qcloud_manager router = APIRouter() @@ -516,11 +517,24 @@ async def create_user_auth( return error_response(code=400, message="该用户已有认证记录") try: + # 调用实名认证 + verify_result = await qcloud_manager.verify_id_card( + id_card=auth.id_number, + name=auth.name + ) + + # 验证不通过 + if verify_result["Result"] != "0": # 0 表示一致,其他值表示不一致 + return error_response( + code=400, + message=f"实名认证失败: {verify_result['Description']}" + ) + # 创建认证记录 auth_record = UserAuthDB( user_id=current_user.userid, name=auth.name, - id_number=auth.id_number, + id_number=auth.id_number ) db.add(auth_record) diff --git a/app/core/config.py b/app/core/config.py index 44328bd..9d360d6 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -38,6 +38,7 @@ class Settings(BaseSettings): # 腾讯云短信配置 TENCENT_SECRET_ID: str = "AKIDxnbGj281iHtKallqqzvlV5YxBCrPltnS" TENCENT_SECRET_KEY: str = "ta6PXTMBsX7dzA7IN6uYUFn8F9uTovoU" + TENCENT_REGION: str = "ap-guangzhou" # 接口地域 SMS_SDK_APP_ID: str = "1400961527" SMS_SIGN_NAME: str = "蜂快到家公众号" SMS_TEMPLATE_ID: str = "2353143" # 验证码短信模板ID diff --git a/app/core/qcloud.py b/app/core/qcloud.py new file mode 100644 index 0000000..2bd383d --- /dev/null +++ b/app/core/qcloud.py @@ -0,0 +1,77 @@ +from tencentcloud.common import credential +from tencentcloud.common.profile.client_profile import ClientProfile +from tencentcloud.common.profile.http_profile import HttpProfile +from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException +from tencentcloud.faceid.v20180301 import faceid_client, models +from app.core.config import settings +import json + +class QCloudManager: + """腾讯云服务管理类""" + + def __init__(self): + """初始化认证信息""" + self.cred = credential.Credential( + settings.TENCENT_SECRET_ID, + settings.TENCENT_SECRET_KEY + ) + + # 配置 HTTP + self.http_profile = HttpProfile() + self.http_profile.endpoint = "faceid.tencentcloudapi.com" + + # 配置 Client + self.client_profile = ClientProfile() + self.client_profile.httpProfile = self.http_profile + + # 初始化人脸识别客户端 + self.faceid_client = faceid_client.FaceidClient( + self.cred, + settings.TENCENT_REGION, + self.client_profile + ) + + async def verify_id_card(self, id_card: str, name: str) -> dict: + """ + 身份证实名认证 + + Args: + id_card: 身份证号 + name: 姓名 + + Returns: + dict: { + "Result": "0"/"1"/"2", # 0:一致 1/2:不一致 + "Description": str, # 结果描述 + "RequestId": str # 请求ID + } + + Raises: + TencentCloudSDKException: 调用API失败 + """ + try: + # 构建请求 + req = models.IdCardVerificationRequest() + params = { + "IdCard": id_card, + "Name": name + } + req.from_json_string(json.dumps(params)) + + # 发送请求 + response = self.faceid_client.IdCardVerification(req) + + # 解析结果 + result = json.loads(response.to_json_string()) + + return { + "Result": result.get("Result", "0"), + "Description": result.get("Description", "验证失败"), + "RequestId": result.get("RequestId", "") + } + + except TencentCloudSDKException as e: + raise Exception(f"身份证实名认证失败: {str(e)}") + +# 创建全局实例 +qcloud_manager = QCloudManager() \ No newline at end of file