From a8296a82e5014124cd1d173a3d2e679f4145e433 Mon Sep 17 00:00:00 2001 From: zhinianboke <115088296+zhinianboke@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:09:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=91=E9=80=81=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XianyuAutoAsync.py | 44 +++++++++--- db_manager.py | 34 ++++++++- reply_server.py | 71 ++++++++++++++++++ static/index.html | 40 +++++++++++ static/js/app.js | 174 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+), 10 deletions(-) diff --git a/XianyuAutoAsync.py b/XianyuAutoAsync.py index 45d7bd1..a02e232 100644 --- a/XianyuAutoAsync.py +++ b/XianyuAutoAsync.py @@ -261,11 +261,23 @@ class XianyuLive: # 检查是否是图片发送标记 if delivery_content.startswith("__IMAGE_SEND__"): - # 提取图片URL - image_url = delivery_content.replace("__IMAGE_SEND__", "") + # 提取卡券ID和图片URL + image_data = delivery_content.replace("__IMAGE_SEND__", "") + if "|" in image_data: + card_id_str, image_url = image_data.split("|", 1) + try: + card_id = int(card_id_str) + except ValueError: + logger.error(f"无效的卡券ID: {card_id_str}") + card_id = None + else: + # 兼容旧格式(没有卡券ID) + card_id = None + image_url = image_data + # 发送图片消息 try: - await self.send_image_msg(websocket, chat_id, send_user_id, image_url) + await self.send_image_msg(websocket, chat_id, send_user_id, image_url, card_id=card_id) logger.info(f'[{msg_time}] 【自动发货图片】已向 {user_url} 发送图片: {image_url}') await self.send_delivery_failure_notification(send_user_name, send_user_id, item_id, "发货成功") except Exception as e: @@ -1081,6 +1093,18 @@ class XianyuLive: except Exception as e: logger.error(f"更新关键词图片URL失败: {e}") + async def _update_card_image_url(self, card_id: int, new_image_url: str): + """更新卡券的图片URL""" + try: + from db_manager import db_manager + success = db_manager.update_card_image_url(card_id, new_image_url) + if success: + logger.info(f"卡券图片URL已更新: 卡券ID={card_id} -> {new_image_url}") + else: + logger.warning(f"卡券图片URL更新失败: 卡券ID={card_id}") + except Exception as e: + logger.error(f"更新卡券图片URL失败: {e}") + async def get_ai_reply(self, send_user_name: str, send_user_id: str, send_message: str, item_id: str, chat_id: str): """获取AI回复""" try: @@ -1935,11 +1959,11 @@ class XianyuLive: delivery_content = db_manager.consume_batch_data(rule['card_id']) elif rule['card_type'] == 'image': - # 图片类型:返回图片发送标记 + # 图片类型:返回图片发送标记,包含卡券ID image_url = rule.get('image_url') if image_url: - delivery_content = f"__IMAGE_SEND__{image_url}" - logger.info(f"准备发送图片: {image_url}") + delivery_content = f"__IMAGE_SEND__{rule['card_id']}|{image_url}" + logger.info(f"准备发送图片: {image_url} (卡券ID: {rule['card_id']})") else: logger.error(f"图片卡券缺少图片URL: 卡券ID={rule['card_id']}") delivery_content = None @@ -2788,7 +2812,7 @@ class XianyuLive: if reply: # 检查是否是图片发送标记 if reply.startswith("__IMAGE_SEND__"): - # 提取图片URL + # 提取图片URL(关键词回复不包含卡券ID) image_url = reply.replace("__IMAGE_SEND__", "") # 发送图片消息 try: @@ -3124,7 +3148,7 @@ class XianyuLive: "items": all_items } - async def send_image_msg(self, ws, cid, toid, image_url, width=800, height=600): + async def send_image_msg(self, ws, cid, toid, image_url, width=800, height=600, card_id=None): """发送图片消息""" try: # 检查图片URL是否需要上传到CDN @@ -3149,6 +3173,10 @@ class XianyuLive: logger.info(f"【{self.cookie_id}】图片上传成功,CDN URL: {cdn_url}") image_url = cdn_url + # 如果是卡券图片,更新数据库中的图片URL + if card_id is not None: + await self._update_card_image_url(card_id, cdn_url) + # 获取实际图片尺寸 from utils.image_utils import image_manager try: diff --git a/db_manager.py b/db_manager.py index dee8dfa..d3e04b9 100644 --- a/db_manager.py +++ b/db_manager.py @@ -2545,8 +2545,9 @@ class DBManager: def update_card(self, card_id: int, name: str = None, card_type: str = None, api_config=None, text_content: str = None, data_content: str = None, - description: str = None, enabled: bool = None, delay_seconds: int = None, - is_multi_spec: bool = None, spec_name: str = None, spec_value: str = None): + image_url: str = None, description: str = None, enabled: bool = None, + delay_seconds: int = None, is_multi_spec: bool = None, spec_name: str = None, + spec_value: str = None): """更新卡券""" with self.lock: try: @@ -2580,6 +2581,9 @@ class DBManager: if data_content is not None: update_fields.append("data_content = ?") params.append(data_content) + if image_url is not None: + update_fields.append("image_url = ?") + params.append(image_url) if description is not None: update_fields.append("description = ?") params.append(description) @@ -2620,6 +2624,32 @@ class DBManager: self.conn.rollback() raise + def update_card_image_url(self, card_id: int, new_image_url: str) -> bool: + """更新卡券的图片URL""" + with self.lock: + try: + cursor = self.conn.cursor() + + # 更新图片URL + self._execute_sql(cursor, + "UPDATE cards SET image_url = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND type = 'image'", + (new_image_url, card_id)) + + self.conn.commit() + + # 检查是否有行被更新 + if cursor.rowcount > 0: + logger.info(f"卡券图片URL更新成功: 卡券ID: {card_id}, 新URL: {new_image_url}") + return True + else: + logger.warning(f"未找到匹配的图片卡券: 卡券ID: {card_id}") + return False + + except Exception as e: + logger.error(f"更新卡券图片URL失败: {e}") + self.conn.rollback() + return False + # ==================== 自动发货规则方法 ==================== def create_delivery_rule(self, keyword: str, card_id: int, delivery_count: int = 1, diff --git a/reply_server.py b/reply_server.py index 7f5a205..bf7a406 100644 --- a/reply_server.py +++ b/reply_server.py @@ -2148,6 +2148,7 @@ def update_card(card_id: int, card_data: dict, _: None = Depends(require_auth)): api_config=card_data.get('api_config'), text_content=card_data.get('text_content'), data_content=card_data.get('data_content'), + image_url=card_data.get('image_url'), description=card_data.get('description'), enabled=card_data.get('enabled', True), delay_seconds=card_data.get('delay_seconds'), @@ -2163,6 +2164,76 @@ def update_card(card_id: int, card_data: dict, _: None = Depends(require_auth)): raise HTTPException(status_code=500, detail=str(e)) +@app.put("/cards/{card_id}/image") +async def update_card_with_image( + card_id: int, + image: UploadFile = File(...), + name: str = Form(...), + type: str = Form(...), + description: str = Form(default=""), + delay_seconds: int = Form(default=0), + enabled: bool = Form(default=True), + is_multi_spec: bool = Form(default=False), + spec_name: str = Form(default=""), + spec_value: str = Form(default=""), + current_user: Dict[str, Any] = Depends(get_current_user) +): + """更新带图片的卡券""" + try: + logger.info(f"接收到带图片的卡券更新请求: card_id={card_id}, name={name}, type={type}") + + # 验证图片文件 + if not image.content_type or not image.content_type.startswith('image/'): + logger.warning(f"无效的图片文件类型: {image.content_type}") + raise HTTPException(status_code=400, detail="请上传图片文件") + + # 验证多规格字段 + if is_multi_spec: + if not spec_name or not spec_value: + raise HTTPException(status_code=400, detail="多规格卡券必须提供规格名称和规格值") + + # 读取图片数据 + image_data = await image.read() + logger.info(f"读取图片数据成功,大小: {len(image_data)} bytes") + + # 保存图片 + image_url = image_manager.save_image(image_data, image.filename) + if not image_url: + logger.error("图片保存失败") + raise HTTPException(status_code=400, detail="图片保存失败") + + logger.info(f"图片保存成功: {image_url}") + + # 更新卡券 + from db_manager import db_manager + success = db_manager.update_card( + card_id=card_id, + name=name, + card_type=type, + image_url=image_url, + description=description, + enabled=enabled, + delay_seconds=delay_seconds, + is_multi_spec=is_multi_spec, + spec_name=spec_name if is_multi_spec else None, + spec_value=spec_value if is_multi_spec else None + ) + + if success: + logger.info(f"卡券更新成功: {name} (ID: {card_id})") + return {"message": "卡券更新成功", "image_url": image_url} + else: + # 如果数据库更新失败,删除已保存的图片 + image_manager.delete_image(image_url) + raise HTTPException(status_code=404, detail="卡券不存在") + + except HTTPException: + raise + except Exception as e: + logger.error(f"更新带图片的卡券失败: {e}") + raise HTTPException(status_code=500, detail=str(e)) + + # 自动发货规则API @app.get("/delivery-rules") def get_delivery_rules(current_user: Dict[str, Any] = Depends(get_current_user)): diff --git a/static/index.html b/static/index.html index 0641f7f..2f2b7a3 100644 --- a/static/index.html +++ b/static/index.html @@ -1528,6 +1528,7 @@ + @@ -1592,6 +1593,45 @@ + +
+