| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- """
- tls_flow.py - TLScontact 签证预约业务流程状态机
- 基于 BaseFlow 声明式注解,实现异常自愈、登录、申请人流转与时段监控
- """
- import time
- from flow_annotated import BaseFlow, on, done, fatal, guard
- from cf_bypasser import CloudflareBypasser
- class TlsContactFlow(BaseFlow):
- def __init__(self, page, config=None, interval=1.0, logger=None, cf_bypasser=None):
- super().__init__(page, config=config, interval=interval, logger=logger, cf_bypasser=cf_bypasser)
- if self.cf_bypasser is None:
- self.cf_bypasser = CloudflareBypasser(page, logger=self.log)
- self.preferred_dates = self.config.get("preferred_dates", [])
- # -----------------------------------------------------------------------
- # 1. 异常守卫注解 (高优先级 priority=1~20)
- # -----------------------------------------------------------------------
- @guard(text="502 Bad Gateway", priority=5)
- def on_502_error(self):
- self.log("[WARN] 检测到 502 网关错误,等待 3 秒后刷新重试...", level="warning")
- time.sleep(3)
- self.page.refresh()
- @guard(text="504 Gateway Time-out", priority=5)
- def on_504_error(self):
- self.log("[WARN] 检测到 504 网关超时,等待 3 秒后刷新重试...", level="warning")
- time.sleep(3)
- self.page.refresh()
- @guard(title="just a moment", priority=6)
- def on_cloudflare_challenge_title(self):
- self.log("🛡️ 命中 Cloudflare Challenge 守卫 (Title 匹配)...")
- self.cf_bypasser.bypass()
- @guard(ele="@src:challenges.cloudflare.com", priority=6)
- def on_cloudflare_challenge_iframe(self):
- self.log("🛡️ 命中 Cloudflare Turnstile 守卫 (Iframe 匹配)...")
- self.cf_bypasser.bypass()
- @guard(text="Session Expired", priority=8)
- def on_session_expired(self):
- self.log("[WARN] 会话已过期,重新跳转至登录入口...", level="warning")
- login_url = self.config.get("login_url") or self.config.get("target_url")
- if login_url:
- self.page.get(login_url)
- # -----------------------------------------------------------------------
- # 2. 致命错误注解 (直接中断)
- # -----------------------------------------------------------------------
- @on(text="You have not yet added an applicant", priority=15)
- @fatal("未找到申请人,请先在官网完善申请人基本信息后再次执行!")
- def on_no_applicant(self):
- pass
- @on(text="Account locked", priority=15)
- @fatal("账号已被锁定,请稍后再试或联系 TLScontact 客服!")
- def on_account_locked(self):
- pass
- # -----------------------------------------------------------------------
- # 3. 业务表单操作 (登录、分组、概览、预约时段)
- # -----------------------------------------------------------------------
- @on(ele="tag:label@text():Email", priority=50)
- def handle_login_by_label(self):
- self.log("正在填写登录表单 (通过 Label 查找)...")
- username = self.config.get("username", "")
- password = self.config.get("password", "")
-
- 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')
- if email_input:
- email_input.clear()
- email_input.input(username)
-
- 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')
- if pwd_input:
- pwd_input.clear()
- pwd_input.input(password)
- time.sleep(0.5)
- login_btn = self.page.ele('tag:button@text():Login') or self.page.ele('tag:button@type=submit')
- if login_btn:
- login_btn.click()
- @on(url="/login", ele="tag:input@type=email", priority=51)
- def handle_login_by_url(self):
- self.log("正在填写登录页面...")
- username = self.config.get("username", "")
- password = self.config.get("password", "")
-
- self.page.ele('tag:input@type=email').input(username)
- self.page.ele('tag:input@type=password').input(password)
- time.sleep(0.5)
- self.page.ele('tag:button@type=submit').click()
- @on(url="travel-groups", priority=60)
- def handle_travel_groups(self):
- self.log("正在选择行程/申请人分组...")
- btn = self.page.ele('tag:button@name=formGroupId') or \
- self.page.ele('tag:button@text():Select') or \
- self.page.ele('.group-btn') or \
- self.page.ele('tag:a@text():View')
- if btn:
- btn.click()
- else:
- self.log("[WARN] 未找到分组按钮,尝试点击列表首项...")
- card = self.page.ele('.group-card')
- if card:
- card.click()
- @on(url="application-summary", priority=70)
- def handle_change_booking(self):
- self.log("正在处理申请人摘要页 (application-summary)...")
- change_btn = self.page.ele('tag:button@text():Change') or self.page.ele('tag:button@text():Book')
- if change_btn:
- change_btn.click()
- time.sleep(0.5)
-
- # 确认弹窗
- yes_btn = self.page.ele('tag:button@text():Yes') or self.page.ele('tag:button@text():Confirm')
- if yes_btn:
- yes_btn.click()
- @on(url="appointment-booking", ele=".available-slot", priority=80)
- def handle_slot_selection(self):
- self.log("📅 发现可用预约时段,正在选择最优时段...")
- slots = self.page.eles(".available-slot") or self.page.eles(".slot-btn")
- if slots:
- selected_slot = slots[0]
- for slot in slots:
- slot_text = slot.text
- if any(pref in slot_text for pref in self.preferred_dates):
- selected_slot = slot
- self.log(f"🎯 命中首选日期时段: {slot_text}")
- break
-
- selected_slot.click()
- time.sleep(0.5)
-
- confirm_btn = self.page.ele("tag:button@text():Confirm Slot") or self.page.ele("tag:button@text():Continue")
- if confirm_btn:
- confirm_btn.click()
- # -----------------------------------------------------------------------
- # 4. 成功终态注解 (Done)
- # -----------------------------------------------------------------------
- @on(url="appointment-booking", ele="tag:button@text():Book your appointment", priority=30)
- @done
- def on_booking_button_submit(self):
- self.log("🎯 点击最终预约提交按钮...")
- btn = self.page.ele("tag:button@text():Book your appointment")
- if btn:
- btn.click()
- self.log("✅ 抢号/预约流程提交完成!")
- @on(url="appointment-confirmation", priority=25)
- @on(text="Appointment Confirmed", priority=25)
- @done
- def on_booking_success_confirmed(self):
- self.log("🎉 页面已跳转至预约成功确认页!预约成功!")
|