# coding=utf-8 import asyncio import logging import os import time import uiautomator2 as u2 import uuid from Apps.YeLiTe.Kit import take_screenshot from Apps.YeLiTe.ReadImageKit import ReadImageKit from Config.Config import TEMP_IMAGE_DIR logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("OpenYiLaiTe") BASE_DIR = os.path.dirname(os.path.abspath(__file__)) async def check_and_close_ad(d): """ 检测并关闭广告弹窗 """ max_loops = 2 w, h = d.window_size() device_info = {'displayWidth': w, 'displayHeight': h} for loop_idx in range(max_loops): logger.info(f"开始第 {loop_idx + 1} 轮广告检测...") image_uuid = f"ylt_ad_check_{int(time.time())}" screenshot_path = take_screenshot(d, image_uuid, save_dir=TEMP_IMAGE_DIR) ad_result = await ReadImageKit.detect_ad_popup(screenshot_path, device_info=device_info) if ad_result: x, y = ad_result['x'], ad_result['y'] ad_type = ad_result.get("ad_type", "unknown") logger.info(f"检测到广告 [{ad_type}],坐标: ({x}, {y}),执行关闭操作...") d.click(x, y) await asyncio.sleep(2.0) if os.path.exists(screenshot_path): os.remove(screenshot_path) else: logger.info("未检测到广告弹窗。") if os.path.exists(screenshot_path): os.remove(screenshot_path) break return True async def open_mini_program(): """ 进入微信小程序: 驿来特 """ d = u2.connect() logger.info("执行进入小程序: 驿来特") # 1. 启动微信 logger.info("启动微信...") d.app_start("com.tencent.mm", stop=True) await asyncio.sleep(5) # 2. 点击搜索按钮 (坐标适配大部分微信版本) logger.info("点击搜索按钮...") w, h = d.window_size() d.click(int(w * 0.84), int(h * 0.08)) await asyncio.sleep(2) # 3. 输入搜索内容 logger.info("输入搜索内容: 驿来特") d.send_keys("驿来特") await asyncio.sleep(3) # 4. 点击搜索结果中的第一个小程序 logger.info("点击搜索结果中的小程序...") d.click(int(w * 0.5), int(h * 0.18)) logger.info("等待小程序加载...") await asyncio.sleep(10) # 5. 广告检测 await check_and_close_ad(d) return True if __name__ == "__main__": asyncio.run(open_mini_program())