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()