diff --git a/db_manager.py b/db_manager.py index 7bed531..85b45f4 100644 --- a/db_manager.py +++ b/db_manager.py @@ -113,6 +113,7 @@ class DBManager: value TEXT NOT NULL, user_id INTEGER NOT NULL, auto_confirm INTEGER DEFAULT 1, + remark TEXT DEFAULT '', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) @@ -372,6 +373,15 @@ class DBManager: # 检查并更新CHECK约束(重建表以支持image类型) self._update_cards_table_constraints(cursor) + # 检查cookies表是否存在remark列 + cursor.execute("PRAGMA table_info(cookies)") + cookie_columns = [column[1] for column in cursor.fetchall()] + + if 'remark' not in cookie_columns: + logger.info("添加cookies表的remark列...") + cursor.execute("ALTER TABLE cookies ADD COLUMN remark TEXT DEFAULT ''") + logger.info("数据库迁移完成:添加remark列") + except Exception as e: logger.error(f"数据库迁移失败: {e}") # 迁移失败不应该阻止程序启动 @@ -1077,11 +1087,11 @@ class DBManager: return None def get_cookie_details(self, cookie_id: str) -> Optional[Dict[str, any]]: - """获取Cookie的详细信息,包括user_id和auto_confirm""" + """获取Cookie的详细信息,包括user_id、auto_confirm和remark""" with self.lock: try: cursor = self.conn.cursor() - self._execute_sql(cursor, "SELECT id, value, user_id, auto_confirm, created_at FROM cookies WHERE id = ?", (cookie_id,)) + self._execute_sql(cursor, "SELECT id, value, user_id, auto_confirm, remark, created_at FROM cookies WHERE id = ?", (cookie_id,)) result = cursor.fetchone() if result: return { @@ -1089,7 +1099,8 @@ class DBManager: 'value': result[1], 'user_id': result[2], 'auto_confirm': bool(result[3]), - 'created_at': result[4] + 'remark': result[4] or '', + 'created_at': result[5] } return None except Exception as e: @@ -1109,6 +1120,19 @@ class DBManager: logger.error(f"更新自动确认发货设置失败: {e}") return False + def update_cookie_remark(self, cookie_id: str, remark: str) -> bool: + """更新Cookie的备注""" + with self.lock: + try: + cursor = self.conn.cursor() + self._execute_sql(cursor, "UPDATE cookies SET remark = ? WHERE id = ?", (remark, cookie_id)) + self.conn.commit() + logger.info(f"更新账号 {cookie_id} 备注: {remark}") + return True + except Exception as e: + logger.error(f"更新账号备注失败: {e}") + return False + def get_auto_confirm(self, cookie_id: str) -> bool: """获取Cookie的自动确认发货设置""" with self.lock: diff --git a/reply_server.py b/reply_server.py index 8dae2f5..b6b64b6 100644 --- a/reply_server.py +++ b/reply_server.py @@ -908,11 +908,16 @@ def get_cookies_details(current_user: Dict[str, Any] = Depends(get_current_user) for cookie_id, cookie_value in user_cookies.items(): cookie_enabled = cookie_manager.manager.get_cookie_status(cookie_id) auto_confirm = db_manager.get_auto_confirm(cookie_id) + # 获取备注信息 + cookie_details = db_manager.get_cookie_details(cookie_id) + remark = cookie_details.get('remark', '') if cookie_details else '' + result.append({ 'id': cookie_id, 'value': cookie_value, 'enabled': cookie_enabled, - 'auto_confirm': auto_confirm + 'auto_confirm': auto_confirm, + 'remark': remark }) return result @@ -1443,6 +1448,10 @@ class AutoConfirmUpdate(BaseModel): auto_confirm: bool +class RemarkUpdate(BaseModel): + remark: str + + @app.put("/cookies/{cid}/auto-confirm") def update_auto_confirm(cid: str, update_data: AutoConfirmUpdate, current_user: Dict[str, Any] = Depends(get_current_user)): """更新账号的自动确认发货设置""" @@ -1503,6 +1512,65 @@ def get_auto_confirm(cid: str, current_user: Dict[str, Any] = Depends(get_curren raise HTTPException(status_code=500, detail=str(e)) +@app.put("/cookies/{cid}/remark") +def update_cookie_remark(cid: str, update_data: RemarkUpdate, current_user: Dict[str, Any] = Depends(get_current_user)): + """更新账号备注""" + if cookie_manager.manager is None: + raise HTTPException(status_code=500, detail="CookieManager 未就绪") + try: + # 检查cookie是否属于当前用户 + user_id = current_user['user_id'] + from db_manager import db_manager + user_cookies = db_manager.get_all_cookies(user_id) + + if cid not in user_cookies: + raise HTTPException(status_code=403, detail="无权限操作该Cookie") + + # 更新备注 + success = db_manager.update_cookie_remark(cid, update_data.remark) + if success: + log_with_user('info', f"更新账号备注: {cid} -> {update_data.remark}", current_user) + return { + "message": "备注更新成功", + "remark": update_data.remark + } + else: + raise HTTPException(status_code=500, detail="备注更新失败") + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + +@app.get("/cookies/{cid}/remark") +def get_cookie_remark(cid: str, current_user: Dict[str, Any] = Depends(get_current_user)): + """获取账号备注""" + if cookie_manager.manager is None: + raise HTTPException(status_code=500, detail="CookieManager 未就绪") + try: + # 检查cookie是否属于当前用户 + user_id = current_user['user_id'] + from db_manager import db_manager + user_cookies = db_manager.get_all_cookies(user_id) + + if cid not in user_cookies: + raise HTTPException(status_code=403, detail="无权限操作该Cookie") + + # 获取Cookie详细信息(包含备注) + cookie_details = db_manager.get_cookie_details(cid) + if cookie_details: + return { + "remark": cookie_details.get('remark', ''), + "message": "获取备注成功" + } + else: + raise HTTPException(status_code=404, detail="账号不存在") + except HTTPException: + raise + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + diff --git a/static/index.html b/static/index.html index 2f2b7a3..1e27179 100644 --- a/static/index.html +++ b/static/index.html @@ -278,13 +278,14 @@ 账号ID - Cookie值 + Cookie值 关键词 状态 默认回复 AI回复 自动确认发货 - 操作 + 备注 + 操作 diff --git a/static/js/app.js b/static/js/app.js index 9c2895d..ec4a155 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -1072,7 +1072,7 @@ async function loadCookies() { if (cookieDetails.length === 0) { tbody.innerHTML = ` - +
暂无账号

请添加新的闲鱼账号开始使用

@@ -1199,6 +1199,13 @@ async function loadCookies() { + +
+ + ${cookie.remark || ' 添加备注'} + +
+