diff --git a/Utils/Dy/DyUtils.py b/Utils/Dy/DyUtils.py index 4e6450e..d92d272 100644 --- a/Utils/Dy/DyUtils.py +++ b/Utils/Dy/DyUtils.py @@ -12,6 +12,8 @@ import threading from datetime import datetime from dataclasses import dataclass, asdict from urllib.parse import urlencode +import re +import random import config # 导入 message_arg 中的方法 @@ -19,6 +21,503 @@ from Utils.Dy.message_arg import send_message, get_user_code, heartbeat_message from Utils.message_models import PlatformMessage +# ===== 抖音登录相关类集成开始 ===== + +class DyLogin: + """抖音登录核心类(集成自Dylogin.py,适配后端通知机制)""" + + def __init__(self, log_callback=None): + self.headers = { + "authority": "doudian-sso.jinritemai.com", + "accept": "application/json, text/plain, */*", + "accept-language": "zh-CN,zh;q=0.9", + "cache-control": "no-cache", + "content-type": "application/x-www-form-urlencoded", + "origin": "https://fxg.jinritemai.com", + "pragma": "no-cache", + "referer": "https://fxg.jinritemai.com/", + "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "\"Windows\"", + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-site", + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", + "x-requested-with": "XMLHttpRequest", + "x-tt-passport-csrf-token": "8aa53236d26cf3328f70ef5d7a52e2ed" + } + self.cookies = { + "passport_csrf_token": "8aa53236d26cf3328f70ef5d7a52e2ed", + "passport_csrf_token_default": "8aa53236d26cf3328f70ef5d7a52e2ed", + "ttwid": "1%7CFgSgrZadP_YyHmeFQZ8Sj2Qo2isOgOYHVWkzE4NIOMM%7C1759041029%7C80598fd5e92a57b97469827b096864a940b5de23748185987bc2f45f25f8c88b" + } + self.log_callback = log_callback + + def _log(self, message, level="INFO"): + """内部日志方法""" + if self.log_callback: + self.log_callback(message, level) + else: + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + color_map = { + "ERROR": "\033[91m", + "WARNING": "\033[93m", + "SUCCESS": "\033[92m", + "DEBUG": "\033[96m", + } + color = color_map.get(level, "") + reset = "\033[0m" + print(f"{color}[{timestamp}] [{level}] {message}{reset}") + + @staticmethod + def encrypt(data=None): + """抖音数据加密方法""" + if data is None: + return "" + e = lambda t: [ord(c) for c in str(t)] + e = e(data) + n = [] + for r in range(len(e)): + n.append(hex(5 ^ e[r])[2:]) + return "".join(n) + + def send_activation_code(self, mobile_phone): + """发送激活码""" + url = "https://doudian-sso.jinritemai.com/send_activation_code/v2/" + params = { + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "account_sdk_source_info": "7e276d64776172647760466a6b66707777606b667c273f3433292772606761776c736077273f63646976602927666d776a686061776c736077273f63646976602927766d60696961776c736077273f63646976602927756970626c6b76273f302927756077686c76766c6a6b76273f5e7e276b646860273f276b6a716c636c6664716c6a6b762729277671647160273f2761606b6c606127785829276c6b6b60774d606c626d71273f3c353329276c6b6b6077526c61716d273f3432353229276a707160774d606c626d71273f3435373229276a70716077526c61716d273f34323532292776716a64776260567164717076273f7e276c6b61607d60614147273f7e276c6167273f276a676f6066712729276a75606b273f2763706b66716c6a6b2729276c6b61607d60614147273f276a676f6066712729274c41474e607c57646b6260273f2763706b66716c6a6b2729276a75606b4164716467647660273f27706b6160636c6b60612729276c7656646364776c273f636469766029276d6476436071666d273f71777060782927696a66646956716a77646260273f7e276c76567075756a77714956716a77646260273f717770602927766c7f60273f343735343d34292772776c7160273f7177706078292776716a7764626054706a7164567164717076273f7e277076646260273f363c333133292774706a7164273f34323c30343632323c3329276c7655776c73647160273f6364697660787829277260676269273f7e2773606b616a77273f27426a6a626960254c6b662b252d4c6b7160692c27292777606b6160776077273f27444b424940252d4c6b71606929254c6b7160692d572c254c776c762d572c255d6025427764756d6c6676252d357d35353535443244352c25416c77606671364134342573765a305a352575765a305a35292541364134342c277829276b6a716c636c6664716c6a6b556077686c76766c6a6b273f2761606b6c6061272927756077636a7768646b6660273f7e27716c68604a776c626c6b273f3432303c3531343537363d353d2b3d2927707660614f564d606475566c7f60273f333534343d31323c29276b64736c6264716c6a6b516c686c6b62273f7e276160666a616061476a617c566c7f60273f3030313d2927606b71777c517c7560273f276b64736c6264716c6a6b2729276c6b6c716c64716a77517c7560273f276b64736c6264716c6a6b2729276b646860273f276d717175763f2a2a637d622b6f6c6b776c716068646c2b666a682a696a626c6b2a666a68686a6b27292777606b61607747696a666e6c6b62567164717076273f276b6a6b2867696a666e6c6b62272927766077736077516c686c6b62273f276c6b6b60772966616b286664666d602960616260296a776c626c6b272927627069605671647771273f276b6a6b602729276270696041707764716c6a6b273f276b6a6b602778782927776074706076715a6d6a7671273f27637d622b6f6c6b776c716068646c2b666a68272927776074706076715a7564716d6b646860273f272a696a626c6b2a666a68686a6b27292767776a72766077273f7e7878", + "msToken": "", + "X-Bogus": "DFSzswVL1tybaQLLC9jRHiB9Piu6", + "_signature": "_02B4Z6wo00001P1YC5QAAIDDwPERCIiHDXT9WA8AAFeUKIdq.vB20O6tjorWCqmtqK591BKCWaGNpder-vHZ3rvQkbxFhNXfTJcMBW66GwjlIj-NQ6d52sU1iZ1ediX423KeCS5rakvHWLrua8" + } + data = { + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "mix_mode": "1", + "service": "https://fxg.jinritemai.com", + "type": "3731", + "mobile": self.encrypt(data=mobile_phone), + "captcha_key": "" + } + response = requests.post(url, headers=self.headers, cookies=self.cookies, params=params, data=data) + self._log(f"发送验证码响应: {response.text}", "DEBUG") + return response + + def verify(self, mobile_phone, code): + """验证手机验证码""" + url = "https://doudian-sso.jinritemai.com/quick_login/v2/" + params = { + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "account_sdk_source_info": "7e276d64776172647760466a6b66707777606b667c273f3433292772606761776c736077273f63646976602927666d776a686061776c736077273f63646976602927766d60696961776c736077273f63646976602927756970626c6b76273f302927756077686c76766c6a6b76273f5e7e276b646860273f276b6a716c636c6664716c6a6b762729277671647160273f2761606b6c606127785829276c6b6b60774d606c626d71273f3c353329276c6b6b6077526c61716d273f3432353229276a707160774d606c626d71273f3435373229276a70716077526c61716d273f34323532292776716a64776260567164717076273f7e276c6b61607d60614147273f7e276c6167273f276a676f6066712729276a75606b273f2763706b66716c6a6b2729276c6b61607d60614147273f276a676f6066712729274c41474e607c57646b6260273f2763706b66716c6a6b2729276a75606b4164716467647660273f27706b6160636c6b60612729276c7656646364776c273f636469766029276d6476436071666d273f71777060782927696a66646956716a77646260273f7e276c76567075756a77714956716a77646260273f717770602927766c7f60273f343735343d34292772776c7160273f7177706078292776716a7764626054706a7164567164717076273f7e277076646260273f363c333133292774706a7164273f34323c30343632323c3329276c7655776c73647160273f6364697660787829277260676269273f7e2773606b616a77273f27426a6a626960254c6b662b252d4c6b7160692c27292777606b6160776077273f27444b424940252d4c6b71606929254c6b7160692d572c254c776c762d572c255d6025427764756d6c6676252d357d35353535443244352c25416c77606671364134342573765a305a352575765a305a35292541364134342c277829276b6a716c636c6664716c6a6b556077686c76766c6a6b273f2761606b6c6061272927756077636a7768646b6660273f7e27716c68604a776c626c6b273f3432303c3531343537363d353d2b3d2927707660614f564d606475566c7f60273f33343337323d373c29276b64736c6264716c6a6b516c686c6b62273f7e276160666a616061476a617c566c7f60273f3030313d2927606b71777c517c7560273f276b64736c6264716c6a6b2729276c6b6c716c64716a77517c7560273f276b64736c6264716c6a6b2729276b646860273f276d717175763f2a2a637d622b6f6c6b776c716068646c2b666a682a696a626c6b2a666a68686a6b27292777606b61607747696a666e6c6b62567164717076273f276b6a6b2867696a666e6c6b62272927766077736077516c686c6b62273f276c6b6b60772966616e286664666d602960616260296a776c626c6b272927627069605671647771273f276b6a6b602729276270696041707764716c6a6b273f276b6a6b602778782927776074706076715a6d6a7671273f27637d622b6f6c6b776c716068646c2b666a68272927776074706076715a7564716d6b646860273f272a696a626c6b2a666a68686a6b27292767776a72766077273f7e7878", + "msToken": "", + "X-Bogus": "DFSzswVL1tybaQLLC9jRHiB9Piu6", + "_signature": "_02B4Z6wo00001n3abFwAAIDBQHN2wdsMnWZ92mjAAPe9KIdq.vB20O6tjorWCqmtqK591BKCWaGNpder-vHZ3rvQkbxFhNXfTJcMBW66GwjlIj-NQ6d52sU1iZ1ediX423KeCS5rakvHWLru09" + } + data = { + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "service": "https://fxg.jinritemai.com", + "subject_aid": "4966", + "mix_mode": "1", + "mobile": self.encrypt(data=mobile_phone), + "code": self.encrypt(data=code), + "captcha_key": "", + "ewid": "72c89cf89652d486d53b316711709044", + "seraph_did": "", + "web_did": "72c89cf89652d486d53b316711709044", + "pc_did": "", + "redirect_sso_to_login": "false" + } + response = requests.post(url, headers=self.headers, cookies=self.cookies, params=params, data=data) + self.cookies.update(response.cookies.get_dict()) + self._log(f"验证码验证响应: {response.text}", "DEBUG") + + # 🔥 修复:增加错误处理,检查响应是否包含错误信息 + ticket = None + try: + # 尝试解析JSON响应 + response_data = response.json() + if "error_code" in response_data: + error_code = response_data.get("error_code", -1) + # 🔥 修复:error_code=0表示成功,非0才是错误 + if error_code != 0: + # 抖音返回了错误信息 + error_msg = response_data.get("description", "验证码验证失败") + self._log(f"抖音验证码验证失败: {error_msg} (错误码: {error_code})", "ERROR") + raise Exception(f"{error_msg}") + else: + # error_code=0表示成功,尝试从redirect_url中提取ticket + self._log(f"✅ 抖音验证码验证成功 (错误码: {error_code})", "SUCCESS") + redirect_url = response_data.get("redirect_url", "") + if redirect_url: + # 从redirect_url中提取ticket + ticket_matches = re.findall(r'ticket=([^&]+)', redirect_url) + if ticket_matches: + ticket = ticket_matches[0] + self._log(f"✅ 从redirect_url中提取到ticket: {ticket[:20]}...", "SUCCESS") + except json.JSONDecodeError: + # 如果不是JSON响应,继续用原来的方式解析 + pass + + # 🔥 修复:如果JSON中没有获取到ticket,尝试用原来的方式解析 + if not ticket: + ticket_matches = re.findall('ticket=(.*?)",', response.text) + if ticket_matches: + ticket = ticket_matches[0] + self._log(f"✅ 从响应文本中提取到ticket: {ticket[:20]}...", "SUCCESS") + + # 最终检查是否获取到ticket + if not ticket: + # 没有找到ticket,说明验证失败 + self._log("抖音验证码验证失败:响应中未找到ticket信息", "ERROR") + raise Exception("验证码验证失败:服务器未返回有效的ticket") + + return ticket + + def callback(self, ticket): + """回调获取登录状态""" + url = f"https://fxg.jinritemai.com/passport/sso/login/callback/?next=https%3A%2F%2Ffxg.jinritemai.com%2Flogin%2Fcommon&ticket={ticket}" + response = requests.get(url, headers=self.headers, cookies=self.cookies, allow_redirects=False) + self.cookies.update(response.cookies.get_dict()) + self._log(f"回调响应状态: {response.status_code}", "DEBUG") + + def subject_list(self): + """获取登录主体列表""" + url = "https://fxg.jinritemai.com/ecomauth/loginv1/get_login_subject" + params = { + "bus_type": "1", + "login_source": "doudian_pc_web", + "entry_source": "0", + "bus_child_type": "0", + "_lid": "438338769861" + } + response = requests.get(url, headers=self.headers, cookies=self.cookies, params=params) + response_data = response.json() + self._log(f"登录主体列表响应: {response_data}", "DEBUG") + + login_subject_list = response_data.get("data", {}).get("login_subject_list", []) + if not login_subject_list: + raise Exception("未获取到登录主体列表") + + login_subject_uid = login_subject_list[0].get("subject_id") + user_identity_id = login_subject_list[0].get("member_id") + encode_shop_id = login_subject_list[0].get("encode_shop_id") + return login_subject_uid, user_identity_id, encode_shop_id + + def tab_shop_login(self, login_subject_uid, user_identity_id, encode_shop_id): + """切换店铺登录""" + url = "https://doudian-sso.jinritemai.com/aff/subject/login/" + params = { + "subject_aid": "4966", + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "account_sdk_source_info": "7e276d64776172647760466a6b66707777606b667c273f3433292772606761776c736077273f63646976602927666d776a686061776c736077273f63646976602927766d60696961776c736077273f63646976602927756970626c6b76273f302927756077686c76766c6a6b76273f5e7e276b646860273f276b6a716c636c6664716c6a6b762729277671647160273f2761606b6c606127785829276c6b6b60774d606c626d71273f3c353329276c6b6b6077526c61716d273f3432353229276a707160774d606c626d71273f3435373229276a70716077526c61716d273f34323532292776716a64776260567164717076273f7e276c6b61607d60614147273f7e276c6167273f276a676f6066712729276a75606b273f2763706b66716c6a6b2729276c6b61607d60614147273f276a676f6066712729274c41474e607c57646b6260273f2763706b66716c6a6b2729276a75606b4164716467647660273f27706b6160636c6b60612729276c7656646364776c273f636469766029276d6476436071666d273f71777060782927696a66646956716a77646260273f7e276c76567075756a77714956716a77646260273f717770602927766c7f60273f343735343d32292772776c7160273f7177706078292776716a7764626054706a7164567164717076273f7e277076646260273f363c333133292774706a7164273f34323c30343632323c3329276c7655776c73647160273f6364697660787829277260676269273f7e2773606b616a77273f27426a6a626960254c6b662b252d4c6b7160692c27292777606b6160776077273f27444b424940252d4c6b71606929254c6b7160692d572c254c776c762d572c255d6025427764756d6c6676252d357d35353535443244352c25416c77606671364134342573765a305a352575765a305a35292541364134342c277829276b6a716c636c6664716c6a6b556077686c76766c6a6b273f2761606b6c6061272927756077636a7768646b6660273f7e27716c68604a776c626c6b273f3432303c3531343537363d353d2b3d2927707660614f564d606475566c7f60273f33343337323d373c29276b64736c6264716c6a6b516c686c6b62273f7e276160666a616061476a617c566c7f60273f3030313d2927606b71777c517c7560273f276b64736c6264716c6a6b2729276c6b6c716c64716a77517c7560273f276b64736c6264716c6a6b2729276b646860273f276d717175763f2a2a637d622b6f6c6b776c716068646c2b666a682a696a626c6b2a666a68686a6b27292777606b61607747696a666e6c6b62567164717076273f276b6a6b2867696a666e6c6b62272927766077736077516c686c6b62273f276c6b6b60772966616e286664666d602960616260296a776c626c6b272927627069605671647771273f276b6a6b602729276270696041707764716c6a6b273f276b6a6b602778782927776074706076715a6d6a7671273f27637d622b6f6c6b776c716068646c2b666a68272927776074706076715a7564716d6b646860273f272a696a626c6b2a666a68686a6b27292767776a72766077273f7e7878", + "msToken": "", + "X-Bogus": "DFSzswVL1tybaQLLC9jRHiB9Piu6", + "_signature": "_02B4Z6wo00001n3abFwAAIDBQHN2wdsMnWZ92mjAAPe9KIdq.vB20O6tjorWCqmtqK591BKCWaGNpder-vHZ3rvQkbxFhNXfTJcMBW66GwjlIj-NQ6d52sU1iZ1ediX423KeCS5rakvHWLru09" + } + data = { + "fp": "verify_mg3bm79v_56d5acc9_118c_ed40_6a1d_f3e184d5c666", + "aid": "4272", + "language": "zh", + "account_sdk_source": "web", + "service": "https://fxg.jinritemai.com", + "subject_aid": "4966", + "mix_mode": "1", + "login_subject_uid": login_subject_uid, + "user_identity_id": user_identity_id, + "encode_shop_id": encode_shop_id, + "captcha_key": "", + "ewid": "72c89cf89652d486d53b316711709044", + "seraph_did": "", + "web_did": "72c89cf89652d486d53b316711709044", + "pc_did": "", + "redirect_sso_to_login": "false" + } + response = requests.post(url, headers=self.headers, cookies=self.cookies, params=params, data=data) + self._log(f"切换店铺登录响应: {response.text}", "DEBUG") + ticket = re.findall('ticket=(.*?)",', response.text)[0] + self.cookies.update(response.cookies.get_dict()) + + # 🔥 执行完整的登录回调流程(参考Dylogin.py) + callback_url = f"https://fxg.jinritemai.com/passport/sso/aff/login/callback/?next=https%3A%2F%2Ffxg.jinritemai.com&ticket={ticket}&aid=4272&subject_aid=4966" + response = requests.get(callback_url, headers=self.headers, cookies=self.cookies, allow_redirects=False) + self.cookies.update(response.cookies.get_dict()) + self._log(f"主登录回调响应状态: {response.status_code}", "DEBUG") + + # 最终回调获取完整cookies + final_callback_url = f"https://fxg.jinritemai.com/ecomauth/loginv1/callback?login_source=doudian_pc_web&subject_aid=4966&encode_shop_id={encode_shop_id}&member_id={user_identity_id}&bus_child_type=0&entry_source=0&ecom_login_extra=&_lid=464136070178" + response = requests.get(final_callback_url, headers=self.headers, cookies=self.cookies) + self.cookies.update(response.cookies.get_dict()) + self._log(f"最终回调响应状态: {response.status_code}", "DEBUG") + + return ticket + + def get_shop_config(self): + """获取抖音平台配置信息(SHOP_ID、PIGEON_CID等)""" + try: + self._log("🔄 开始获取抖音平台配置", "DEBUG") + + # 获取配置信息的API调用 + headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', + 'Accept': 'application/json, text/plain, */*', + 'Referer': 'https://fxg.jinritemai.com/', + } + + params = [ + ('biz_type', '4'), + ('PIGEON_BIZ_TYPE', '2'), + ('_ts', int(time.time() * 1000)), + ('_pms', '1'), + ('FUSION', 'true'), + ] + + response = requests.get( + 'https://pigeon.jinritemai.com/chat/api/backstage/conversation/get_link_info', + params=params, + cookies=self.cookies, + headers=headers, + timeout=30 + ) + + self._log(f"配置请求响应状态: {response.status_code}", "DEBUG") + self._log(f"配置响应内容: {response.text}", "DEBUG") + + if response.status_code != 200: + self._log(f"❌ 配置请求失败: HTTP {response.status_code}", "ERROR") + return None + + data = response.json() + if data.get('code') != 0: + error_msg = data.get('message', '未知错误') + self._log(f"❌ 获取配置失败: {error_msg}", "ERROR") + return None + + config_data = data.get('data', {}) + + # 🔥 构造抖音平台必需的配置信息 + shop_config = { + 'SHOP_ID': '217051461', # 从登录响应或配置中获取 + 'PIGEON_CID': '1216524360102748', # 从配置中获取 + } + + # 如果API返回了相关信息,使用API返回的值 + if 'shopId' in config_data: + shop_config['SHOP_ID'] = str(config_data['shopId']) + if 'pigeonCid' in config_data: + shop_config['PIGEON_CID'] = str(config_data['pigeonCid']) + + self._log(f"✅ 获取到抖音配置: SHOP_ID={shop_config['SHOP_ID']}, PIGEON_CID={shop_config['PIGEON_CID']}", + "SUCCESS") + return shop_config + + except Exception as e: + self._log(f"❌ 获取抖音配置失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + return None + + def _send_verification_needed_message(self, store_id, phone_number=None): + """向后端发送需要验证码的通知""" + try: + self._log(f"开始发送验证码需求通知,店铺ID: {store_id}, 手机号: {phone_number}", "INFO") + from WebSocket.backend_singleton import get_backend_client + backend = get_backend_client() + self._log(f"获取到后端客户端: {backend is not None}", "DEBUG") + + if backend: + message = { + "type": "connect_message", + "store_id": store_id, + "status": False, + "content": "需要验证码", + "phone_number": phone_number + } + self._log(f"准备发送验证码通知消息: {message}", "DEBUG") + backend.send_message(message) + self._log("✅ 成功向后端发送验证码需求通知(含手机号)", "SUCCESS") + else: + self._log("❌ 后端客户端为空,无法发送验证码需求通知", "ERROR") + except Exception as e: + self._log(f"❌ 发送验证码需求通知失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + + def _send_verification_error_message(self, store_id, error_msg): + """向后端发送验证码错误的通知""" + try: + self._log(f"开始发送验证码错误通知,店铺ID: {store_id}, 错误: {error_msg}", "INFO") + from WebSocket.backend_singleton import get_backend_client + backend = get_backend_client() + + if backend: + message = { + "type": "connect_message", + "store_id": store_id, + "status": False, + "content": error_msg + } + self._log(f"准备发送验证码错误消息: {message}", "DEBUG") + backend.send_message(message) + self._log("✅ 成功向后端发送验证码错误通知", "SUCCESS") + else: + self._log("❌ 后端客户端为空,无法发送验证码错误通知", "ERROR") + except Exception as e: + self._log(f"❌ 发送验证码错误通知失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + + def _send_login_success_message(self, store_id): + """向后端发送登录成功的通知""" + try: + self._log(f"开始发送登录成功通知,店铺ID: {store_id}", "INFO") + from WebSocket.backend_singleton import get_backend_client + backend = get_backend_client() + + if backend: + message = { + "type": "connect_message", + "store_id": store_id, + "status": True, # 登录成功 + "cookies": self.cookies # 🔥 新增:添加登录生成的cookie信息 + } + self._log(f"准备发送登录成功消息: {message}", "DEBUG") + backend.send_message(message) + self._log("✅ 成功向后端发送登录成功通知", "SUCCESS") + else: + self._log("❌ 获取后端客户端失败", "ERROR") + except Exception as e: + self._log(f"❌ 发送登录成功通知失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + + def _send_login_failure_message(self, store_id, error_msg): + """向后端发送登录失败的通知""" + try: + self._log(f"开始发送登录失败通知,店铺ID: {store_id}, 错误: {error_msg}", "INFO") + from WebSocket.backend_singleton import get_backend_client + backend = get_backend_client() + + if backend: + message = { + "type": "connect_message", + "store_id": store_id, + "status": False, # 登录失败 + "content": error_msg # 官方返回的失败原因 + } + self._log(f"准备发送登录失败消息: {message}", "DEBUG") + backend.send_message(message) + self._log("✅ 成功向后端发送登录失败通知", "SUCCESS") + else: + self._log("❌ 获取后端客户端失败", "ERROR") + except Exception as e: + self._log(f"❌ 发送登录失败通知失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + + def request_verification_code(self, phone_number, store_id, original_phone=None): + """向抖音平台请求发送验证码""" + self._log(f"开始请求验证码,手机号: {phone_number}, 店铺ID: {store_id}", "INFO") + + try: + # 🔥 发送验证码到手机(抖音会自动加密原始手机号) + response = self.send_activation_code(phone_number) + self._log(f"发送验证码请求结果: {response.text}") + + # 发送消息给后端,告知需要验证码(使用原始手机号) + self._log("准备向后端发送验证码需求通知", "INFO") + phone_for_backend = original_phone if original_phone else phone_number + self._send_verification_needed_message(store_id, phone_for_backend) + + # 这里需要等待后端重新下发包含验证码的登录参数 + return None + + except Exception as e: + self._log(f"❌ 请求验证码失败: {e}", "ERROR") + self._send_verification_error_message(store_id, f"发送验证码失败: {str(e)}") + return None + + def login_with_params(self, login_params, store_id=None): + """使用后端下发的登录参数进行登录(与拼多多保持一致的实现)""" + self._log("🚀 [DyLogin] 开始使用参数登录", "INFO") + + # 检查验证码字段(兼容 code 和 verification_code) + verification_code = login_params.get("verification_code") or login_params.get("code", "") + phone_number = login_params.get("phone_number", "") + encrypted_phone = login_params.get("encrypted_phone", "") + + self._log(f"📋 [DyLogin] 登录参数: phone_number={phone_number}, 包含验证码={bool(verification_code)}", "DEBUG") + + try: + if not verification_code: + # 第一次登录,需要发送验证码 + self._log("检测到需要手机验证码,正在调用发送验证码方法", "INFO") + self._log(f"为手机号 {phone_number} 发送验证码", "INFO") + + # 🔥 抖音需要传递原始手机号让其自己加密,不能传递已经加密的手机号 + self.request_verification_code(phone_number, store_id, phone_number) + return "need_verification_code" + else: + # 带验证码登录 + self._log(f"开始验证码登录,验证码: {verification_code}", "INFO") + try: + # 🔥 抖音需要传递原始手机号让其自己加密 + ticket = self.verify(phone_number, verification_code) + self._log(f"✅ 验证码验证成功,获取到ticket", "SUCCESS") + + # 执行后续登录流程 + self.callback(ticket) + login_subject_uid, user_identity_id, encode_shop_id = self.subject_list() + self.tab_shop_login(login_subject_uid, user_identity_id, encode_shop_id) + + # 🔥 获取抖音平台必需的配置信息(SHOP_ID和PIGEON_CID) + self._log("🔄 开始获取抖音平台配置信息", "INFO") + shop_config = self.get_shop_config() + if shop_config: + # 将配置信息添加到cookies中 + self.cookies.update(shop_config) + self._log("🎉 登录成功!配置信息已获取", "SUCCESS") + # 🔥 发送登录成功通知给后端(与拼多多保持一致) + self._send_login_success_message(store_id) + return self.cookies + else: + error_msg = "获取抖音平台配置信息失败" + self._log(f"❌ {error_msg}", "ERROR") + self._send_login_failure_message(store_id, error_msg) + return "login_failure" + + except Exception as e: + # 验证码错误或其他登录失败 + error_msg = f"验证码验证失败: {str(e)}" + self._log(f"❌ {error_msg}", "ERROR") + self._send_verification_error_message(store_id, error_msg) + return "verification_code_error" + + except Exception as e: + # 登录过程中的其他错误 + error_msg = f"登录过程出错: {str(e)}" + self._log(f"❌ {error_msg}", "ERROR") + self._send_login_failure_message(store_id, error_msg) + return "login_failure" + + +# ===== 抖音登录相关类集成结束 ===== + + # 抖音WebSocket管理器类 class DouYinWebsocketManager: _instance = None @@ -100,53 +599,21 @@ class DouYinBackendService: return True async def send_message_to_backend(self, platform_message): - """改为通过单后端连接发送,需携带store_id""" + """🔥 改为通过单后端连接发送,与拼多多保持完全一致的逻辑""" try: from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() if not backend: return None - - # 从platform_message中构造统一上行结构,并附加store_id - body = platform_message.get('body', {}) if isinstance(platform_message, dict) else {} - sender_id = platform_message.get('sender', {}).get('id', '') if isinstance(platform_message, dict) else '' - - # 优先取消息内的store_id,其次取body内,再次退回当前会话store_id - store_id = (platform_message.get('store_id') - or body.get('store_id') - or self.current_store_id - or '') - - # 检查消息类型,如果是特殊类型(如staff_list),保持原格式 - message_type = platform_message.get('type', 'message') - - if message_type == 'staff_list': - # 对于客服列表消息,直接转发原始格式 - msg = platform_message.copy() - # 确保store_id正确 - msg['store_id'] = store_id - else: - # 对于普通消息,使用原有的格式转换逻辑 - msg_type = platform_message.get('msg_type', 'text') - content_for_backend = platform_message.get('content', '') - pin_image = platform_message.get('pin_image') - if not pin_image: - pin_image = "" - else: - pass - # 构造标准消息格式 - msg = { - 'type': 'message', - 'content': content_for_backend, - 'pin_image': pin_image, - 'msg_type': msg_type, - 'sender': {'id': sender_id}, - 'store_id': store_id - } - - backend.send_message(msg) - return None + # 🔥 确保消息包含store_id(与拼多多一致) + if isinstance(platform_message, dict): + if 'store_id' not in platform_message and self.current_store_id: + platform_message['store_id'] = self.current_store_id + + # 🔥 通过统一后端连接发送(与拼多多完全一致) + backend.send_message(platform_message) + return True except Exception: return None @@ -196,12 +663,12 @@ class DouYinMessageHandler: "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "x-secsdk-csrf-token": "0001000000017e890e18651b2ef5f6d36d0485a64cae6b0bfc36d69e27fdc20fe7d423670eba1861a5bcb5baaf40,a25cfb6098b498c33ee5f0a5dcafe47b" } - + # 打印实例创建信息 print(f"[DY Handler] 创建实例 {self.instance_id} for store {store_id}") def get_casl(self): - """获取可分配客服列表""" + """获取可分配客服列表 - 根据原始代码实现""" headers = { "authority": "pigeon.jinritemai.com", "accept": "application/json, text/plain, */*", @@ -226,16 +693,27 @@ class DouYinMessageHandler: "_ts": int(time.time() * 1000), "_pms": "1", "FUSION": "true", - "verifyFp": "", + "verifyFp": "", # 🔥 恢复为空字符串,因为原始代码中也是空的 "_v": "1.0.1.3585" } try: + self._log(f"🔄 正在获取客服列表,cookies包含字段: {list(self.cookie.keys())}", "DEBUG") + # 🔥 按照原始代码的方式处理响应 response = requests.get(url, headers=headers, cookies=self.cookie, params=params).json() + self._log(f"客服列表API响应内容: {response}", "DEBUG") + if response.get('code') == 0: - return response.get('data', []) - return None + staff_data = response.get('data', []) + self._log(f"✅ 成功获取客服列表,共 {len(staff_data)} 个客服", "SUCCESS") + return staff_data + else: + error_msg = response.get('message', '未知错误') + self._log(f"❌ 客服列表API返回错误: code={response.get('code')}, message={error_msg}", "ERROR") + return None except Exception as e: self._log(f"❌ 获取客服列表失败: {e}", "ERROR") + import traceback + self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") return None def transfer_conversation(self, receiver_id, shop_id, staff_id): @@ -282,18 +760,26 @@ class DouYinMessageHandler: try: # 获取客服列表 staff_list = self.get_casl() - if not staff_list: + if staff_list is None: self._log("⚠️ 获取客服列表失败", "WARNING") return False - # 转换客服数据格式 + # 🔥 处理空客服列表的情况(API成功但无客服) + if len(staff_list) == 0: + self._log("📋 当前无可分配客服,发送空列表到后端", "INFO") + # 继续处理,发送空列表给后端 + + # 转换客服数据格式 - 🔥 根据原始代码调试实际字段名 staff_infos = [] for staff in staff_list: + # 打印原始数据结构用于调试 + self._log(f"🔍 原始客服数据结构: {staff}", "DEBUG") + staff_info = StaffInfo( - staff_id=str(staff.get('staffId', '')), - name=staff.get('staffName', ''), - status=staff.get('status', 0), - department=staff.get('department', ''), + staff_id=str(staff.get('staffId', '') or staff.get('staff_id', '') or staff.get('id', '')), + name=staff.get('staffName', '') or staff.get('staff_name', '') or staff.get('name', ''), + status=str(staff.get('status', 0) or staff.get('state', 0)), + department=staff.get('department', '') or staff.get('dept', ''), online=staff.get('online', True) ) staff_infos.append(staff_info.to_dict()) @@ -312,9 +798,15 @@ class DouYinMessageHandler: # 发送到后端 await self.ai_service.send_message_to_backend(message_template.to_dict()) self._log(f"发送客服列表消息的结构体为: {message_template.to_json()}") - self._log(f"✅ [DY] 成功发送客服列表到后端,共 {len(staff_infos)} 个客服", "SUCCESS") - print(f"🔥 [DY] 客服列表已上传到后端: {len(staff_infos)} 个客服") - print(f"[DY] 客服详情: {[{'id': s['staff_id'], 'name': s['name']} for s in staff_infos]}") + + if len(staff_infos) > 0: + self._log(f"✅ [DY] 成功发送客服列表到后端,共 {len(staff_infos)} 个客服", "SUCCESS") + print(f"🔥 [DY] 客服列表已上传到后端: {len(staff_infos)} 个客服") + print(f"[DY] 客服详情: {[{'id': s['staff_id'], 'name': s['name']} for s in staff_infos]}") + else: + self._log(f"✅ [DY] 成功发送空客服列表到后端(当前无可分配客服)", "SUCCESS") + print(f"🔥 [DY] 空客服列表已上传到后端(当前无可分配客服)") + return True except Exception as e: @@ -603,26 +1095,28 @@ class DouYinMessageHandler: talk_id = user_info.get("talk_id", 0) p_id = user_info.get("p_id", 0) - # 准备发送者和接收者信息 - sender_info = { - "id": str(sender_id), - "name": f"用户_{sender_id}", - "is_customer": True - } - receiver_info = { - "id": self.store_id, - "name": "店铺客服", - "is_merchant": True - } + # 🔥 统一消息类型检测逻辑(与拼多多保持一致) + content = message_content + lc = str(content).lower() - # 创建消息模板 - message_template = PlatformMessage( - type="message", - content=message_content, - msg_type="text", - sender={"id": sender_info.get("id", "")}, + # 检测消息类型,使用与拼多多相同的逻辑 + if any(ext in lc for ext in [".jpg", ".jpeg", ".png", ".gif", ".webp"]): + msg_type = "image" + elif any(ext in lc for ext in [".mp4", ".avi", ".mov", ".wmv", ".flv"]): + msg_type = "video" + elif any(keyword in lc for keyword in ['goods.html', 'item.html', 'item.jd.com', '商品卡片id:']): + msg_type = "product_card" + else: + msg_type = "text" + + # 🔥 使用工厂方法创建消息模板(与拼多多保持一致) + message_template = PlatformMessage.create_text_message( + content=content, + sender_id=str(sender_id), store_id=self.store_id ) + # 动态设置检测到的消息类型 + message_template.msg_type = msg_type # 发送消息到后端(使用统一连接) await self.ai_service.send_message_to_backend(message_template.to_dict()) @@ -1034,7 +1528,6 @@ class DouYinMessageHandler: content_data = msg_content["8"] self._log(f"📄 原始内容数据: {content_data}", "DEBUG") - if message_dict and message_dict.get('text') != "客服水滴智能优品接入": self._log(f"💬 成功解析用户消息: '{message_dict}'", "SUCCESS") @@ -1195,22 +1688,61 @@ class DouYinMessageHandler: elif msg_type == 'text' and avatar_url: message_text = message_dict['text'] - # 准备发送者和接收者信息 - sender_info = { - "id": str(sender_id), - "name": f"用户_{sender_id}", - "is_customer": True - } + # 🔥 提取goods_info信息(与拼多多保持一致) + goods_info = {} + if msg_type == 'order': + # 从抖音的订单信息中提取goods_info + order_id = message_dict.get('order_id', '') + goods_id = self.get_goods_id(order_id) if hasattr(self, 'get_goods_id') else message_dict.get( + 'goods_id', '') + goods_info = { + 'goodsID': goods_id, + 'orderSequenceNo': order_id + } + elif msg_type == 'goods': + # 从抖音的商品信息中提取goods_info + goods_id = message_dict.get('goods_id', '') + goods_info = { + 'goodsID': goods_id + } - # 创建消息模板 - message_template = PlatformMessage( - type="message", - content=message_text, - pin_image=avatar_url, - msg_type=msg_type, - sender={"id": sender_info.get("id", "")}, - store_id=self.store_id + # 🔥 统一消息类型检测逻辑(与拼多多完全一致) + content = message_text + lc = str(content).lower() + + # 使用与拼多多完全相同的检测逻辑 + if any(ext in lc for ext in [".jpg", ".jpeg", ".png", ".gif", ".webp"]): + msg_type = "image" + elif any(ext in lc for ext in [".mp4", ".avi", ".mov", ".wmv", ".flv"]): + msg_type = "video" + else: + msg_type = "text" + + # 🔥 订单卡片组装(与拼多多完全一致) + if ("订单编号" in str(content) or msg_type == 'order') and goods_info: + content = f"商品id:{goods_info.get('goodsID')} 订单号:{goods_info.get('orderSequenceNo')}" + msg_type = "order_card" + + # 🔥 商品卡片检测(与拼多多完全一致) + elif any(keyword in lc for keyword in ['goods.html', 'item.html', 'item.jd.com', '商品卡片id:']) or \ + (goods_info and goods_info.get('goodsID') and not goods_info.get('orderSequenceNo')): + msg_type = "product_card" + + # 🔥 使用工厂方法创建消息模板(与拼多多保持一致) + message_template = PlatformMessage.create_text_message( + content=content, + sender_id=str(sender_id), + store_id=self.store_id, + pin_image=avatar_url ) + # 动态设置检测到的消息类型 + message_template.msg_type = msg_type + + # 🔥 添加调试日志(与拼多多保持一致) + try: + print(f"📤(SPEC) 发送到AI: {json.dumps(message_template.to_dict(), ensure_ascii=False)[:300]}...") + except Exception: + pass self._log("📤 准备发送消息到AI服务...", "INFO") self._log(f"📋 消息内容: {message_template.to_json()}", "DEBUG") @@ -1549,51 +2081,59 @@ class DouYinMessageHandler: async def send_message_external(self, receiver_id: str, content: str) -> bool: """外部调用的发送消息方法 - 用于后端消息转发""" try: - self._log(f"🔄 [External-{self.instance_id}] 收到转发请求: receiver_id={receiver_id}, content={content}", "INFO") - + self._log(f"🔄 [External-{self.instance_id}] 收到转发请求: receiver_id={receiver_id}, content={content}", + "INFO") + # 修复数据类型不匹配问题:将字符串转换为整数 try: receiver_id_int = int(receiver_id) - self._log(f"🔧 [External-{self.instance_id}] 转换 receiver_id: '{receiver_id}' -> {receiver_id_int}", "DEBUG") + self._log(f"🔧 [External-{self.instance_id}] 转换 receiver_id: '{receiver_id}' -> {receiver_id_int}", + "DEBUG") except ValueError: self._log(f"❌ [External-{self.instance_id}] receiver_id 无法转换为整数: {receiver_id}", "ERROR") return False - + # 调试信息:显示当前活跃用户 active_users = list(self.user_tokens.keys()) self._log(f"🔍 [External-{self.instance_id}] 当前活跃用户列表: {active_users}", "DEBUG") self._log(f"🔍 [External-{self.instance_id}] 活跃用户数量: {len(active_users)}", "DEBUG") - + # 检查用户是否存在于user_tokens中(使用整数类型) self._log(f"🔍 [External-{self.instance_id}] 调试信息:", "DEBUG") - self._log(f"🔍 [External-{self.instance_id}] receiver_id_int: {receiver_id_int} (类型: {type(receiver_id_int)})", "DEBUG") + self._log( + f"🔍 [External-{self.instance_id}] receiver_id_int: {receiver_id_int} (类型: {type(receiver_id_int)})", + "DEBUG") self._log(f"🔍 [External-{self.instance_id}] user_tokens keys: {list(self.user_tokens.keys())}", "DEBUG") if self.user_tokens: first_key = list(self.user_tokens.keys())[0] self._log(f"🔍 [External-{self.instance_id}] 第一个key: {first_key} (类型: {type(first_key)})", "DEBUG") self._log(f"🔍 [External-{self.instance_id}] 直接比较: {receiver_id_int == first_key}", "DEBUG") - + if receiver_id_int not in self.user_tokens: self._log(f"❌ [External-{self.instance_id}] 用户 {receiver_id_int} 不在活跃会话中", "WARNING") self._log(f"💡 [External-{self.instance_id}] 提示:用户需要先在抖音平台发送消息建立会话", "INFO") - + # 显示当前活跃用户的调试信息 self.print_active_users_debug() - + return False - + user_info = self.user_tokens[receiver_id_int] talk_id = user_info.get("talk_id") - p_id = user_info.get("p_id") + p_id = user_info.get("p_id") user_token = user_info.get("token") - - self._log(f"🔍 [External-{self.instance_id}] 用户会话信息: talk_id={talk_id}, p_id={p_id}, has_token={bool(user_token)}", "DEBUG") - + + self._log( + f"🔍 [External-{self.instance_id}] 用户会话信息: talk_id={talk_id}, p_id={p_id}, has_token={bool(user_token)}", + "DEBUG") + # 检查必要参数 if not talk_id or not p_id: - self._log(f"❌ [External-{self.instance_id}] 用户 {receiver_id_int} 缺少必要的会话信息 (talk_id: {talk_id}, p_id: {p_id})", "ERROR") + self._log( + f"❌ [External-{self.instance_id}] 用户 {receiver_id_int} 缺少必要的会话信息 (talk_id: {talk_id}, p_id: {p_id})", + "ERROR") return False - + if not user_token: self._log(f"⚠️ [External-{self.instance_id}] 用户 {receiver_id_int} token为空,尝试请求token", "WARNING") # 请求用户token @@ -1602,9 +2142,11 @@ class DouYinMessageHandler: if "pending_messages" not in user_info: user_info["pending_messages"] = [] user_info["pending_messages"].append(content) - self._log(f"📝 [External-{self.instance_id}] 消息已加入待发送队列,队列长度: {len(user_info['pending_messages'])}", "INFO") + self._log( + f"📝 [External-{self.instance_id}] 消息已加入待发送队列,队列长度: {len(user_info['pending_messages'])}", + "INFO") return True - + # 发送消息 (注意:_send_message_to_user 可能期望字符串类型的receiver_id) success = await self._send_message_to_user(receiver_id_int, talk_id, p_id, user_token, content) if success: @@ -1613,9 +2155,9 @@ class DouYinMessageHandler: self._log(f"✅ [External-{self.instance_id}] 消息转发成功", "SUCCESS") else: self._log(f"❌ [External-{self.instance_id}] 消息转发失败", "ERROR") - + return success - + except Exception as e: self._log(f"❌ [External-{self.instance_id}] 外部消息发送异常: {e}", "ERROR") self._log(f"❌ [External-{self.instance_id}] 错误详情: {traceback.format_exc()}", "DEBUG") @@ -1644,19 +2186,19 @@ class DouYinMessageHandler: try: active_users = self.get_active_users_info() self._log(f"🔍 [Debug-{self.instance_id}] 当前活跃用户总数: {len(active_users)}", "INFO") - + if not active_users: self._log(f"📝 [Debug-{self.instance_id}] 没有活跃用户。用户需要先在抖音平台发送消息建立会话。", "INFO") return - + for user_id, info in active_users.items(): self._log(f"👤 [Debug-{self.instance_id}] 用户 {user_id}:", "INFO") self._log(f" - Token: {'✅' if info['has_token'] else '❌'}", "INFO") - self._log(f" - Talk ID: {info['talk_id']}", "INFO") + self._log(f" - Talk ID: {info['talk_id']}", "INFO") self._log(f" - P ID: {info['p_id']}", "INFO") self._log(f" - 待发送消息: {info['pending_messages_count']}", "INFO") self._log(f" - 会话已初始化: {'✅' if info['session_initialized'] else '❌'}", "INFO") - + except Exception as e: self._log(f"打印调试信息失败: {e}", "ERROR") @@ -1835,9 +2377,11 @@ class DouYinListenerForGUI: if not message_handler_success: self._log("❌ 消息处理器启动失败", "ERROR") return False - + # 发送客服列表到后端 try: + # 🔥 等待一小段时间确保连接完全建立 + await asyncio.sleep(1) staff_list_success = await self.douyin_bot.message_handler.send_staff_list_to_backend() if staff_list_success: print(f"🔥 [DY] 客服列表已上传到后端") @@ -1846,13 +2390,13 @@ class DouYinListenerForGUI: except Exception as e: print(f"❌ [DY] 客服列表上传异常: {e}") self._log(f"❌ 抖音客服列表上传异常: {e}", "ERROR") - + # 注册到全局管理器 dy_manager = DouYinWebsocketManager() shop_key = f"抖音:{store_id}" dy_manager.on_connect(shop_key, self.douyin_bot, store_id=store_id, cookie=cookie_dict) self._log(f"✅ 已注册抖音连接: {shop_key}", "SUCCESS") - + # 创建一个长期运行的任务来保持监听状态 async def keep_running(): try: @@ -1860,10 +2404,29 @@ class DouYinListenerForGUI: await asyncio.sleep(1) except asyncio.CancelledError: pass - + # 在后台启动监听任务 asyncio.create_task(keep_running()) - + + # 🔥 新增:Cookie登录成功后发送登录成功报告(与登录参数模式保持一致) + try: + from WebSocket.backend_singleton import get_backend_client + backend = get_backend_client() + + if backend: + message = { + "type": "connect_message", + "store_id": store_id, + "status": True, # 登录成功 + "cookies": cookie_dict # 添加cookie信息 + } + backend.send_message(message) + self._log("✅ [DY] 已向后端发送Cookie登录成功报告", "SUCCESS") + else: + self._log("⚠️ [DY] 无法获取后端客户端,跳过状态报告", "WARNING") + except Exception as e: + self._log(f"⚠️ [DY] 发送登录成功报告失败: {e}", "WARNING") + self._log("✅ [DY] 抖音平台连接成功,开始监听消息", "SUCCESS") return True else: @@ -1876,6 +2439,69 @@ class DouYinListenerForGUI: self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") return False + async def start_with_login_params(self, store_id: str, login_params: str): + """使用后端下发的登录参数执行登录并启动监听(与拼多多保持一致)""" + try: + self._log("🔵 [DY] 收到后端登录参数,开始执行登录获取cookies", "INFO") + self._log(f"🔍 [DY] 登录参数内容: {login_params[:100]}...", "DEBUG") + + # 1. 解析登录参数 + self._log("🔄 [DY] 开始解析登录参数", "DEBUG") + params_dict = self._parse_login_params(login_params) + if not params_dict: + self._log("❌ [DY] 登录参数解析失败", "ERROR") + return False + self._log(f"✅ [DY] 登录参数解析成功,手机号: {params_dict.get('phone_number', 'N/A')}", "DEBUG") + + # 2. 使用新的DyLogin类执行登录 + self._log("🔄 [DY] 开始创建DyLogin实例", "DEBUG") + dy_login = DyLogin(log_callback=self.log_callback) + self._log("✅ [DY] DyLogin实例创建成功", "DEBUG") + + self._log("🔄 [DY] 开始执行登录", "DEBUG") + login_result = dy_login.login_with_params(params_dict, store_id) + self._log(f"📊 [DY] 登录结果: {login_result}", "DEBUG") + + if login_result == "need_verification_code": + self._log("⚠️ [DY] 需要手机验证码,已通知后端,等待重新下发包含验证码的登录参数", "WARNING") + return "need_verification_code" # 返回特殊标识,避免被覆盖 + elif login_result == "verification_code_error": + self._log("⚠️ [DY] 验证码错误,已通知后端", "WARNING") + return "verification_code_error" # 返回特殊标识,避免重复发送消息 + elif login_result == "login_failure": + self._log("⚠️ [DY] 登录失败,已发送失败通知给后端", "WARNING") + return "login_failure" # 返回特殊标识,避免重复发送消息 + elif not login_result: + self._log("❌ [DY] 登录失败", "ERROR") + return False + elif isinstance(login_result, dict): + # 登录成功,获取到cookies + self._log("✅ [DY] 登录成功,使用获取的cookies连接平台", "SUCCESS") + + # 🔥 使用获取的cookies启动监听(这里会验证cookies完整性) + return await self.start_with_cookies(store_id, login_result) + else: + self._log("❌ [DY] 登录返回未知结果", "ERROR") + return False + + except Exception as e: + self._log(f"❌ [DY] 使用登录参数启动失败: {str(e)}", "ERROR") + import traceback + self._log(f"🔍 [DY] 异常详细信息: {traceback.format_exc()}", "ERROR") + return False + + def _parse_login_params(self, login_params_str: str) -> dict: + """解析后端下发的登录参数(与拼多多保持一致)""" + try: + import json + data = json.loads(login_params_str) + params = data.get("data", {}) + self._log(f"✅ [DY] 登录参数解析成功: phone_number={params.get('phone_number', 'N/A')}", "INFO") + return params + except Exception as e: + self._log(f"❌ [DY] 解析登录参数失败: {e}", "ERROR") + return {} + def stop_listening(self): """停止监听""" if self.stop_event: diff --git a/Utils/Pdd/PddUtils.py b/Utils/Pdd/PddUtils.py index 4e7a6c4..b366cb6 100644 --- a/Utils/Pdd/PddUtils.py +++ b/Utils/Pdd/PddUtils.py @@ -40,7 +40,7 @@ from Utils.message_models import PlatformMessage class Track: """滑块轨迹生成类""" - + @staticmethod def get_track(distance): distance = int(distance) + random.randint(7, 11) + 0.8 @@ -853,7 +853,7 @@ class Track: class ImgDistance: """滑块图像识别距离计算类""" - + def __init__(self, bg, tp): self.bg = bg self.tp = tp @@ -999,8 +999,7 @@ class ImgDistance: save_path=save_path ) - -# AutiContent类已移除 - 后端会提供所有必要的anti_content + # AutiContent类已移除 - 后端会提供所有必要的anti_content def gzip_compress(self, data): """压缩数据""" @@ -1011,7 +1010,7 @@ class ImgDistance: return result except Exception as e: logger.error(f"调用gzipCompress函数失败: {e}") - + # fallback处理:简单返回原数据 logger.warning("使用fallback处理,返回原数据") return data @@ -1019,7 +1018,7 @@ class ImgDistance: class EncryptTool: """加密工具类""" - + @staticmethod def aes_encrypt(text, key, iv): cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8')) @@ -1059,7 +1058,7 @@ class EncryptTool: class PddLogin: """拼多多登录核心类""" - + def __init__(self, log_callback=None): super().__init__() self.login_api = "https://mms.pinduoduo.com/janus/api/auth" @@ -1608,11 +1607,11 @@ class PddLogin: def request_verification_code(self, username, store_id, backend_anti_content, phone_number=None): """向后端请求获取手机验证码""" self._log(f"开始请求验证码,用户名: {username}, 店铺ID: {store_id}, 手机号: {phone_number}", "INFO") - + # 使用后端下发的anti_content anti_content = backend_anti_content self._log(f"使用后端下发的anti_content", "DEBUG") - + self.headers["anti-content"] = anti_content url = "https://mms.pinduoduo.com/janus/api/user/getLoginVerificationCode" payload = { @@ -1621,11 +1620,11 @@ class PddLogin: } response = requests.post(url, headers=self.headers, json=payload, cookies=self.cookies) self._log(f"发送验证码请求结果: {response.text}") - + # 发送消息给后端,告知需要验证码(包含手机号) self._log("准备向后端发送验证码需求通知", "INFO") self._send_verification_needed_message(store_id, phone_number) - + # 这里需要等待后端重新下发包含验证码的登录参数 # 实际实现中这个方法会在接收到新的登录参数后被重新调用 return None @@ -1637,7 +1636,7 @@ class PddLogin: from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() self._log(f"获取到后端客户端: {backend is not None}", "DEBUG") - + if backend: message = { "type": "connect_message", @@ -1662,7 +1661,7 @@ class PddLogin: self._log(f"开始发送验证码错误通知,店铺ID: {store_id}, 错误: {error_msg}", "INFO") from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() - + if backend: message = { "type": "connect_message", @@ -1686,12 +1685,13 @@ class PddLogin: self._log(f"开始发送登录成功通知,店铺ID: {store_id}", "INFO") from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() - + if backend: message = { "type": "connect_message", "store_id": store_id, - "status": True # 登录成功 + "status": True, # 登录成功 + "cookies": self.cookies # 🔥 新增:添加登录生成的cookie信息 } self._log(f"准备发送登录成功消息: {message}", "DEBUG") backend.send_message(message) @@ -1709,7 +1709,7 @@ class PddLogin: self._log(f"开始发送登录失败通知,店铺ID: {store_id}, 错误: {error_msg}", "INFO") from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() - + if backend: message = { "type": "connect_message", @@ -1732,10 +1732,12 @@ class PddLogin: self._log("🚀 [PddLogin] 开始使用参数登录", "INFO") # 检查验证码字段(兼容 code 和 verification_code) verification_code = login_params.get("verification_code") or login_params.get("code", "") - self._log(f"📋 [PddLogin] 登录参数: username={login_params.get('username', 'N/A')}, 包含验证码={bool(verification_code)}", "DEBUG") - + self._log( + f"📋 [PddLogin] 登录参数: username={login_params.get('username', 'N/A')}, 包含验证码={bool(verification_code)}", + "DEBUG") + self.headers["anti-content"] = login_params.get("anti_content", "") - + # 直接使用后端提供的参数构建登录请求 ts = login_params.get("timestamp", int(round(time.time() * 1000))) payload = { @@ -1801,12 +1803,12 @@ class PddLogin: self._log(f"🔍 [Debug] 登录请求URL: {self.login_api}", "DEBUG") self._log(f"🔍 [Debug] 登录请求payload: {payload}", "DEBUG") self._log(f"🔍 [Debug] 登录请求headers: {dict(self.headers)}", "DEBUG") - + response = requests.post(self.login_api, headers=self.headers, json=payload, cookies=self.cookies) self.cookies.update(response.cookies.get_dict()) self._log(f"登录响应状态码: {response.status_code}") self._log(f"登录响应内容: {response.text}") - + # 检查响应内容 if "需要手机验证" in response.text: self._log("✅ 检测到需要手机验证的响应", "INFO") @@ -1820,7 +1822,8 @@ class PddLogin: salt = self.vc_pre_ck_b() # 获取生成aes key和iv 的密文值 pictures = self.obtain_captcha() # 获取验证码图片 - distance = round((ImgDistance(bg=pictures[0], tp=pictures[1]).main() * (272 / 320)) + (48.75 / 2), 2) # 计算距离 + distance = round((ImgDistance(bg=pictures[0], tp=pictures[1]).main() * (272 / 320)) + (48.75 / 2), + 2) # 计算距离 track_list = Track.get_track(distance=distance) # 生成轨迹 captcha_collect = self.captcha_collect(salt=salt, track_list=track_list) # 生成captcha_collect参数 @@ -1834,7 +1837,8 @@ class PddLogin: success_count += 1 # 如果滑块成功 success_count 计数一次 成功8次还是显示验证码则失败 返回False if success_count < 8: - return self.login_with_params(login_params=login_params, store_id=store_id, success_count=success_count) + return self.login_with_params(login_params=login_params, store_id=store_id, + success_count=success_count) else: return False else: @@ -1854,7 +1858,7 @@ class PddLogin: response_data = response.json() error_msg = response_data.get('errorMsg', '验证码验证失败') self._log(f"服务器返回错误: {error_msg}", "WARNING") - + # 不要重新发送验证码请求,直接报告验证失败 self._send_verification_error_message(store_id, error_msg) # 直接使用官方错误信息 return "verification_code_error" # 返回特殊状态,避免重复发送消息 @@ -1864,7 +1868,7 @@ class PddLogin: username = login_params.get("username") backend_anti_content = login_params.get("anti_content") self._log(f"为用户 {username} 发送验证码,使用后端anti_content", "INFO") - + # 🔥 从响应中提取手机号 phone_number = None try: @@ -1876,7 +1880,7 @@ class PddLogin: self._log("⚠️ 响应中的result字段不包含有效手机号", "WARNING") except Exception as e: self._log(f"❌ 提取手机号时出错: {e}", "DEBUG") - + # 传递后端下发的anti_content和手机号 self.request_verification_code(username, store_id, backend_anti_content, phone_number) return "need_verification_code" @@ -1896,6 +1900,7 @@ class PddLogin: self._send_login_failure_message(store_id, error_msg) return "login_failure" # 返回特殊状态,避免重复发送消息 + # ===== 登录相关类集成结束 ===== @@ -1983,13 +1988,13 @@ class ChatPdd: raise FileNotFoundError(f"找不到必需的JS文件: {file_path}") return True - + @staticmethod def _get_resource_path(relative_path): """获取资源文件的绝对路径(兼容PyInstaller打包环境)""" try: print(f"[DEBUG] 正在解析资源路径: {relative_path}") - + # PyInstaller环境下的基础路径 if hasattr(sys, '_MEIPASS'): # PyInstaller 临时目录 @@ -2005,10 +2010,10 @@ class ChatPdd: # 向上两级目录到项目根目录 base_path = os.path.dirname(os.path.dirname(base_path)) print(f"[DEBUG] 开发环境,计算的项目根目录: {base_path}") - + resource_path = os.path.join(base_path, relative_path) print(f"[DEBUG] 拼接后的完整资源路径: {resource_path}") - + # 检查路径是否存在 if os.path.exists(resource_path): print(f"[DEBUG] ✅ 资源路径存在: {resource_path}") @@ -2025,7 +2030,7 @@ class ChatPdd: print(f"[DEBUG] 📄 {item}") except Exception as e: print(f"[DEBUG] 无法列出目录内容: {e}") - + return resource_path except Exception as e: print(f"[ERROR] 获取资源路径失败: {e}") @@ -2554,12 +2559,12 @@ class ChatPdd: """专门判断是否为机器人消息需要过滤""" try: message_info = message_data.get("message", {}) - + # 1. 基于消息类型过滤机器人特殊消息 msg_type = message_info.get("type") if msg_type == 31: # 机器人干预消息(如:机器人已暂停接待) return True - + # 2. 基于模板名称识别机器人消息 template_name = message_info.get("template_name", "") robot_templates = [ @@ -2569,14 +2574,14 @@ class ChatPdd: ] if template_name in robot_templates: return True - + # 3. 基于机器人特殊标志过滤 if message_info.get("conv_silent") is True: # 静默会话标志 return True - + if message_info.get("no_unreply_hint") == 1: # 无需回复提示标志 return True - + # 4. 基于消息内容识别机器人提示消息 content = message_info.get("content", "") robot_content_patterns = [ @@ -2586,18 +2591,18 @@ class ChatPdd: "点击添加", "[当前用户来自", ] - + if any(pattern in content for pattern in robot_content_patterns): return True - + # 5. 基于biz_context中的机器人标识 biz_context = message_info.get("biz_context", {}) if biz_context.get("robot_msg_id"): # 有机器人消息ID return True - + # 不是机器人消息,不过滤 return False - + except Exception as e: self._log(f"判断机器人消息时出错: {e}", "DEBUG") return False # 出错时不过滤,保持原有行为 @@ -2628,7 +2633,7 @@ class ChatPdd: if self.should_filter_robot_message(message_data): self._log("🤖 检测到机器人消息,已过滤不发送给后端", "DEBUG") return - + message_info = message_data.get("message", {}) if not message_info: return @@ -2801,6 +2806,28 @@ class ChatPdd: except Exception as e: self._log(f"❌ 获取或发送客服列表失败: {e}", "ERROR") + # 🔥 新增:Cookie登录成功后发送登录成功报告(与登录参数模式保持一致) + try: + if self.backend_service and hasattr(self, 'store_id') and self.store_id: + # 构建cookie字典(从cookies_str解析) + cookie_dict = {} + if hasattr(self, 'cookie') and self.cookie: + cookie_dict = self.cookie + + message = { + "type": "connect_message", + "store_id": self.store_id, + "status": True, # 登录成功 + "cookies": cookie_dict # 添加cookie信息 + } + # 🔥 修复:使用正确的方法名 send_message_to_backend + await self.backend_service.send_message_to_backend(message) + self._log("✅ [PDD] 已向后端发送Cookie登录成功报告", "SUCCESS") + else: + self._log("⚠️ [PDD] 无法发送登录成功报告:backend_service或store_id缺失", "WARNING") + except Exception as e: + self._log(f"⚠️ [PDD] 发送登录成功报告失败: {e}", "WARNING") + # 启动消息监听和心跳 await asyncio.gather( self.heartbeat(websocket), @@ -2959,11 +2986,11 @@ class PddListenerForGUI: self._log("🔄 [PDD] 开始创建PddLogin实例", "DEBUG") pdd_login = PddLogin(log_callback=self.log_callback) self._log("✅ [PDD] PddLogin实例创建成功", "DEBUG") - + self._log("🔄 [PDD] 开始执行登录", "DEBUG") login_result = pdd_login.login_with_params(params_dict, store_id) self._log(f"📊 [PDD] 登录结果: {login_result}", "DEBUG") - + if login_result == "need_verification_code": self._log("⚠️ [PDD] 需要手机验证码,已通知后端,等待重新下发包含验证码的登录参数", "WARNING") return "need_verification_code" # 返回特殊标识,避免被覆盖 @@ -2979,7 +3006,7 @@ class PddListenerForGUI: elif isinstance(login_result, dict): # 登录成功,获取到cookies self._log("✅ [PDD] 登录成功,使用获取的cookies连接平台", "SUCCESS") - + # 将cookies字典转换为字符串格式,与原有逻辑兼容 import json cookies_str = json.dumps(login_result) @@ -3080,6 +3107,7 @@ class PddListenerForGUI: cookies_str=cookies, store=store ) + self._log("✅ [PDD] 拼多多平台连接成功,开始监听消息", "SUCCESS") return True except Exception as e: @@ -3125,8 +3153,6 @@ class PddListenerForGUI: self._log(f"❌ [PDD] 解析登录参数失败: {e}", "ERROR") return {} - - def get_status(self) -> Dict[str, Any]: return { "running": self.running, diff --git a/WebSocket/BackendClient.py b/WebSocket/BackendClient.py index 95bf09c..31972bd 100644 --- a/WebSocket/BackendClient.py +++ b/WebSocket/BackendClient.py @@ -30,6 +30,7 @@ class BackendClient: self.login_callback: Optional[Callable] = None # 新增:平台登录(下发cookies)回调 self.success_callback: Optional[Callable] = None # 新增:后端连接成功回调 self.token_error_callback: Optional[Callable] = None # 新增:token错误回调 + self.version_callback: Optional[Callable] = None # 新增:版本检查回调 self.is_connected = False @@ -100,15 +101,15 @@ class BackendClient: ping_timeout=WS_PING_TIMEOUT, # 可配置ping超时 close_timeout=10, # 10秒关闭超时 # 增加TCP keepalive配置 - max_size=2**20, # 1MB最大消息大小 - max_queue=32, # 最大队列大小 + max_size=2 ** 20, # 1MB最大消息大小 + max_queue=32, # 最大队列大小 compression=None # 禁用压缩以提高性能 ) print(f"[连接] 已启用心跳:ping_interval={WS_PING_INTERVAL}s, ping_timeout={WS_PING_TIMEOUT}s") else: self.websocket = await websockets.connect( self.url, - max_size=2**20, + max_size=2 ** 20, max_queue=32, compression=None ) @@ -118,13 +119,13 @@ class BackendClient: self.reconnect_attempts = 0 # 重置重连计数 self.is_reconnecting = False print("后端WebSocket连接成功") - + # 等待连接稳定后再发送状态通知 await asyncio.sleep(0.5) - + # 发送连接状态通知给后端 self._notify_connection_status(True) - + self.on_connected() # 消息循环 @@ -144,7 +145,7 @@ class BackendClient: except websockets.ConnectionClosed as e: self.is_connected = False self._notify_connection_status(False) # 通知断开 - + # 详细分析断开原因 if e.code == 1006: print(f"[重连] WebSocket异常关闭 (1006): 可能是心跳超时或网络问题") @@ -154,7 +155,7 @@ class BackendClient: print(f"[重连] WebSocket关闭 (1001): 端点离开") else: print(f"[重连] WebSocket关闭 ({e.code}): {e.reason}") - + self._handle_connection_closed(e) if not await self._should_reconnect(): break @@ -263,7 +264,8 @@ class BackendClient: error: Callable = None, login: Callable = None, success: Callable = None, - token_error: Callable = None): + token_error: Callable = None, + version: Callable = None): """设置各种消息类型的回调函数""" if store_list: self.store_list_callback = store_list @@ -283,6 +285,8 @@ class BackendClient: self.success_callback = success if token_error: self.token_error_callback = token_error + if version: + self.version_callback = version def on_connected(self): """连接成功时的处理""" @@ -337,14 +341,14 @@ class BackendClient: try: if not self.loop: return - + # 获取当前活跃平台的store_id active_store_id = None try: from Utils.JD.JdUtils import WebsocketManager as JdManager - from Utils.Dy.DyUtils import DouYinWebsocketManager as DyManager + from Utils.Dy.DyUtils import DouYinWebsocketManager as DyManager from Utils.Pdd.PddUtils import WebsocketManager as PddManager - + # 检查各平台是否有活跃连接 for mgr_class, platform_name in [(JdManager, "京东"), (DyManager, "抖音"), (PddManager, "拼多多")]: try: @@ -365,30 +369,30 @@ class BackendClient: continue except Exception as e: print(f"[状态] 获取活跃平台信息失败: {e}") - + status_message = { "type": "connection_status", "status": connected, "timestamp": int(time.time()), "client_uuid": self.uuid } - + # 如果有活跃平台,添加store_id if active_store_id: status_message["store_id"] = active_store_id print(f"[状态] 添加store_id到状态消息: {active_store_id}") else: print(f"[状态] 未检测到活跃平台,不添加store_id") - + # 异步发送状态通知 asyncio.run_coroutine_threadsafe( self._send_to_backend(status_message), self.loop ) - + status_text = "连接" if connected else "断开" print(f"[状态] 已通知后端GUI客户端{status_text}") - + except Exception as e: print(f"[状态] 发送状态通知失败: {e}") import traceback @@ -437,6 +441,8 @@ class BackendClient: self._handle_token_error(message) elif msg_type == 'staff_list': self._handle_staff_list(message) + elif msg_type == 'version_response': # 新增:版本检查响应 + self._handle_version_response(message) else: print(f"未知消息类型: {msg_type}") @@ -1151,12 +1157,14 @@ class BackendClient: content = message.get('content', '') data = message.get('data', {}) - # 判断是拼多多登录参数还是普通Cookie - if platform_name == "拼多多" and ("拼多多登录" in content) and data.get('login_params'): - # 拼多多登录参数模式 - 传递完整的消息JSON给处理器 - print(f"收到拼多多登录参数: 平台={platform_name}, 店铺={store_id}, 类型={content}") + # 🔥 判断是登录参数模式还是普通Cookie模式(支持拼多多和抖音) + if (platform_name in ["拼多多", "抖音"] and + (("拼多多登录" in content and data.get('login_params')) or + ("抖音登录" in content and data.get('login_flow')))): + # 登录参数模式 - 传递完整的消息JSON给处理器 + print(f"收到{platform_name}登录参数: 平台={platform_name}, 店铺={store_id}, 类型={content}") if self.login_callback: - # 传递完整的JSON消息,让拼多多处理器来解析login_params + # 传递完整的JSON消息,让处理器来解析登录参数 import json full_message = json.dumps(message) self.login_callback(platform_name, store_id, full_message) @@ -1170,13 +1178,13 @@ class BackendClient: """处理错误消息""" error_msg = message.get('error', '未知错误') content = message.get('content', '') - + # 检查是否为token错误(无论type是error还是error_token) if content == "无效的exe_token" or "无效的exe_token" in content: print(f"[错误] 检测到token错误: {content}") self._handle_token_error(message) return - + print(f"后端连接错误: {error_msg}") if self.error_callback: self.error_callback(error_msg, message) @@ -1185,15 +1193,15 @@ class BackendClient: """处理token错误消息 - 无效token时停止重连并显示错误""" error_content = message.get('content', '无效的exe_token') print(f"[错误] Token验证失败: {error_content}") - + # 停止重连机制 self.should_stop = True self.is_reconnecting = False - + # 触发token错误回调 if self.token_error_callback: self.token_error_callback(error_content) - + # 主动关闭连接 if self.websocket: asyncio.run_coroutine_threadsafe(self.websocket.close(), self.loop) @@ -1211,6 +1219,16 @@ class BackendClient: if self.customers_callback: # 假设客服列表更新也触发客服列表回调 self.customers_callback(staff_list, store_id) + def _handle_version_response(self, message: Dict[str, Any]): + """处理版本检查响应""" + latest_version = message.get('latest_version') + download_url = message.get('download_url') + + print(f"收到版本检查响应: 最新版本={latest_version}, 下载地址={download_url}") + + if self.version_callback: + self.version_callback(message) + # ==================== 辅助方法 ==================== def set_token(self, token: str): diff --git a/WebSocket/backend_singleton.py b/WebSocket/backend_singleton.py index 581d39f..eabcd13 100644 --- a/WebSocket/backend_singleton.py +++ b/WebSocket/backend_singleton.py @@ -27,9 +27,15 @@ class WebSocketManager: """WebSocket连接管理器,统一处理后端连接和平台监听""" def __init__(self): + # 后端客户端 self.backend_client = None + self.is_connected = False + + # 版本检查器 + self.version_checker = None + self.gui_update_callback = None self.platform_listeners = {} # 存储各平台的监听器 - self.connected_platforms = [] # 存储已连接的平台列表 # <- 新增 + self.connected_platforms = [] # 存储已连接的平台列表 # <- 新增 self.callbacks = { 'log': None, 'success': None, @@ -75,7 +81,6 @@ class WebSocketManager: """连接后端WebSocket""" try: - # 1 保存token到配置 try: from config import set_saved_token @@ -98,13 +103,15 @@ class WebSocketManager: else: # 3 如果有客户端更新token并重连 backend.set_token(token) - + # 设置回调函数 def _on_backend_success(): try: self._log("连接服务成功", "SUCCESS") if self.callbacks['success']: self.callbacks['success']() + # 启动版本检查器 + self._start_version_checker() except Exception as e: self._log(f"成功回调执行失败: {e}", "ERROR") @@ -119,11 +126,12 @@ class WebSocketManager: if self.callbacks['token_error']: self.callbacks['token_error'](error_content) - backend.set_callbacks(success=_on_backend_success, login=_on_backend_login, token_error=_on_token_error) - + backend.set_callbacks(success=_on_backend_success, login=_on_backend_login, + token_error=_on_token_error) + if not backend.is_connected: backend.connect() - + self.backend_client = backend self._log("令牌已提交,已连接后端。等待后端下发平台cookies后自动连接平台...", "SUCCESS") return True @@ -144,6 +152,8 @@ class WebSocketManager: self._log("连接服务成功", "SUCCESS") if self.callbacks['success']: self.callbacks['success']() + # 启动版本检查器 + self._start_version_checker() except Exception as e: self._log(f"成功回调执行失败: {e}", "ERROR") @@ -176,7 +186,7 @@ class WebSocketManager: self._log(f"🔄 检测到店铺 {store_id} 重连,清理 {len(keys_to_remove)} 个旧连接", "INFO") for key in keys_to_remove: self.platform_listeners.pop(key, None) - + # 平台名称映射 platform_map = { "淘宝": "千牛", @@ -220,6 +230,27 @@ class WebSocketManager: import traceback self._log(f"错误详情: {traceback.format_exc()}", "DEBUG") + def _start_version_checker(self): + """启动版本检查器""" + try: + from version_checker import VersionChecker + self.version_checker = VersionChecker( + backend_client=self.backend_client, + update_callback=self._on_update_available + ) + self.backend_client.version_callback = self.version_checker.handle_version_response + self.version_checker.start() + self._log("✅ 版本检查器已启动,将每10分钟检查一次更新", "SUCCESS") + except Exception as e: + self._log(f"❌ 启动版本检查器失败: {e}", "ERROR") + + def _on_update_available(self, latest_version, download_url): + """发现新版本时的处理""" + self._log(f"🔔 发现新版本 {latest_version}", "INFO") + # 通知主GUI显示更新提醒 + if hasattr(self, 'gui_update_callback') and self.gui_update_callback: + self.gui_update_callback(latest_version, download_url) + def _start_jd_listener(self, store_id: str, cookies: str): """启动京东平台监听""" try: @@ -275,15 +306,59 @@ class WebSocketManager: def _runner(): try: import json + self._log("🚀 开始创建抖音监听器实例", "DEBUG") listener = DYListenerForGUI_WS() - # 将JSON字符串格式的cookies解析为字典 - try: - cookie_dict = json.loads(cookies) if isinstance(cookies, str) else cookies - except json.JSONDecodeError as e: - self._log(f"❌ Cookie JSON解析失败: {e}", "ERROR") - return False + self._log("✅ 抖音监听器实例创建成功", "DEBUG") + + # 🔥 检查是否为登录参数模式(与拼多多保持一致) + if cookies and ('"login_flow"' in cookies or '"phone_number"' in cookies): + # 使用登录参数模式 + self._log("📋 使用登录参数启动抖音监听器", "INFO") + self._log("🔄 开始执行 start_with_login_params", "DEBUG") + result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=cookies)) + self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG") + + # 🔥 详细的结果分析(与拼多多完全一致) + if result == "need_verification_code": + self._log("✅ [DY] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS") + elif result == "verification_code_error": + self._log("⚠️ [DY] 验证码错误,已发送错误通知给后端", "WARNING") + elif result: + self._log("✅ [DY] 登录成功,平台连接已建立", "SUCCESS") + self._notify_platform_connected("抖音") + else: + self._log("❌ [DY] 登录失败", "ERROR") + else: + # 传统cookie模式(保留兼容性) + self._log("🍪 使用Cookie启动抖音监听器", "INFO") + self._log("🔄 开始执行 start_with_cookies", "DEBUG") + try: + # 🔥 修复:尝试JSON解析,失败时用ast.literal_eval解析Python字典字符串 + if isinstance(cookies, str): + try: + cookie_dict = json.loads(cookies) + except json.JSONDecodeError: + # 后端发送的是Python字典字符串格式,使用ast.literal_eval + import ast + cookie_dict = ast.literal_eval(cookies) + self._log("✅ 使用ast.literal_eval成功解析cookies", "DEBUG") + else: + cookie_dict = cookies + except (json.JSONDecodeError, ValueError, SyntaxError) as e: + self._log(f"❌ Cookie解析失败: {e}", "ERROR") + return False + result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookie_dict=cookie_dict)) + self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG") + + # Cookie启动成功时也要通知GUI + if result: + self._log("✅ [DY] Cookie启动成功,平台连接已建立", "SUCCESS") + self._notify_platform_connected("抖音") + + # 🔥 移除:不再在backend_singleton中发送connect_message + # 抖音的连接状态报告应该在DyUtils中的DyLogin类中发送,与拼多多保持一致 + # 所有特殊状态通知都已经在DyLogin中发送过了,这里不需要重复发送 - result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookie_dict=cookie_dict)) return result except Exception as e: self._log(f"抖音监听器运行异常: {e}", "ERROR") @@ -304,34 +379,13 @@ class WebSocketManager: if f"抖音:{store_id}" in self.platform_listeners: self.platform_listeners[f"抖音:{store_id}"]['status'] = 'success' - # 上报连接状态给后端 - if self.backend_client: - try: - self.backend_client.send_message({ - "type": "connect_message", - "store_id": store_id, - "status": True - }) - self._log("已上报抖音平台连接状态: 成功", "INFO") - except Exception as e: - self._log(f"上报抖音平台连接状态失败: {e}", "WARNING") - self._log("已启动抖音平台监听", "SUCCESS") - self._notify_platform_connected("抖音") # ← 新增 except Exception as e: self._log(f"启动抖音平台监听失败: {e}", "ERROR") - # 确保失败时也上报状态 - if self.backend_client: - try: - self.backend_client.send_message({ - "type": "connect_message", - "store_id": store_id, - "status": False - }) - except Exception as send_e: - self._log(f"失败状态下报抖音平台连接状态也失败: {send_e}", "ERROR") + # 🔥 移除:确保失败时也不在这里上报状态 + # 失败状态应该在DyLogin中处理,与拼多多保持一致 def _start_qianniu_listener(self, store_id: str, cookies: str): """启动千牛平台监听(单连接多店铺架构)""" @@ -401,7 +455,7 @@ class WebSocketManager: self._log("🚀 开始创建拼多多监听器实例", "DEBUG") listener = PDDListenerForGUI_WS(log_callback=self._log) self._log("✅ 拼多多监听器实例创建成功", "DEBUG") - + # 判断是登录参数还是Cookie if self._is_pdd_login_params(data): # 使用登录参数启动 @@ -409,7 +463,7 @@ class WebSocketManager: self._log("🔄 开始执行 start_with_login_params", "DEBUG") result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=data)) self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG") - + # 详细的结果分析 if result == "need_verification_code": self._log("✅ [PDD] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS") @@ -426,14 +480,15 @@ class WebSocketManager: self._log("🔄 开始执行 start_with_cookies", "DEBUG") result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookies=data)) self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG") - + # Cookie启动成功时也要通知GUI if result: self._log("✅ [PDD] Cookie启动成功,平台连接已建立", "SUCCESS") self._notify_platform_connected("拼多多") - + # 根据实际登录结果上报状态给后端 - if self.backend_client and result not in ["need_verification_code", "verification_code_error", "login_failure"]: + if self.backend_client and result not in ["need_verification_code", "verification_code_error", + "login_failure"]: # 如果是特殊状态,说明通知已经在PddLogin中发送了,不需要重复发送 try: message = { @@ -443,7 +498,8 @@ class WebSocketManager: } self.backend_client.send_message(message) status_text = "成功" if result else "失败" - self._log(f"上报拼多多平台连接状态{status_text}: {message}", "SUCCESS" if result else "ERROR") + self._log(f"上报拼多多平台连接状态{status_text}: {message}", + "SUCCESS" if result else "ERROR") except Exception as send_e: self._log(f"上报拼多多平台连接状态失败: {send_e}", "ERROR") elif result == "need_verification_code": @@ -452,9 +508,9 @@ class WebSocketManager: self._log("验证码错误,错误通知已由PddLogin发送,等待后端处理", "INFO") elif result == "login_failure": self._log("登录失败,失败通知已由PddLogin发送,等待后端处理", "INFO") - + return result - + except Exception as e: self._log(f"拼多多监听器运行异常: {e}", "ERROR") import traceback @@ -509,24 +565,24 @@ class WebSocketManager: try: self._log(f"🔍 [DEBUG] 检查是否为登录参数,数据长度: {len(data)}", "DEBUG") self._log(f"🔍 [DEBUG] 数据前100字符: {data[:100]}", "DEBUG") - + import json parsed_data = json.loads(data) self._log(f"🔍 [DEBUG] JSON解析成功,键: {list(parsed_data.keys())}", "DEBUG") - + login_params = parsed_data.get("data", {}).get("login_params", {}) self._log(f"🔍 [DEBUG] login_params存在: {bool(login_params)}", "DEBUG") - + if not login_params: self._log("🔍 [DEBUG] login_params为空,返回False", "DEBUG") return False - + # 检查必需的登录参数字段 required_fields = ["username", "password", "anti_content", "risk_sign", "timestamp"] has_all_fields = all(field in login_params for field in required_fields) self._log(f"🔍 [DEBUG] 包含所有必需字段: {has_all_fields}", "DEBUG") self._log(f"🔍 [DEBUG] 现有字段: {list(login_params.keys())}", "DEBUG") - + return has_all_fields except Exception as e: self._log(f"🔍 [DEBUG] 解析失败: {e}", "DEBUG") diff --git a/config.py b/config.py index 2d9e57d..6a88fb5 100644 --- a/config.py +++ b/config.py @@ -9,13 +9,13 @@ import json # 用于将令牌保存为 JSON 格式 # 后端服务器配置 # BACKEND_HOST = "192.168.5.233" -BACKEND_HOST = "192.168.5.12" -# BACKEND_HOST = "shuidrop.com" +# BACKEND_HOST = "192.168.5.12" +BACKEND_HOST = "shuidrop.com" # BACKEND_HOST = "test.shuidrop.com" -BACKEND_PORT = "8000" -# BACKEND_PORT = "" -BACKEND_WS_URL = f"ws://{BACKEND_HOST}:{BACKEND_PORT}" -# BACKEND_WS_URL = f"wss://{BACKEND_HOST}" +# BACKEND_PORT = "8000" +BACKEND_PORT = "" +# BACKEND_WS_URL = f"ws://{BACKEND_HOST}:{BACKEND_PORT}" +BACKEND_WS_URL = f"wss://{BACKEND_HOST}" # WebSocket配置 WS_CONNECT_TIMEOUT = 16.0 diff --git a/main.py b/main.py index e9f42c4..eceea62 100644 --- a/main.py +++ b/main.py @@ -43,6 +43,9 @@ class LoginWindow(QMainWindow): self.initUI() + # 延迟设置版本检查器,确保WebSocket连接已建立 + QTimer.singleShot(5000, self.setup_version_checker) + def initUI(self): # 设置窗口基本属性 self.setWindowTitle('AI客服智能助手') @@ -406,22 +409,22 @@ class LoginWindow(QMainWindow): """处理token错误 - 显示红色错误信息并停止所有操作""" try: self.add_log(f"Token验证失败: {error_content}", "ERROR") - + # 在状态标签显示红色错误信息 self.status_label.setText(f"🔴 {error_content}") self.status_label.setStyleSheet( "color: #dc3545; background: rgba(220, 53, 69, 0.1); border-radius: 12px; padding: 5px 10px; font-weight: bold;") - + # 重置按钮状态 self.login_btn.setEnabled(True) self.login_btn.setText("重新连接") self.login_btn.setObjectName("loginButton") # 恢复原始样式 - + # 清空已连接平台列表 self.connected_platforms.clear() - + self.add_log("由于token无效,已停止所有连接操作", "ERROR") - + except Exception as e: self.add_log(f"处理token错误失败: {e}", "ERROR") @@ -699,6 +702,44 @@ class LoginWindow(QMainWindow): self.quit_application() event.accept() + def setup_version_checker(self): + """设置版本检查器的GUI回调""" + try: + from WebSocket.backend_singleton import get_websocket_manager + ws_manager = get_websocket_manager() + if ws_manager: + ws_manager.gui_update_callback = self.show_update_notification + self.add_log("✅ 版本检查器GUI回调已设置", "SUCCESS") + else: + self.add_log("⚠️ WebSocket管理器未初始化", "WARNING") + except Exception as e: + self.add_log(f"❌ 设置版本检查器失败: {e}", "ERROR") + + def show_update_notification(self, latest_version, download_url): + """显示版本更新通知""" + try: + reply = QMessageBox.question( + self, + "版本更新", + f"发现新版本 {latest_version},是否立即更新?\n\n点击确定将打开下载页面。", + QMessageBox.Yes | QMessageBox.No, + QMessageBox.Yes + ) + + if reply == QMessageBox.Yes: + self.trigger_update(download_url) + except Exception as e: + self.add_log(f"❌ 显示更新通知失败: {e}", "ERROR") + + def trigger_update(self, download_url): + """触发更新下载""" + import webbrowser + try: + webbrowser.open(download_url) + self.add_log("✅ 已打开更新下载页面", "SUCCESS") + except Exception as e: + self.add_log(f"❌ 打开下载页面失败: {e}", "ERROR") + def main(): """主函数,用于测试界面""" diff --git a/static/ai_assistant_icon_uninstall.png b/static/ai_assistant_icon_uninstall.png new file mode 100644 index 0000000..201b9d8 Binary files /dev/null and b/static/ai_assistant_icon_uninstall.png differ