tls_flow.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. tls_flow.py - TLScontact 签证预约业务流程状态机
  3. 基于 BaseFlow 声明式注解,实现异常自愈、登录、申请人流转与时段监控
  4. """
  5. import time
  6. from flow_annotated import BaseFlow, on, done, fatal, guard
  7. from cf_bypasser import CloudflareBypasser
  8. class TlsContactFlow(BaseFlow):
  9. def __init__(self, page, config=None, interval=1.0, logger=None, cf_bypasser=None):
  10. super().__init__(page, config=config, interval=interval, logger=logger, cf_bypasser=cf_bypasser)
  11. if self.cf_bypasser is None:
  12. self.cf_bypasser = CloudflareBypasser(page, logger=self.log)
  13. self.preferred_dates = self.config.get("preferred_dates", [])
  14. # -----------------------------------------------------------------------
  15. # 1. 异常守卫注解 (高优先级 priority=1~20)
  16. # -----------------------------------------------------------------------
  17. @guard(text="502 Bad Gateway", priority=5)
  18. def on_502_error(self):
  19. self.log("[WARN] 检测到 502 网关错误,等待 3 秒后刷新重试...", level="warning")
  20. time.sleep(3)
  21. self.page.refresh()
  22. @guard(text="504 Gateway Time-out", priority=5)
  23. def on_504_error(self):
  24. self.log("[WARN] 检测到 504 网关超时,等待 3 秒后刷新重试...", level="warning")
  25. time.sleep(3)
  26. self.page.refresh()
  27. @guard(title="just a moment", priority=6)
  28. def on_cloudflare_challenge_title(self):
  29. self.log("🛡️ 命中 Cloudflare Challenge 守卫 (Title 匹配)...")
  30. self.cf_bypasser.bypass()
  31. @guard(ele="@src:challenges.cloudflare.com", priority=6)
  32. def on_cloudflare_challenge_iframe(self):
  33. self.log("🛡️ 命中 Cloudflare Turnstile 守卫 (Iframe 匹配)...")
  34. self.cf_bypasser.bypass()
  35. @guard(text="Session Expired", priority=8)
  36. def on_session_expired(self):
  37. self.log("[WARN] 会话已过期,重新跳转至登录入口...", level="warning")
  38. login_url = self.config.get("login_url") or self.config.get("target_url")
  39. if login_url:
  40. self.page.get(login_url)
  41. # -----------------------------------------------------------------------
  42. # 2. 致命错误注解 (直接中断)
  43. # -----------------------------------------------------------------------
  44. @on(text="You have not yet added an applicant", priority=15)
  45. @fatal("未找到申请人,请先在官网完善申请人基本信息后再次执行!")
  46. def on_no_applicant(self):
  47. pass
  48. @on(text="Account locked", priority=15)
  49. @fatal("账号已被锁定,请稍后再试或联系 TLScontact 客服!")
  50. def on_account_locked(self):
  51. pass
  52. # -----------------------------------------------------------------------
  53. # 3. 业务表单操作 (登录、分组、概览、预约时段)
  54. # -----------------------------------------------------------------------
  55. @on(ele="tag:label@text():Email", priority=50)
  56. def handle_login_by_label(self):
  57. self.log("正在填写登录表单 (通过 Label 查找)...")
  58. username = self.config.get("username", "")
  59. password = self.config.get("password", "")
  60. email_input = self.page.ele('tag:label@text():Email').next('tag:input') or self.page.ele('tag:input@type=email') or self.page.ele('@name=email')
  61. if email_input:
  62. email_input.clear()
  63. email_input.input(username)
  64. pwd_input = self.page.ele('tag:label@text():Password').next('tag:input') or self.page.ele('tag:input@type=password') or self.page.ele('@name=password')
  65. if pwd_input:
  66. pwd_input.clear()
  67. pwd_input.input(password)
  68. time.sleep(0.5)
  69. login_btn = self.page.ele('tag:button@text():Login') or self.page.ele('tag:button@type=submit')
  70. if login_btn:
  71. login_btn.click()
  72. @on(url="/login", ele="tag:input@type=email", priority=51)
  73. def handle_login_by_url(self):
  74. self.log("正在填写登录页面...")
  75. username = self.config.get("username", "")
  76. password = self.config.get("password", "")
  77. self.page.ele('tag:input@type=email').input(username)
  78. self.page.ele('tag:input@type=password').input(password)
  79. time.sleep(0.5)
  80. self.page.ele('tag:button@type=submit').click()
  81. @on(url="travel-groups", priority=60)
  82. def handle_travel_groups(self):
  83. self.log("正在选择行程/申请人分组...")
  84. btn = self.page.ele('tag:button@name=formGroupId') or \
  85. self.page.ele('tag:button@text():Select') or \
  86. self.page.ele('.group-btn') or \
  87. self.page.ele('tag:a@text():View')
  88. if btn:
  89. btn.click()
  90. else:
  91. self.log("[WARN] 未找到分组按钮,尝试点击列表首项...")
  92. card = self.page.ele('.group-card')
  93. if card:
  94. card.click()
  95. @on(url="application-summary", priority=70)
  96. def handle_change_booking(self):
  97. self.log("正在处理申请人摘要页 (application-summary)...")
  98. change_btn = self.page.ele('tag:button@text():Change') or self.page.ele('tag:button@text():Book')
  99. if change_btn:
  100. change_btn.click()
  101. time.sleep(0.5)
  102. # 确认弹窗
  103. yes_btn = self.page.ele('tag:button@text():Yes') or self.page.ele('tag:button@text():Confirm')
  104. if yes_btn:
  105. yes_btn.click()
  106. @on(url="appointment-booking", ele=".available-slot", priority=80)
  107. def handle_slot_selection(self):
  108. self.log("📅 发现可用预约时段,正在选择最优时段...")
  109. slots = self.page.eles(".available-slot") or self.page.eles(".slot-btn")
  110. if slots:
  111. selected_slot = slots[0]
  112. for slot in slots:
  113. slot_text = slot.text
  114. if any(pref in slot_text for pref in self.preferred_dates):
  115. selected_slot = slot
  116. self.log(f"🎯 命中首选日期时段: {slot_text}")
  117. break
  118. selected_slot.click()
  119. time.sleep(0.5)
  120. confirm_btn = self.page.ele("tag:button@text():Confirm Slot") or self.page.ele("tag:button@text():Continue")
  121. if confirm_btn:
  122. confirm_btn.click()
  123. # -----------------------------------------------------------------------
  124. # 4. 成功终态注解 (Done)
  125. # -----------------------------------------------------------------------
  126. @on(url="appointment-booking", ele="tag:button@text():Book your appointment", priority=30)
  127. @done
  128. def on_booking_button_submit(self):
  129. self.log("🎯 点击最终预约提交按钮...")
  130. btn = self.page.ele("tag:button@text():Book your appointment")
  131. if btn:
  132. btn.click()
  133. self.log("✅ 抢号/预约流程提交完成!")
  134. @on(url="appointment-confirmation", priority=25)
  135. @on(text="Appointment Confirmed", priority=25)
  136. @done
  137. def on_booking_success_confirmed(self):
  138. self.log("🎉 页面已跳转至预约成功确认页!预约成功!")