// 确保无障碍服务已启用
auto.waitFor();
log("脚本开始运行...");

// ===== 配置管理器(悬浮窗) =====
let config = {
quantity: 1, // 购买数量
boxCount: 12, // 整盒盲盒数量
paymentPassword: "123456", // 支付密码
delay: 1000 // 操作延迟时间(毫秒)
};

// 创建配置悬浮窗
let floatyWindow = floaty.window(
<frame gravity="center">
<vertical padding="16" bg="#88000000" w="200">
<text text="泡泡玛特配置" textSize="16sp" textColor="#FFFFFF"/>
<horizontal>
<text text="数量:" w="60" textColor="#FFFFFF"/>
<input id="quantityInput" text="1"/>
</horizontal>
<horizontal>
<text text="整盒数:" w="60" textColor="#FFFFFF"/>
<input id="boxCountInput" text="12"/>
</horizontal>
<button id="saveBtn" text="保存并开始"/>
</vertical>
</frame>
);

// 设置悬浮窗位置
floatyWindow.setPosition(device.width / 4, device.height / 4);

// 保存按钮点击事件
floatyWindow.saveBtn.click(() => {
// 更新配置
config.quantity = parseInt(floatyWindow.quantityInput.getText()) || config.quantity;
config.boxCount = parseInt(floatyWindow.boxCountInput.getText()) || config.boxCount;

// 隐藏悬浮窗
floatyWindow.close();

// 开始购买流程
toast("配置已保存,开始购买流程");
runPurchaseProcess();

});

// ===== 核心购买流程 =====
function runPurchaseProcess() {
// 1. 等待"立即购买"按钮出现
log("等待检测立即购买按钮...");
let buyButton = text("立即购买").findOne(10000);
if (!buyButton) {
toast("未找到立即购买按钮!");
return;
}

log("开始执行购买流程");

// 2. 点击立即购买
log("点击立即购买");
buyButton.click();
sleep(config.delay);

// 3. 选择整盒盲盒
log("选择整盒盲盒");
let fullBoxBtn = textMatches(/整盒.*盲盒/).findOne(5000);
if (fullBoxBtn) {
    fullBoxBtn.click();
    sleep(config.delay);
} else {
    toast("未找到整盒选项!");
}

// 4. 确定选择
log("确认选择");
let confirmBtn = text("确定").findOne(5000);
if (confirmBtn) {
    confirmBtn.click();
    sleep(config.delay);
}

// 5. 设置购买数量
if (config.quantity > 1) {
    log("设置购买数量: " + config.quantity);
    let plusButton = text("+").findOne(3000);
    if (plusButton) {
        for (let i = 1; i < config.quantity; i++) {
            plusButton.click();
            sleep(500);
        }
    }
}

// 6. 确认信息并支付
log("确认信息并支付");
let payBtn = text("确认信息并支付").findOne(5000);
if (payBtn) {
    payBtn.click();
    sleep(config.delay);
}

// 7. 确认无误
log("确认无误");
let confirmInfoBtn = text("确认无误").findOne(5000);
if (confirmInfoBtn) {
    confirmInfoBtn.click();
    sleep(config.delay);
}

// 8. 输入支付密码
log("输入支付密码");
const width = device.width;
const height = device.height;

const keyboardMap = {
    "1": {x: width * 0.15, y: height * 0.75},
    "2": {x: width * 0.5, y: height * 0.75},
    "3": {x: width * 0.85, y: height * 0.75},
    "4": {x: width * 0.15, y: height * 0.85},
    "5": {x: width * 0.5, y: height * 0.85},
    "6": {x: width * 0.85, y: height * 0.85},
    "7": {x: width * 0.15, y: height * 0.95},
    "8": {x: width * 0.5, y: height * 0.95},
    "9": {x: width * 0.85, y: height * 0.95},
    "0": {x: width * 0.5, y: height * 1.05}
};

for (let digit of config.paymentPassword) {
    if (keyboardMap[digit]) {
        click(keyboardMap[digit].x, keyboardMap[digit].y);
        sleep(300);
    }
}

// 9. 检查支付结果
sleep(3000);
if (text("支付成功").exists() || text("购买成功").exists()) {
    toast("支付成功!");
} else {
    toast("支付结果未知");
}

}

log("初始化完成,等待配置...");
toast("请点击悬浮窗进行配置");
为什么这一段代码运行没反应

已存档
9mfpn7z08t · 2 个月前

回答

Hamibot AI
1
· 2 个月前

这段代码运行没反应可能有几个原因,以下是常见问题和解决方法:

  1. 无障碍服务未启用
// 确保脚本开头有
auto.waitFor();

如果无障碍服务未开启,脚本会卡在这一步。请到手机设置中开启Hamibot的无障碍权限。

  1. 悬浮窗权限未开启
    需要开启"后台弹出界面"权限才能显示配置窗口。在MIUI中进入:
    设置->应用设置->权限管理->特殊权限设置->后台弹出界面

  2. UI模式限制
    如果是在UI模式下运行,auto.waitFor()会阻塞UI线程。建议:

// 改为使用auto()代替auto.waitFor()
auto();
  1. 控件查找失败
    脚本中大量依赖文本匹配查找控件,如果界面语言或布局不同会导致失败。建议:
// 增加更灵活的匹配方式
let buyButton = textMatches(/立即购买|Buy Now/).findOne(10000);
  1. 悬浮窗显示问题
    尝试调整悬浮窗位置:
floatyWindow.setPosition(100, 100);  // 更小的坐标值
  1. 完整的修改建议
// 文档地址:https://docs.hamibot.com/
auto();

// 简化悬浮窗创建
let floatyWindow = floaty.window(
    <frame gravity="center" bg="#88000000">
        <vertical padding="16">
            <text text="泡泡玛特配置" textSize="16sp"/>
            <text text="数量:"/>
            <input id="quantityInput" text="1"/>
            <button id="saveBtn" text="保存"/>
        </vertical>
    </frame>
);

floatyWindow.setPosition(100, 100);

floatyWindow.saveBtn.click(function(){
    toast("点击事件触发");
    // 测试代码
});

toast("脚本已启动");

如果仍有问题,建议:

  1. 检查Hamibot的日志输出
  2. 分步测试脚本功能
  3. 确保测试设备与泡泡玛特APP版本匹配

hamibot.exit();


补充说明:原脚本中的支付密码键盘坐标可能需要根据实际设备调整,不同手机分辨率会导致点击位置偏移。
ℹ️ 本回答由 AI 生成,内容仅供参考