完善惊喜任务逐项执行和存储授权,兼容微信浏览入口及应用推荐下载落点。增加重复运行保护、投影恢复和任务完成复核,并保留原有安装适配。 验证:59项离线回归通过,生成文件一致性、语法及差异空白检查通过。
386 lines
20 KiB
JavaScript
386 lines
20 KiB
JavaScript
// 顺序可调整;失败仅重试当前流程,不能把失败记成完成。
|
||
var YIBAO_WORKFLOW = {
|
||
order: ["0_0", "0_1", "1", "2", "3"],
|
||
logPath: "/sdcard/Download/yingyongbao-all.log",
|
||
statePath: "/sdcard/Download/yingyongbao-all-state.json"
|
||
};
|
||
var activeYibaoFlow = null;
|
||
var yibaoWorkflowState = null;
|
||
|
||
function log(message) {
|
||
var line = new Date().toISOString() + " [应用宝一条龙][" +
|
||
(activeYibaoFlow ? activeYibaoFlow.name : "准备") + "][" + currentPhase + "][" + currentStep + "]" +
|
||
(yibaoWorkflowState && yibaoWorkflowState.engineId ? "[引擎"+yibaoWorkflowState.engineId+"] " : " ") + message;
|
||
console.log(line);
|
||
try { files.append(YIBAO_WORKFLOW.logPath, line + "\n"); } catch (ignored) {}
|
||
}
|
||
function errorText(error) {
|
||
if (!error) return "未知错误";
|
||
return String(error.message || error) + (error.stack ? "\n" + String(error.stack) : "");
|
||
}
|
||
function saveFailureCapture(tag) {
|
||
// 遵守不导出截图的约定,只记录阶段、错误及前台应用。
|
||
log("失败现场:" + tag + " foreground=" + describeForegroundApp(getForegroundApp()));
|
||
}
|
||
function persistYibaoWorkflowState() {
|
||
if (!yibaoWorkflowState) return;
|
||
yibaoWorkflowState.updatedAt = new Date().toISOString();
|
||
try { files.write(YIBAO_WORKFLOW.statePath, JSON.stringify(yibaoWorkflowState, null, 2)); }
|
||
catch (error) { log("状态记录失败:" + error); }
|
||
}
|
||
function activateYibaoFlow(profile) {
|
||
activeYibaoFlow = profile;
|
||
CONFIG = JSON.parse(JSON.stringify(profile.config));
|
||
// 公共弹窗原来自下载流程;只注入它需要的计时,不携带其他流程的业务配置。
|
||
if (!CONFIG.adView1) CONFIG.adView1 = {};
|
||
CONFIG.adView1.rewardBrowseWaitMs = 16000;
|
||
if (!CONFIG.adView1.afterBrowseRewardWaitMs) CONFIG.adView1.afterBrowseRewardWaitMs = 3000;
|
||
var installerAliases = ["com.android.packageinstaller", "com.google.android.packageinstaller", "com.oplus.appdetail", "com.coloros.safecenter", "com.vivo.packageinstaller"];
|
||
installerAliases.forEach(function (name) {
|
||
if (CONFIG.extraTask.installerPackageAliases.indexOf(name) < 0) CONFIG.extraTask.installerPackageAliases.push(name);
|
||
});
|
||
["com.bbk.launcher2", "com.android.launcher3", "com.oppo.launcher", "com.android.systemui", "com.vivo.appfilter"].forEach(function (name) {
|
||
if (CONFIG.extraTask.launcherPackages.indexOf(name) < 0) CONFIG.extraTask.launcherPackages.push(name);
|
||
});
|
||
currentPhase = "准备"; currentStep = ""; shellTimeoutCount = 0;
|
||
Object.keys(YIBAO_DEFAULT_BINDINGS).forEach(function (name) {
|
||
// 此处由构建器生成静态赋值;不通过 eval 或 global 对象绑定 AutoJs 函数。
|
||
bindYibaoFunction(name, YIBAO_IMPLEMENTATIONS[
|
||
Object.prototype.hasOwnProperty.call(profile.overrides, name) ? profile.overrides[name] : YIBAO_DEFAULT_BINDINGS[name]]);
|
||
});
|
||
}
|
||
function recognizeScreenSafe() {
|
||
// 先完成安装器;返回 null 让业务丢弃旧页面,再重新识别任务列表。
|
||
if (recoverCommonInstaller()) return null;
|
||
try {
|
||
var nativePage=nativeYibaoRecognition(readCommonInstallPage());
|
||
var items=nativePage ? nativePage.items : supplementYibaoTaskButtons(supplementYibaoNavigation(recognizeScreen()));
|
||
if(yibaoWorkflowState) {
|
||
var counts=yibaoWorkflowState.recognition || (yibaoWorkflowState.recognition={nativeFrames:0,ocrFrames:0});
|
||
if(nativePage)counts.nativeFrames++;else counts.ocrFrames++;
|
||
}
|
||
if(nativePage)log("直接读取原生控件,无需截图:"+nativePage.kind);
|
||
if (checkinStreakPopupTitle(items)) return closeCheckinStreakPopup(items);
|
||
return mealRewardPopupButton(items) ? closeMealRewardPopup(items) : items;
|
||
}
|
||
catch (error) {
|
||
if (error && (error.captureBackendFatal || error.taskInterrupted || error.unsafeWorker)) throw error;
|
||
log("OCR 采样失败,跳过本帧:" + errorText(error));
|
||
return null;
|
||
}
|
||
}
|
||
function waitPackageInstallerAndFinish(timeoutMs) { return waitCommonInstaller(timeoutMs, "open"); }
|
||
function readAdView1InstallPage() { return readCommonInstallPage(); }
|
||
function planAdView1InstallAction(page) { return planCommonInstallAction(page, "open"); }
|
||
function waitAdView3InstallerAndFinish(timeoutMs) {
|
||
if (readCommonInstallPage()) return waitCommonInstaller(timeoutMs, "finish");
|
||
var busy = commonInstallBusy;
|
||
commonInstallBusy = true;
|
||
try { return waitAdView3LegacyInstallerAndFinish(timeoutMs); }
|
||
finally { commonInstallBusy = busy; }
|
||
}
|
||
function backToYibao(timeoutMs) {
|
||
if (!commonInstallBusy && isInstallerForeground(getForegroundApp())) {
|
||
if (!waitCommonInstaller(CONFIG.extraTask.installTimeoutMs, activeYibaoFlow.completion)) return false;
|
||
}
|
||
if (!businessBackToYibao(timeoutMs)) {
|
||
if (isInstallerForeground(getForegroundApp())) return false;
|
||
log("原返回方式超时,使用应用宝启动Intent恢复前台,再核对页面");
|
||
if (!recoverYibaoForeground()) return false;
|
||
}
|
||
// 安装后的首次启动可能延迟抢回前台,单帧看到应用宝不能视为返回完成。
|
||
for (var attempt = 0; attempt < 3; attempt += 1) {
|
||
var stable = true;
|
||
for (var frame = 0; frame < 3; frame += 1) {
|
||
sleep(600);
|
||
var foreground = getForegroundApp();
|
||
if (!foreground || foreground.packageName !== CONFIG.yibaoPackage) { stable = false; break; }
|
||
}
|
||
if (stable) return true;
|
||
if (attempt === 2) break;
|
||
var current = getForegroundApp();
|
||
if (isInstallerForeground(current)) return false;
|
||
log("返回后前台再次变化,重新拉起应用宝并复核:" + describeForegroundApp(current));
|
||
if (!recoverYibaoForeground()) return false;
|
||
sleep(1000);
|
||
}
|
||
log("未确认应用宝持续处于前台,不继续点击任务页");
|
||
return false;
|
||
}
|
||
function recoverYibaoForeground() {
|
||
// 不清任务栈,保留广告奖励和安装后的去浏览弹窗;仅在外部应用返回失败时使用。
|
||
if (isInstallerForeground(getForegroundApp())) return false;
|
||
try {
|
||
var intent=context.getPackageManager().getLaunchIntentForPackage(CONFIG.yibaoPackage);
|
||
if (!intent) return false;
|
||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
||
context.startActivity(intent);
|
||
var deadline=Date.now()+8000;
|
||
while(Date.now()<deadline) {
|
||
var foreground=getForegroundApp();
|
||
if(isInstallerForeground(foreground))return false;
|
||
if(foreground && foreground.packageName===CONFIG.yibaoPackage)return true;
|
||
sleep(400);
|
||
}
|
||
} catch(error) {
|
||
if(error && (error.taskInterrupted || error.captureBackendFatal || error.unsafeWorker) ||
|
||
/InterruptedException|ScriptInterruptedException/.test(String(error)))throw error;
|
||
log("启动Intent恢复前台失败:"+errorText(error));
|
||
}
|
||
return false;
|
||
}
|
||
function launchYibao() {
|
||
try {businessLaunchYibao();}
|
||
catch(error) {
|
||
if(error && (error.taskInterrupted || error.captureBackendFatal || error.unsafeWorker) ||
|
||
/InterruptedException|ScriptInterruptedException/.test(String(error)))throw error;
|
||
if(!/应用宝未进入前台/.test(String(error)) || !recoverYibaoForeground())throw error;
|
||
log("原启动未恢复前台,已通过启动Intent恢复,继续识别当前页面");
|
||
}
|
||
if (activeYibaoFlow.id === "0_1") return;
|
||
var items = recognizeScreenSafe();
|
||
if (items && inspectMealSubsidyPage(items).isMealPage) {
|
||
log("重新打开应用宝后恢复到饭点补贴页,返回赚钱任务中心再开始当前流程");
|
||
leaveMealSubsidyPage();
|
||
}
|
||
}
|
||
function handleBrowseExitDialog(items) { return handleAdView1BrowseOffer(items); }
|
||
|
||
function workflowStartupPackage() {
|
||
var foreground = getForegroundAppByAutoJs();
|
||
return foreground ? foreground.packageName : "";
|
||
}
|
||
function clickWorkflowStartupNode(node) {
|
||
if (!node || !node.enabled()) return false;
|
||
var rect = node.bounds();
|
||
if (rect.width() <= 0 || rect.height() <= 0 || rect.centerX() <= 0 || rect.centerX() >= device.width ||
|
||
rect.centerY() <= 0 || rect.centerY() >= device.height) return false;
|
||
// 设置页优先执行原生点击,避免坐标手势被系统浮层截获。
|
||
var clickable = node;
|
||
for (var depth = 0; clickable && depth < 4; depth += 1) {
|
||
if (clickable.enabled() && clickable.clickable() && clickable.click()) return true;
|
||
clickable = clickable.parent();
|
||
}
|
||
return press(rect.centerX(), rect.centerY(), 100) !== false;
|
||
}
|
||
function waitWorkflowStartupPackage(packageName, timeoutMs) {
|
||
var deadline = Date.now() + timeoutMs;
|
||
while (Date.now() < deadline) {
|
||
if (workflowStartupPackage() === packageName) return;
|
||
sleep(200);
|
||
}
|
||
throw new Error("未进入启动目标页面:" + packageName);
|
||
}
|
||
function isYibaoPackageStopped(packageName) {
|
||
try {
|
||
if (typeof context === "undefined" || typeof android === "undefined") return false;
|
||
var info = context.getPackageManager().getApplicationInfo(packageName, 0);
|
||
return (Number(info.flags) & Number(android.content.pm.ApplicationInfo.FLAG_STOPPED)) !== 0;
|
||
} catch (error) { log("读取应用停止标记失败:" + error); return false; }
|
||
}
|
||
function usesYibaoTaskStackRestart() {
|
||
return /oppo|oneplus|realme/i.test(String(device.brand || "") + " " + String(device.manufacturer || ""));
|
||
}
|
||
function restartYibaoTaskStack(target) {
|
||
// ColorOS启动及vivo设置页强停失败时重建界面任务栈,保留后台下载服务。
|
||
var intent = context.getPackageManager().getLaunchIntentForPackage(target);
|
||
if (!intent) throw new Error("没有可用的应用宝启动入口");
|
||
home();
|
||
sleep(500);
|
||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||
context.startActivity(intent);
|
||
var deadline = Date.now() + 15000;
|
||
while (Date.now() < deadline) {
|
||
if (workflowStartupPackage() === target || isInstallerForeground(getForegroundApp())) break;
|
||
sleep(200);
|
||
}
|
||
sleep(CONFIG.appLaunchWaitMs);
|
||
if (isInstallerForeground(getForegroundApp())) {
|
||
log("应用宝重开后恢复了待安装任务,交给原有安装流程,不判为启动失败");
|
||
return;
|
||
}
|
||
if (workflowStartupPackage() !== target) throw new Error("重开应用宝后前台再次变化,停止页面操作");
|
||
log("已退出旧界面并重新打开应用宝,使用任务栈重建,未强杀后台下载服务");
|
||
}
|
||
function stopYibaoForWorkflow(target, appName) {
|
||
var settingsPackage = "com.android.settings";
|
||
app.openAppSetting(target);
|
||
waitWorkflowStartupPackage(settingsPackage, 8000);
|
||
var stopPattern = /^(?:强行停止|强制停止|结束运行|Force stop)$/i;
|
||
var deadline = Date.now() + 8000, stop = null;
|
||
while (Date.now() < deadline) {
|
||
if (workflowStartupPackage() === settingsPackage && text(appName).packageName(settingsPackage).exists()) {
|
||
stop = textMatches(stopPattern).packageName(settingsPackage).findOnce();
|
||
if (stop) break;
|
||
}
|
||
sleep(200);
|
||
}
|
||
if (!stop) throw new Error("未确认应用宝设置详情,不操作其他应用");
|
||
sleep(600);
|
||
stop = textMatches(stopPattern).packageName(settingsPackage).findOnce();
|
||
if (!stop || workflowStartupPackage() !== settingsPackage || !text(appName).packageName(settingsPackage).exists()) {
|
||
throw new Error("应用宝设置页在启动前发生变化");
|
||
}
|
||
if (stop.enabled() && !isYibaoPackageStopped(target)) {
|
||
log("已确认应用宝详情,点击强行停止 bounds=" + stop.bounds());
|
||
if (!clickWorkflowStartupNode(stop)) throw new Error("无法点击应用宝强行停止");
|
||
deadline = Date.now() + 8000;
|
||
while (Date.now() < deadline) {
|
||
if (workflowStartupPackage() !== settingsPackage) throw new Error("停止应用时离开设置页面");
|
||
var confirm = id("android:id/button1").packageName(settingsPackage).findOnce();
|
||
if (confirm && /^(?:强行停止|强制停止|确定|确认|停止|Force stop|OK)$/i.test(String(confirm.text()))) {
|
||
log("确认应用宝强行停止");
|
||
clickWorkflowStartupNode(confirm);
|
||
// 用户指定:点确认后直接启动,不再以强停标记或按钮状态阻塞流程。
|
||
log("已点击强停确认,直接重新打开应用宝,不检查强停结果");
|
||
break;
|
||
}
|
||
stop = textMatches(stopPattern).packageName(settingsPackage).findOnce();
|
||
// vivo 部分版本在强停后仍把按钮报告为 enabled=true,以系统包停止标记为准。
|
||
if (isYibaoPackageStopped(target) || (stop && !stop.enabled())) break;
|
||
sleep(200);
|
||
}
|
||
}
|
||
}
|
||
function restartYibaoForWorkflow() {
|
||
var target = CONFIG.yibaoPackage;
|
||
var appName = String(app.getAppName(target) || "");
|
||
if (!appName) throw new Error("未安装应用宝");
|
||
setPhase("关闭并重新打开应用宝");
|
||
if (usesYibaoTaskStackRestart()) {
|
||
restartYibaoTaskStack(target);
|
||
return;
|
||
}
|
||
try {
|
||
stopYibaoForWorkflow(target, appName);
|
||
if (!app.launchPackage(target)) throw new Error("无法重新打开应用宝");
|
||
waitWorkflowStartupPackage(target, 15000);
|
||
sleep(CONFIG.appLaunchWaitMs);
|
||
} catch (error) {
|
||
if (error && (error.taskInterrupted || error.captureBackendFatal || error.unsafeWorker || error.workflowCancelled) ||
|
||
/InterruptedException|ScriptInterruptedException/.test(String(error))) throw error;
|
||
if (!/vivo|iqoo/i.test(String(device.brand || "") + " " + String(device.manufacturer || ""))) throw error;
|
||
log("vivo 启动前强停或重开未完成:" + errorText(error) + ";改用重建应用宝任务栈继续启动");
|
||
restartYibaoTaskStack(target);
|
||
return;
|
||
}
|
||
log("已完成启动前强停操作并重新打开应用宝(不校验强停结果)");
|
||
}
|
||
function runYibaoStages(order) {
|
||
for (var index = 0; index < order.length; index += 1) {
|
||
var profile = YIBAO_PROFILES.filter(function (p) { return p.id === order[index]; })[0];
|
||
if (!profile) throw new Error("未知流程:" + order[index]);
|
||
activateYibaoFlow(profile);
|
||
var stage = {id:profile.id, name:profile.name, status:"running", attempts:0};
|
||
yibaoWorkflowState.stages.push(stage);
|
||
yibaoWorkflowState.current = profile.id;
|
||
persistYibaoWorkflowState();
|
||
for (var attempt = 1; attempt <= CONFIG.flowMaxAttempts; attempt += 1) {
|
||
stage.attempts = attempt;
|
||
persistYibaoWorkflowState();
|
||
try {
|
||
log("开始流程 " + (index + 1) + "/" + order.length + ",尝试 " + attempt);
|
||
recoverCommonInstaller();
|
||
if (profile.id !== "0_1") {
|
||
var entryPage = recognizeScreenSafe();
|
||
if (entryPage && inspectMealSubsidyPage(entryPage).isMealPage) {
|
||
log("检测到前一流程遗留的饭点补贴页,先返回赚钱任务中心再启动当前流程");
|
||
leaveMealSubsidyPage();
|
||
}
|
||
}
|
||
var result = runHomeYibao();
|
||
if (result === false || captureBackendState.fatalError) throw captureBackendState.fatalError || new Error("流程返回失败");
|
||
stage.status = result && /^(failed|unresolved)$/.test(result.status) ? "failed" :
|
||
(result && result.status === "skipped" ? "skipped" : "success");
|
||
if(result && result.remainingTasks)stage.remainingTasks=result.remainingTasks;
|
||
if(result && result.reason)stage.reason=result.reason;
|
||
if (stage.status === "skipped") {
|
||
stage.reason = result.reason;
|
||
log("跳过当前流程:" + stage.reason);
|
||
}
|
||
break;
|
||
} catch (error) {
|
||
stage.error = errorText(error);
|
||
stage.phase = currentPhase; stage.step = currentStep;
|
||
log("流程失败:" + stage.error);
|
||
if (error && (error.taskInterrupted || error.captureBackendFatal || error.unsafeWorker || error.workflowCancelled) ||
|
||
/InterruptedException|ScriptInterruptedException/.test(String(error))) {
|
||
stage.status = "interrupted";
|
||
persistYibaoWorkflowState();
|
||
throw error;
|
||
}
|
||
stage.status = "failed";
|
||
if (attempt < CONFIG.flowMaxAttempts) {
|
||
if (!backToYibao(20000)) break;
|
||
sleep(1500);
|
||
}
|
||
}
|
||
}
|
||
persistYibaoWorkflowState();
|
||
// 没找到当前业务入口时走下一流程;仍在安装器时不能跳过正在进行的安装。
|
||
if (stage.status === "failed" && isInstallerForeground(getForegroundApp())) recoverCommonInstaller();
|
||
}
|
||
}
|
||
function ensureYibaoAccessibility() {
|
||
var deadline = Date.now() + 10000;
|
||
while (Date.now() < deadline) {
|
||
if (auto.rootInActiveWindow) return;
|
||
sleep(300);
|
||
}
|
||
throw new Error("无障碍未提供活动窗口,请开启 AutoJs6 无障碍服务并解锁手机");
|
||
}
|
||
function resolveYibaoWorkflowOrder() {
|
||
var order = YIBAO_WORKFLOW.order.slice();
|
||
var args = typeof engines !== "undefined" ? engines.myEngine().execArgv : null;
|
||
var startFlow = args && args.yibaoStartFlow;
|
||
if (startFlow !== undefined && startFlow !== null) {
|
||
var index = order.indexOf(String(startFlow));
|
||
if (index < 0) throw new Error("未知恢复流程:" + startFlow);
|
||
order = order.slice(index);
|
||
}
|
||
return order;
|
||
}
|
||
function runYibaoWorkflow() {
|
||
activateYibaoFlow(YIBAO_PROFILES[0]);
|
||
var lease=acquireYibaoWorkflowLease();
|
||
if(!lease)return;
|
||
yibaoWorkflowState = {status:"running",startedAt:new Date().toISOString(),current:null,stages:[],engineId:String(engines.myEngine().id)};
|
||
try {
|
||
var order = resolveYibaoWorkflowOrder();
|
||
var first = YIBAO_PROFILES.filter(function (profile) { return profile.id === order[0]; })[0];
|
||
if (!first) throw new Error("一条龙没有有效的起始流程");
|
||
activateYibaoFlow(first);
|
||
yibaoWorkflowState.order = order;
|
||
startYibaoScreenAwake();
|
||
persistYibaoWorkflowState();
|
||
setPhase("检查无障碍活动窗口");
|
||
ensureYibaoAccessibility();
|
||
var pendingInstaller = isInstallerForeground(getForegroundApp());
|
||
setPhase("申请一次截图权限");
|
||
if (!requestCapturePermission()) throw new Error("未获得截图权限,请允许 AutoJs6 整个屏幕录制");
|
||
sleep(CONFIG.ocrReadyWaitMs);
|
||
// 恢复安装器也可能需要OCR,必须先授权;授权切到AutoJs6后拉回原任务栈。
|
||
if (pendingInstaller) {
|
||
app.launchPackage(CONFIG.yibaoPackage);
|
||
sleep(1200);
|
||
if (!isInstallerForeground(getForegroundApp())) back();
|
||
sleep(600);
|
||
recoverCommonInstaller();
|
||
}
|
||
restartYibaoForWorkflow();
|
||
runYibaoStages(order);
|
||
var failed = yibaoWorkflowState.stages.some(function (stage) { return stage.status !== "success" && stage.status !== "skipped"; });
|
||
yibaoWorkflowState.status = failed ? "partial_failure" : "success";
|
||
toast(failed ? "应用宝一条龙已结束,部分流程失败,请查看日志" : "应用宝五个流程已完成");
|
||
} catch (error) {
|
||
yibaoWorkflowState.status = "interrupted";
|
||
yibaoWorkflowState.error = errorText(error);
|
||
log("一条龙停止:" + yibaoWorkflowState.error);
|
||
toast("应用宝一条龙停止:" + String(error.message || error));
|
||
} finally {
|
||
yibaoWorkflowState.finishedAt = new Date().toISOString();
|
||
persistYibaoWorkflowState();
|
||
try {stopYibaoScreenAwake();}finally{lease.release();}
|
||
}
|
||
}
|