Commit 9d7cb84d authored by 邱阿朋's avatar 邱阿朋

refactor(app): 重构 SPA报表下载功能

- 修改 __get_report_table_html 函数,增加重试逻辑,优化异常处理- 重命名 __get_report_table_html 为 __get_report_table_links,提高代码可读性
- 优化日志记录,增加更多详细信息
- 移除 tool_gui.py 中的 cleanup_resources 函数调用
parent fc0de0cd
...@@ -32,8 +32,13 @@ class Spa(AutoInterface): ...@@ -32,8 +32,13 @@ class Spa(AutoInterface):
full_url = host + url full_url = host + url
self.page.get(full_url, timeout=10) self.page.get(full_url, timeout=10)
def __get_report_table_html(self, invoice_id): def __get_report_table_links(self, invoice_id):
retry_count = 0
while True: while True:
if retry_count > 5:
self.logger.warning(f"{invoice_id} 获取报表链接重试次数超过{retry_count}次,请检查发票号")
return None
try: try:
self.__page_get(f"hz/vendor/members/coop?searchText={invoice_id}") self.__page_get(f"hz/vendor/members/coop?searchText={invoice_id}")
# 点击选项卡 # 点击选项卡
...@@ -42,16 +47,25 @@ class Spa(AutoInterface): ...@@ -42,16 +47,25 @@ class Spa(AutoInterface):
# 点击下载报表选项 # 点击下载报表选项
# self.page.ele(f"#invoiceDownloads-{invoice_id}_2").click() # self.page.ele(f"#invoiceDownloads-{invoice_id}_2").click()
self.page.ele('xpath://li[a[contains(text(), "Backup report")]]/a').click() self.page.ele('xpath://li[a[contains(text(), "Backup report")]]/a').click()
self.page.wait(1) self.page.wait(2)
# 获取报表表单内容 # 获取报表表单内容
report_table_html = self.page.ele("#backup-report-table").html report_table_html = self.page.ele("#backup-report-table").html
if report_table_html is None or report_table_html == "": if report_table_html is None or report_table_html == "":
self.logger.warning("表单内容为空,刷新网页") self.logger.warning("表单内容为空,刷新网页")
self.page.refresh() self.page.refresh()
continue continue
return report_table_html
tree = etree.HTML(report_table_html)
links = tree.xpath('//table[@id="backup-report-table"]//a/@href')
if len(links) == 0:
self.logger.warning(f"{invoice_id} 未获取到下载连接,开始重试...")
retry_count += 1
continue
return links
except ElementNotFoundError: except ElementNotFoundError:
self.logger.warning("元素未找到,刷新网页") self.logger.warning(f"{invoice_id} 元素未找到,刷新网页")
self.page.refresh() self.page.refresh()
def __export_item_read_data(self, invoice_id): def __export_item_read_data(self, invoice_id):
...@@ -60,14 +74,12 @@ class Spa(AutoInterface): ...@@ -60,14 +74,12 @@ class Spa(AutoInterface):
df = pd.read_excel(file_name, sheet_name=None) df = pd.read_excel(file_name, sheet_name=None)
return df return df
# 获取报表表单内容 # 获取报表表单所有链接
report_table_html = self.__get_report_table_html(invoice_id) links = self.__get_report_table_links(invoice_id)
tree = etree.HTML(report_table_html)
# 提取所有链接
links = tree.xpath('//table[@id="backup-report-table"]//a/@href')
if len(links) == 0: if len(links) == 0:
# data_list = get_report_agreement_text(invoice_id) # data_list = get_report_agreement_text(invoice_id)
# return {"Accrual For Current Period": pd.DataFrame(data_list)} # return {"Accrual For Current Period": pd.DataFrame(data_list)}
self.logger.warning(f"{invoice_id} 没有下载连接")
return None return None
for link in links: for link in links:
...@@ -90,6 +102,7 @@ class Spa(AutoInterface): ...@@ -90,6 +102,7 @@ class Spa(AutoInterface):
header_is_normal = any(column in headers for column in column_names_to_check) header_is_normal = any(column in headers for column in column_names_to_check)
if not header_is_normal: if not header_is_normal:
os.remove(report_file) os.remove(report_file)
self.logger.warning(f"{invoice_id} 文件格式不满足要求")
continue continue
save_report_file = os.path.join(os.getcwd(), "spa", str(invoice_id), "BackupReport.xls") save_report_file = os.path.join(os.getcwd(), "spa", str(invoice_id), "BackupReport.xls")
...@@ -103,8 +116,8 @@ class Spa(AutoInterface): ...@@ -103,8 +116,8 @@ class Spa(AutoInterface):
data = df[df['Asin'].notna()] data = df[df['Asin'].notna()]
excel.save_xls(data, file_name, sheet_name) excel.save_xls(data, file_name, sheet_name)
return pd.read_excel(file_name, sheet_name=None) return pd.read_excel(file_name, sheet_name=None)
except ValueError as e: except ValueError:
self.logger.warning(f"{invoice_id} 文件格式错误: {e}") self.logger.warning(f"{invoice_id} 文件格式错误")
return None return None
......
...@@ -238,8 +238,6 @@ class VCManagerGUI(ttk.Window): ...@@ -238,8 +238,6 @@ class VCManagerGUI(ttk.Window):
except Exception as e: except Exception as e:
self.log(f"发生错误:{str(e)}") self.log(f"发生错误:{str(e)}")
self.log(traceback.format_exc()) self.log(traceback.format_exc())
finally:
self.cleanup_resources()
def init_browser(self): def init_browser(self):
"""初始化浏览器配置""" """初始化浏览器配置"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment