Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -102,6 +102,12 @@ class ItemCreate(BaseModel):
|
|
| 102 |
author: str
|
| 103 |
price: float = 0.0 # 标价/积分字段
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# --- 1. 用户系统 ---
|
| 106 |
@app.post("/api/users/register")
|
| 107 |
async def register_user(user: UserRegister):
|
|
@@ -121,7 +127,9 @@ async def register_user(user: UserRegister):
|
|
| 121 |
# 构建用户档案,初始化各项统计为 0
|
| 122 |
new_user = user.dict()
|
| 123 |
new_user.update({
|
| 124 |
-
"
|
|
|
|
|
|
|
| 125 |
"privacy": {"follows": False, "likes": False, "favorites": False, "downloads": False}
|
| 126 |
})
|
| 127 |
|
|
@@ -159,9 +167,40 @@ async def get_user_profile(account: str):
|
|
| 159 |
raise HTTPException(status_code=404, detail="用户不存在")
|
| 160 |
|
| 161 |
user_data = users_db[account]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
safe_user_data = {k: v for k, v in user_data.items() if k != "password"}
|
| 163 |
return {"status": "success", "data": safe_user_data}
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# --- 2. 排行榜核心数据流 ---
|
| 166 |
@app.get("/api/items")
|
| 167 |
async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
|
|
@@ -180,6 +219,44 @@ async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
|
|
| 180 |
|
| 181 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# 【核心修改】:处理前端发布的新插件/应用请求
|
| 184 |
@app.post("/api/items")
|
| 185 |
async def create_item(item: ItemCreate):
|
|
|
|
| 102 |
author: str
|
| 103 |
price: float = 0.0 # 标价/积分字段
|
| 104 |
|
| 105 |
+
# 【新增】:关注功能的数据模型
|
| 106 |
+
class FollowToggle(BaseModel):
|
| 107 |
+
user_id: str
|
| 108 |
+
target_account: str
|
| 109 |
+
is_active: bool
|
| 110 |
+
|
| 111 |
# --- 1. 用户系统 ---
|
| 112 |
@app.post("/api/users/register")
|
| 113 |
async def register_user(user: UserRegister):
|
|
|
|
| 127 |
# 构建用户档案,初始化各项统计为 0
|
| 128 |
new_user = user.dict()
|
| 129 |
new_user.update({
|
| 130 |
+
"created_at": int(time.time()),
|
| 131 |
+
"followers": [],
|
| 132 |
+
"following": [],
|
| 133 |
"privacy": {"follows": False, "likes": False, "favorites": False, "downloads": False}
|
| 134 |
})
|
| 135 |
|
|
|
|
| 167 |
raise HTTPException(status_code=404, detail="用户不存在")
|
| 168 |
|
| 169 |
user_data = users_db[account]
|
| 170 |
+
|
| 171 |
+
# 【核心】:动态聚合该用户的作品收到的点赞、收藏和被使用总数
|
| 172 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 173 |
+
user_items = [item for item in items_db if item.get("author") == account]
|
| 174 |
+
|
| 175 |
+
user_data["receivedLikes"] = sum(item.get("likes", 0) for item in user_items)
|
| 176 |
+
user_data["receivedFavorites"] = sum(item.get("favorites", 0) for item in user_items)
|
| 177 |
+
user_data["receivedUses"] = sum(item.get("uses", 0) for item in user_items)
|
| 178 |
+
|
| 179 |
safe_user_data = {k: v for k, v in user_data.items() if k != "password"}
|
| 180 |
return {"status": "success", "data": safe_user_data}
|
| 181 |
|
| 182 |
+
@app.post("/api/users/follow")
|
| 183 |
+
async def toggle_follow(follow: FollowToggle):
|
| 184 |
+
users_db = db.load_data("users.json", default_data={})
|
| 185 |
+
if follow.target_account not in users_db or follow.user_id not in users_db:
|
| 186 |
+
raise HTTPException(status_code=404, detail="用户不存在")
|
| 187 |
+
|
| 188 |
+
target_user = users_db[follow.target_account]
|
| 189 |
+
current_user = users_db[follow.user_id]
|
| 190 |
+
|
| 191 |
+
target_followers = target_user.setdefault("followers", [])
|
| 192 |
+
current_following = current_user.setdefault("following", [])
|
| 193 |
+
|
| 194 |
+
if follow.is_active:
|
| 195 |
+
if follow.user_id not in target_followers: target_followers.append(follow.user_id)
|
| 196 |
+
if follow.target_account not in current_following: current_following.append(follow.target_account)
|
| 197 |
+
else:
|
| 198 |
+
if follow.user_id in target_followers: target_followers.remove(follow.user_id)
|
| 199 |
+
if follow.target_account in current_following: current_following.remove(follow.target_account)
|
| 200 |
+
|
| 201 |
+
db.save_data("users.json", users_db)
|
| 202 |
+
return {"status": "success"}
|
| 203 |
+
|
| 204 |
# --- 2. 排行榜核心数据流 ---
|
| 205 |
@app.get("/api/items")
|
| 206 |
async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
|
|
|
|
| 219 |
|
| 220 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 221 |
|
| 222 |
+
# 【新增】:创作者排行榜动态聚合与排序接口
|
| 223 |
+
@app.get("/api/creators")
|
| 224 |
+
async def get_creators(sort: str = "downloads", limit: int = 20):
|
| 225 |
+
users_db = db.load_data("users.json", default_data={})
|
| 226 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 227 |
+
|
| 228 |
+
creators = []
|
| 229 |
+
for account, u in users_db.items():
|
| 230 |
+
u_items = [i for i in items_db if i.get("author") == account]
|
| 231 |
+
likes = sum(i.get("likes", 0) for i in u_items)
|
| 232 |
+
favorites = sum(i.get("favorites", 0) for i in u_items)
|
| 233 |
+
uses = sum(i.get("uses", 0) for i in u_items)
|
| 234 |
+
tools_count = sum(1 for i in u_items if i.get("type") == "tool")
|
| 235 |
+
apps_count = sum(1 for i in u_items if i.get("type") == "app")
|
| 236 |
+
|
| 237 |
+
creators.append({
|
| 238 |
+
"account": account,
|
| 239 |
+
"name": u.get("name", account),
|
| 240 |
+
"avatar": u.get("avatarDataUrl", "https://via.placeholder.com/150"),
|
| 241 |
+
"shortDesc": u.get("intro", "这个人很懒,什么都没写..."),
|
| 242 |
+
"fullDesc": u.get("intro", "这个人很懒,什么都没写..."),
|
| 243 |
+
"likes": likes,
|
| 244 |
+
"favorites": favorites,
|
| 245 |
+
"downloads": uses,
|
| 246 |
+
"toolsCount": tools_count,
|
| 247 |
+
"appsCount": apps_count,
|
| 248 |
+
"followers": len(u.get("followers", [])),
|
| 249 |
+
"created_at": u.get("created_at", 0),
|
| 250 |
+
"trendData": {"tools": [0,0,0,0,0, tools_count], "apps": [0,0,0,0,0, apps_count]}
|
| 251 |
+
})
|
| 252 |
+
|
| 253 |
+
if sort == "likes": creators.sort(key=lambda x: x.get("likes", 0), reverse=True)
|
| 254 |
+
elif sort == "favorites": creators.sort(key=lambda x: x.get("favorites", 0), reverse=True)
|
| 255 |
+
elif sort == "downloads": creators.sort(key=lambda x: x.get("downloads", 0), reverse=True)
|
| 256 |
+
else: creators.sort(key=lambda x: x.get("created_at", 0), reverse=True)
|
| 257 |
+
|
| 258 |
+
return {"status": "success", "data": creators[:limit]}
|
| 259 |
+
|
| 260 |
# 【核心修改】:处理前端发布的新插件/应用请求
|
| 261 |
@app.post("/api/items")
|
| 262 |
async def create_item(item: ItemCreate):
|