- 修改数据库配置将MYSQL_DB从mys_info_v3改为mys_info_test - 新增auth_users表wechat_openid字段并建立唯一索引 - 为plan_checkins表添加计划性能索引(idx_plan_period_student) - 将checkin_records_old表关联方式从student_id改为user_id - 将plan_checkins表关联方式从student_id改为user_id并更新附件路径 - 移除routes/auth.py中的uuid导入使用安全token生成函数 - 重构routes/checkin.py打卡逻辑去除course_type字段并优化返回结构 - 添加打卡时间限制验证功能防止过期打卡操作 - 实现小组统计功能支持老师查看各组打卡情况 - 更新主页index.html界面适配新的打卡和计划功能 - 调整计划提交时间规则为周五至周四周期 - 添加月度和年度计划提交状态验证机制
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import os
|
|
|
|
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
|
|
|
from .. import config, db
|
|
from ..user import user_payload
|
|
from .auth import get_current_user
|
|
|
|
router = APIRouter(prefix="/api/v1/profile", tags=["profile"])
|
|
|
|
MAX_AVATAR_SIZE = 4 * 1024 * 1024
|
|
ALLOWED_AVATAR_EXT = {".png", ".jpg", ".jpeg", ".webp"}
|
|
|
|
|
|
def _profile(user):
|
|
payload = user_payload(user)
|
|
return {
|
|
"name": payload["name"],
|
|
"student_id": payload["student_id"],
|
|
"display_name": payload["display_name"],
|
|
"phone": user.get("phone_display", "") or "",
|
|
"group": payload["group"],
|
|
"role": payload["role"],
|
|
"class_groups": payload["class_groups"],
|
|
"wechat_avatar": user.get("wechat_avatar", ""),
|
|
"login_type": user.get("login_type", ""),
|
|
}
|
|
|
|
|
|
@router.get("")
|
|
def get_profile(user=Depends(get_current_user)):
|
|
return {"success": True, **_profile(user)}
|
|
|
|
|
|
@router.post("/avatar")
|
|
def upload_avatar(file: UploadFile = File(...), user=Depends(get_current_user)):
|
|
data = file.file.read()
|
|
if len(data) > MAX_AVATAR_SIZE:
|
|
raise HTTPException(status_code=400, detail="头像不能超过4MB")
|
|
|
|
safe_name = os.path.basename(file.filename or "")
|
|
ext = os.path.splitext(safe_name)[1].lower()
|
|
if ext not in ALLOWED_AVATAR_EXT:
|
|
raise HTTPException(status_code=400, detail="不支持的文件类型")
|
|
|
|
avatar_base = config.AVATAR_DIR
|
|
uid = str(user["id"])
|
|
save_dir = os.path.join(avatar_base, uid)
|
|
os.makedirs(save_dir, exist_ok=True)
|
|
for old_name in os.listdir(save_dir):
|
|
if old_name.startswith("avatar.") and old_name != "avatar" + ext:
|
|
try:
|
|
os.remove(os.path.join(save_dir, old_name))
|
|
except OSError:
|
|
pass
|
|
|
|
filename = "avatar" + ext
|
|
file_path = os.path.join(save_dir, filename)
|
|
with open(file_path, "wb") as f:
|
|
f.write(data)
|
|
|
|
url_path = "/clock/static/avatars/" + uid + "/" + filename
|
|
|
|
db.execute("UPDATE auth_users SET wechat_avatar=%s WHERE id=%s", (url_path, user["id"]))
|
|
return {"success": True, "wechat_avatar": url_path}
|