微信自动抢红包

toast("start!!");
device.keepScreenOn();
auto.waitFor();
// console.show();
// console.setPosition(0, 0);

events.observeNotification();
events.onNotification(function (notification) {
    log(notification.getText());
    if (notification.getPackageName() != "com.tencent.mm") return;
    if (notification.getText().indexOf("[微信红包]") != -1) {
        log("有红包!");
        notification.click();
        hongbao();
    }
});

function hongbao() {
    //log("in func hongbao");
    try {
        var a = text("微信红包").findOne(3000); //最长3s
        if (!a) return;
        var a = text("微信红包").find();
        for (var i = 0; i < a.size(); i++) {
            // log("8888888" + a[i].parent().parent().child(0).child(1).childCount());
            if (a[i].parent().parent().child(0).child(1).childCount() == 2) continue;
            a[i].parent().parent().parent().click();
            if (depth(10).text("手慢了,红包派完了").exists()) {
                depth(8).desc("返回").click();
                return;
            }
            desc("开").depth(9).click();
            sleep(1000);
            let money = depth(16).id("d_h").findOne().text();
            toastLog("本次抢红包金额:" + money);
            back();
        }
    } catch (error) {
        log("err in function hongbao : " + error);
    }
}
threads.start(function () {
    let flag = 1;
    while (true) {
        //log("1111111111111111111111111111");
        while (!device.isScreenOn() || currentPackage() != "com.tencent.mm") sleep(5000);
        while (visibleToUser(true).depth(11).desc("聊天信息").exists()) {
            hongbao();
            sleep(500);
        }

        t = depth(8).className("com.tencent.mm.ui.mogic.WxViewPager").findOnce();
        if (t) {
            t.refresh();
            var b = t.child(0).bounds();
            if (b.left == 0 && b.top == 0 && b.right == 1080 && b.bottom == 2340) {
                // log("main Page");
                list = t.child(0).child(0).child(1);
                try {
                    t.refresh();
                    //log(list.childCount());
                    if (list.childCount() == 0) {
                        swipe(800, 1030, 800, 1000, 80);
                        sleep(500);
                        //list.refresh();
                        flag = 0;
                        throw new Error("err list not refesh!");
                    }
                    for (let i = 0; i < list.childCount(); ++i) {
                        if (list.child(i).childCount() != 2) continue;
                        if (list.child(i).child(0).childCount() != 2) continue;
                        var txt = list.child(i).child(1).child(1).child(0).child(0).text();
                        //log(txt);
                        if (txt.indexOf("[微信红包]") != -1) {
                            list.child(i).click();
                            hongbao();
                            break;
                        }
                    }
                } catch (error) {
                    log("err in traverse listView : " + error);
                }
            }
        }
        if (flag)
            sleep(500);
        flag = 1;
    }   
 });

  

posted @ 2020-08-21 21:30  楼主好菜啊  阅读(70)  评论(0编辑  收藏  举报