81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
import configparser
|
|
from pathlib import Path
|
|
from fastapi import HTTPException
|
|
|
|
config_path = Path("backend/config.conf")
|
|
|
|
def _save_config(config):
|
|
try:
|
|
with open(config_path, 'w') as configfile:
|
|
config.write(configfile)
|
|
except Exception as e:
|
|
raise HTTPException(500, f"配置保存失败: {str(e)}")
|
|
|
|
def get_price_config():
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
return {
|
|
"points_rate": float(config.get("price", "points_rate")),
|
|
"get_points_rate": float(config.get("price", "get_points_rate"))
|
|
}
|
|
|
|
def get_night_config():
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
return {
|
|
"start_time": config.get("stay_up_late", "start_time"),
|
|
"end_time": config.get("stay_up_late", "end_time"),
|
|
"price_minutes": int(config.get("stay_up_late", "price_minutes"))
|
|
}
|
|
|
|
def update_points_rate(new_rate: float):
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
config.set("price", "points_rate", str(new_rate))
|
|
_save_config(config)
|
|
return {"message": "积分抵扣率更新成功"}
|
|
|
|
def update_get_points_rate(new_rate: float):
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
config.set("price", "get_points_rate", str(new_rate))
|
|
_save_config(config)
|
|
return {"message": "积分获取率更新成功"}
|
|
|
|
def update_night_start(new_time: str):
|
|
# 添加时间格式验证
|
|
try:
|
|
from datetime import datetime
|
|
datetime.strptime(new_time, "%H:%M:%S")
|
|
except ValueError:
|
|
raise HTTPException(400, "时间格式应为 HH:MM:SS")
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
config.set("stay_up_late", "start_time", new_time)
|
|
_save_config(config)
|
|
return {"message": "晚场开始时间更新成功"}
|
|
|
|
def update_night_end(new_time: str):
|
|
# 同样添加时间验证
|
|
try:
|
|
from datetime import datetime
|
|
datetime.strptime(new_time, "%H:%M:%S")
|
|
except ValueError:
|
|
raise HTTPException(400, "时间格式应为 HH:MM:SS")
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
config.set("stay_up_late", "end_time", new_time)
|
|
_save_config(config)
|
|
return {"message": "晚场结束时间更新成功"}
|
|
|
|
def update_price_minutes(minutes: int):
|
|
if minutes <= 0:
|
|
raise HTTPException(400, "分钟数必须大于0")
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
config.set("stay_up_late", "price_minutes", str(minutes))
|
|
_save_config(config)
|
|
return {"message": "计费分钟间隔更新成功"}
|