from fastapi import APIRouter, HTTPException from fastapi.responses import JSONResponse from .hook import * from .pydanticModel import * from .dao import tableStore router = APIRouter() #afdian的回调是post @router.get('/',response_model=AfdianResp) @router.post('/',response_model=AfdianResp) async def update_balance(afdianHookjson: AfdianHookJson): resp = {'ec': 200} # 检查 ec 是否为 200 if afdianHookjson.ec != 200: return {'ec': "afdian hook错误,ec 不是 200"} # 提取订单详情和可选参数 order_details = afdianHookjson.data.order custom_order_id = order_details.custom_order_id total_amount = order_details.total_amount table_store = tableStore(ots_client=router.ots_client, table_name=router.table_name) if all([custom_order_id is not None, total_amount is not None]): # 说明是余额类型 try: # 先给mysql的数据库添加 - 这个毕竟久 稳定 但是不应该影响其他数据库添加(万一不用了)-------------------- updateBalance(email=custom_order_id, amount=total_amount) # 先给mysql的数据库添加 - 这个毕竟久 稳定 但是不应该影响其他数据库添加(万一不用了)-------------------- except Exception as e: print("updateBalance v2b digitalocean mysql failed",e) try: #应该改成从原来的余额基础上加total_amount的值 table_store.getUserInfo(email=custom_order_id) cur_balance = table_store.balance balance_new = cur_balance+ total_amount # 更新余额列 update_balance_result = table_store.updateColumnByPrimaryKey( key=router.key, key_value=custom_order_id, update_column='balance', update_column_value=balance_new ) if update_balance_result: return resp #全部成功运行则返回爱发电要求的ec =200 else: return {'ec': "updateBalance tablestore 结果失败"} except Exception as e: print(e) return {'ec': "尝试 updateBalance tablestore 失败"} else: #这个直接返回200吧,反正测试接口的时候需要 ,平时也不用到 # return {'ec': "afdian hook custom_order_id 或者 total_amount 为 None"} return resp # test # @router.get('/',response_model=AfdianResp) # async def update_balance(afdianHookjson: AfdianHookJson): # print("收到请求1") # return {'ec':200}