payHook / server /account_manager.py
rogerxavier's picture
Upload 20 files
ee37dcf verified
raw
history blame
3.33 kB
import time
from fastapi import HTTPException,APIRouter
from .pydanticModel import *
from .dao import tableStore
router = APIRouter()
# 定义路由和函数
@router.post("/login")
@router.get("/login")
async def login(login: signIn):
# ->"user info or throw error"
# 检查email和password是否匹配
table_store = tableStore(ots_client=router.ots_client,table_name=router.table_name)
try:
login_result = table_store.login(login.email, login.password)
if login_result['ec']==200:
return login_result
else:
raise HTTPException(status_code=400, detail="login failed in table store")
except Exception as e:
print(e)
raise HTTPException(status_code=400, detail="sign in failed")
@router.get("/signUp")
@router.post("/signUp")
async def sign_up(signUp: signUp):
# ->"user info or throw error"
table_store = tableStore(ots_client=router.ots_client,table_name=router.table_name)
try:
signUp_result = table_store.userSignUp(signUp.email,signUp.password)
if signUp_result['ec']==200:
return signUp_result
else:
raise HTTPException(status_code=400, detail="sign up failed in table store")
except Exception as e:
raise HTTPException(status_code=400, detail="sign up failed")
@router.get("/purchase")
@router.post("/purchase")
async def purchase(plan: userPlan):
#->bool or raise error
# 提取plan价格和时间
price = plan.price
time_to_add = plan.duration_seconds
email = plan.email
password = plan.password
table_store = tableStore(ots_client=router.ots_client, table_name=router.table_name)
# 读取存储数据,如果存的expiredAt>now,那么从expiredAt加时间
# 如果存的<now ,那么从now加时间 (总之是取两者较大+time_to_add)
table_store.getUserInfo(email=email)
cur_balance = table_store.balance
cur_expired_at = table_store.expired_at
cur_time = int(time.time())
user_password_stored = table_store.stored_password
if cur_balance -price <0:
raise HTTPException(status_code=400, detail="balance not enough")
elif password!=user_password_stored:
raise HTTPException(status_code=400, detail="auth not pass in purchase")
else:
expired_new = max(cur_expired_at, cur_time) + time_to_add
balance_new = cur_balance -price
# 更新余额和时间
update_balance_result = table_store.updateColumnByPrimaryKey(
key=router.key,
key_value=email,
update_column='balance',
update_column_value=balance_new
)
if not update_balance_result:
raise HTTPException(status_code=400, detail="updateBalance 结果失败")
update_expired_result = table_store.updateColumnByPrimaryKey(
key=router.key,
key_value=email,
update_column='expiredAt',
update_column_value=expired_new
)
if not update_expired_result:
raise HTTPException(status_code=400, detail="update_expired_result 结果失败")
return True