编辑代码

//1111
importClass('android.database.sqlite.SQLiteDatabase');
importClass("android.content.ContentValues");
importClass("android.content.Context");
importClass("android.database.Cursor");

let minTime = 15000;
let maxTime = 30000;
let zhengDianState = 0;

try {
    main()
} finally {
    console.hide();
}

function main() {
    console.show();
    console.setPosition(device.width / 3, device.height / 2)
    try {
        goBuyFund();
        changeOrder(1);
    } catch (e) {
        log("启动出错了" + e)
    }
    let runNum = 0;
    while (true) {
        let startTime = new Date().getTime();
        try {
            sleep(1000)
            goBuyFund();
            getHongBao();
        } catch (e) {
            log("跳转页面出错了" + e)
        }
        let endTime = new Date().getTime();
        let runTime = (endTime - startTime) / 1000
        toastLog("运行时间:" + runTime)
        if (runTime < 40) {
            if (minTime > 90000) {
                home();
            }
            waitTimes=random(minTime, maxTime)
            toastLog("等待" + waitTimes/1000+"秒")
            sleep(waitTimes);
            let oNow = new Date();
            let nowtime = oNow.getTime();
            //获取下一个小时
            let h = oNow.getHours();
            // 获取下一个小时的时间戳
            let oWeek = oNow.getDay();//0\1-6
            let year = oNow.getFullYear()
            let month = oNow.getMonth() + 1
            let day = oNow.getDate()
            let date = year + '/' + month + '/' + day
            // var end = new Date(new Date(new Date().toLocaleDateString()).getTime() + h * 60 * 60 * 1000 - 1).getTime();
            var end = new Date(new Date(date).getTime() + (h + 1) * 60 * 60 * 1000 - 1).getTime();

            var timing = end - nowtime;
            timePlus(timing, h, oWeek);
        } else {
            minTime = 5000;
            maxTime = 15000;
        }
    }
}

function timePlus(timing, h, oWeek) {
    log("星期" + oWeek + ",时间" + h + ",距整点差" + (timing / 1000 / 60).toFixed(2) + "分");
    if (zhengDianState) {
        minTime = 15000;
        maxTime = 30000;
        zhengDianState = 0
        return
    }
    if (timing + 60000 > 240000) {
        if (oWeek == 0 || oWeek == 6 || h > 22 || h < 8) {
            if (h < 8 && h > 0) {
                log("睡眠时间")
                sleep(20 * 60 * 1000)
                minTime = 300000;
                maxTime = 500000;
            }
            log("延长时间")
            minTime >= 200000 ? minTime = 200000+ random(-100000, 100000) : minTime = minTime + random(30000, 60000);
            maxTime >= 350000 ? maxTime = 350000+ random(-100000, 100000) : maxTime = maxTime + random(30000, 60000);
            //minTime < 15000 ? minTime = 15000 : minTime = minTime;
            //maxTime < 30000 ? maxTime = 30000 : maxTime = maxTime;
        } else {
            log("正常时间")
            minTime >= 150000 ? minTime = 150000+ random(-50000, 50000) : minTime = minTime + random(10000, 40000);
            maxTime >= 250000 ? maxTime = 250000+ random(-50000, 50000) : maxTime = maxTime + random(10000, 40000);
            //minTime < 15000 ? minTime = 15000 : minTime = minTime;
            //maxTime < 30000 ? maxTime = 30000 : maxTime = maxTime;
        }
    } else {
        minTime = timing + random(0, 10000);
        maxTime = timing + random(0, 20000);
        zhengDianState = 1;
    }

}

function getHongBao() {
    let dWith = device.width / 2;
    let dheight = device.height / 5;
    let x1 = random(dWith - 100, dWith + 100);
    let y1 = random(dheight, dheight + 100);
    let x2 = random(dWith - 100, dWith + 100);
    let y2 = random(dheight * 3, dheight * 3 + 100);
    let duration = random(800, 1100);
    sml_move(x1, y1, x2, y2, duration);
    sleep(4500);

    let answer = text("答题有礼").find();
    //let fans = text("粉丝福利").find();
    let jiJin = ""

    if (!answer.empty()) {
        toastLog("找到答题有礼!")
        for (let i = 0; i < answer.length; i++) {
            answer[i].click();
            //2.7
            let oZhi = textStartsWith("亲密度值").findOne(10000);
            jiJin = textEndsWith("财富号").findOnce();
            if (jiJin) {
                jiJin = jiJin.text();
            }
            // if (!oZhi) {
            //     textEndsWith("粉丝").findOnce();
            //     toastLog("进入财富号!")
            //     sleep(500)
            //     let heig = textStartsWith("首").findOnce();
            //     if (heig) {
            //         toastLog("进入粉丝中心")
            //         click(device.width / 2, heig.bounds().centerY() - 5);
            //     } else {
            //         click(device.width / 2, device.height - 150);
            //     }
            //     sleep(500);
            // }
            goPage(jiJin);
        }
    }
    // if (!fans.empty()) {
    //     toastLog("找到粉丝福利!")
    //     for (let i = 0; i < fans.length; i++) {
    //         sleep(2000)
    //         fans[i].click();
    //         //2.7
    //         let oZhi = textStartsWith("亲密度值").findOne(10000);
    //         jiJin=textEndsWith("财富号").findOnce();
    //         if(jiJin){
    //             jiJin=jiJin.text();
    //         }
    //         if (!oZhi) {
    //             textEndsWith("粉丝").findOnce();
    //             toastLog("进入财富号!")
    //             sleep(500)
    //             let heig = textStartsWith("首").findOnce();
    //             if (heig) {
    //                 toastLog("进入粉丝中心")
    //                 click(device.width / 2, heig.bounds().centerY() - 5);
    //             } else {
    //                 click(device.width / 2, device.height - 150);
    //             }
    //             sleep(500);
    //         }
    //         toastLog("进入粉丝中心")
    //         goPage(jiJin)
    //     }
    // }
}

function goPage(jiJin) {
    let w = text("答题领取").findOne(5000);
    sleep(800)
    let daTis = text("答题领取").find()
    let allDaTiCount = daTis.length
    log("共找到" + allDaTiCount + "个红包")

    if (allDaTiCount == 1) {
        text("答题领取").findOne(5000).click();
        grodRed(jiJin)
    } else if (allDaTiCount > 1) {
        for (let i = allDaTiCount; i > 0; i--) {
            log("第" + i + "个红包")
            let redBtn = text("答题领取").findOnce(i - 1);
            if (redBtn) {
                redBtn.click()
                grodRed(jiJin)
                sleep(500)
            }
        }
    }
    sleep(500)
    let chi = textContains("我的持有").findOne(1000);
    if (!chi) {
        back()
    }
    chi = textContains("我的持有").findOne(3000);
    if (!chi) {
        log("返回基金列表失败,重试")
        back()
        sleep(1000);
        chi = textContains("我的持有").findOnce();
    }
}

function grodRed(jiJin) {
    //sleep(1000)
    textStartsWith("第1题").findOne(5000);
    let questionCount = className("android.view.View").depth(6).indexInParent(3).findOne().text() * 1;
    log("共" + questionCount + "题")
    for (let i = 1; i <= questionCount; i++) {
        sleep(1000)
        log("第" + i + "题")
        let questionInfo = getQuestion(jiJin);
        //log(questionInfo)
        let question = questionInfo.get("question");
        let SQLAnswer = findAnswer(question);
        let JSAnswer = "";
        if (SQLAnswer) {
            toastLog("SQLAnswer:" + SQLAnswer)
            let answerBtn = text(SQLAnswer).findOnce();
            if (answerBtn) {
                text(SQLAnswer).findOnce().click()
                sleep(1000)
                let thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
                let oNow = new Date();
                let h = oNow.getHours();
                if (text(SQLAnswer).findOnce() && thisQuestionCount == i) {
                    JSAnswer = clickAnswer(questionInfo, i)
                    if (JSAnswer) {
                        toastLog("JSAnswer:" + JSAnswer)
                        questionInfo.put("answer", java.lang.String(JSAnswer));
                        updateSQL(questionInfo)
                    } else {
                        toastLog("答题领取红包失败!")
                    }
                }
            } else {
                JSAnswer = clickAnswer(questionInfo, i)
                if (JSAnswer) {
                    toastLog("JSAnswer:" + JSAnswer)
                    questionInfo.put("answer", java.lang.String(JSAnswer));
                    updateSQL(questionInfo)
                } else {
                    toastLog("答题领取红包失败!")
                }
            }
        } else {
            JSAnswer = clickAnswer(questionInfo)
            if (JSAnswer) {
                toastLog("JSAnswer:" + JSAnswer)
                questionInfo.put("answer", java.lang.String(JSAnswer));
                updateSQL(questionInfo)
            } else {
                toastLog("答题领取红包失败!")
            }
        }
    }
    sleep(500)
    text("距离红包失效还有").findOne(2000);
    log("答题红包领取完成!")
    sleep(500)
    back();
}

function clickAnswer(questionInfo, i) {
    let answer = "";
    let option_1 = questionInfo.get("option_1");
    let option_2 = questionInfo.get("option_2");
    let option_3 = questionInfo.get("option_3");
    let option_4 = questionInfo.get("option_4");
    let thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
    let option_1s=text(option_1).find();
    text(option_1).findOnce(option_1s.length-1).click()
    sleep(3000)
    thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
    if (text(option_1).findOnce() && thisQuestionCount == i) {
        let option_2s=text(option_2).find();
        text(option_2).findOnce(option_2s.length-1).click()
        sleep(3000)
        thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
        if (text(option_2).findOnce() && thisQuestionCount == i) {
            if (option_3) {
                text(option_3).findOnce().click()
                sleep(3000)
                thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
                if (text(option_3).findOnce() && thisQuestionCount == i) {
                    text(option_4).findOnce().click()
                    sleep(3000)
                    thisQuestionCount = className("android.view.View").depth(6).indexInParent(1).findOne().text() * 1;
                    if (text(option_4).findOnce() && thisQuestionCount == i) {
                        return answer
                    } else {
                        answer = option_4
                    }
                } else {
                    answer = option_3
                }
            } else {
                return answer
            }
        } else {
            answer = option_2
        }
    } else {
        answer = option_1
    }
    return answer
}

function findAnswer(question) {
    db = context.openOrCreateDatabase("test5.db", Context.MODE_PRIVATE, null);
    db.execSQL("create table if not exists fund_question (id integer primary key autoincrement,fund_name varchar(20),question varchar(100),option_1 varchar(20)," +
        "option_2 varchar(20),option_3 varchar(20),option_4 varchar(20),answer varchar(20),no_answer varchar(20),UNIQUE(fund_name,question));")
    var c = db.query("fund_question", null, "question='" + question + "'", null, null, null, null, null);
    let answer = "";
    if (c.getCount() > 0) {
        c.moveToFirst();
        answer = c.getString(c.getColumnIndex("answer"));
    }
    db.close();
    return answer

}

function updateSQL(questionInfo) {
    db = context.openOrCreateDatabase("test5.db", Context.MODE_PRIVATE, null);
    db.execSQL("create table if not exists fund_question (id integer primary key autoincrement,fund_name varchar(20),question varchar(100),option_1 varchar(20)," +
        "option_2 varchar(20),option_3 varchar(20),option_4 varchar(20),answer varchar(20),no_answer varchar(20),UNIQUE(fund_name,question));")
    db.execSQL("DELETE FROM  fund_question  WHERE question='" + questionInfo.get("question") + "'");
    db.insert("fund_question", null, questionInfo);
    db.close()
    // var c = db.query("fund_question", null, null, null, null, null, null, null);
    // toastLog(c.getColumnNames())//获取列名数组
    // while (c.moveToNext()) {
    //     var id = c.getInt(c.getColumnIndex("id"));
    //     var question = c.getString(c.getColumnIndex("question"));
    //     var option_1 = c.getString(c.getColumnIndex("option_1"));
    //     var option_2 = c.getString(c.getColumnIndex("option_2"));
    //     var option_3 = c.getString(c.getColumnIndex("option_3"));
    //     var option_4 = c.getString(c.getColumnIndex("option_4"));
    //     var answer = c.getString(c.getColumnIndex("answer"));
    //     toastLog(c.getString(1) + "数据库 id=" + id + " question=" + question + " option_1= " + option_1 + " option_2= " + option_2 + " option_3= " + option_3 + " option_4= " + option_4 + " answer=" + answer);
    // }

}

function getQuestion(jiJin) {
    let list = text("学习养基技能领福利").findOnce();
    if (list) {
        let str = [];
        list.parent().children().forEach(function (chi) {
            let tv = chi.findOne(depth(6))
            if (tv) {
                if (tv.text() != "") {
                    let text6 = tv.text()
                    str.push(text6)
                }
            }
        });
        let ti = textMatches(/\第\d\题\:/).findOnce();
        let tiIndex = 0
        if (ti) {
            tiIndex = ti.indexInParent() * 1
        }
        let tiShi = text("温馨提示").findOnce();
        let tiShiIndex = 0
        if (tiShi) {
            tiShiIndex = tiShi.indexInParent() * 1
        }
        let cv = new ContentValues();
        if (tiIndex > 0 && tiShiIndex > 0) {
            cv.put("fund_name", java.lang.String(jiJin));
            cv.put("question", java.lang.String(str[ti.indexInParent() * 1 + 1]));
            cv.put("option_1", java.lang.String(str[ti.indexInParent() * 1 + 2]));
            cv.put("option_2", java.lang.String(str[ti.indexInParent() * 1 + 3]));
            if (ti.indexInParent() * 1 + 4 < tiShi.indexInParent() * 1) {
                cv.put("option_3", java.lang.String(str[ti.indexInParent() * 1 + 4]));
            }
            if (ti.indexInParent() * 1 + 5 < tiShi.indexInParent() * 1) {
                cv.put("option_4", java.lang.String(str[ti.indexInParent() * 1 + 5]));
            }
        }
        return cv
    }
}

function changeOrder(i) {
    textContains("我的持有").findOnce().click();
    className("CheckBox").findOne(5000);
    sleep(500);
    let order = className("CheckBox").findOnce(i);//0风险等级视图,1基金公司视图,2单个基金视图
    if (!order.checked()) {
        order.click()
    } else {
        text("关闭").findOnce().click();
    }
}

function goBuyFund() {
    if (textContains("我的持有").exists()) {
        toastLog("支付宝已启动")
        return;
    } else {
        back();
        toastLog("启动支付宝")
        goAli();
        textContains("我的持有").findOne(5000)
        toastLog("支付宝启动完成");
    }
}

function goAli() {
    home();
    text("支付宝").findOne(2000) ? text("支付宝").findOnce().click() : toast("未找到支付宝");
    textContains("我的持有").findOne(1000);
    if (textContains("我的持有").exists()) {
        toastLog("支付宝已启动")
        return;
    }
    let fund = text("理财").depth(2).findOne(1000);
    sleep(500)
    if (!fund) {
        while (desc("返回").exists()) {
            desc("返回").findOnce().click()
            sleep(500)
            fund = text("理财").depth(2).findOne(1000);
            sleep(500)
        }
    }
    if (text("理财").depth(2).exists()) {
        sleep(500)
        text("理财").depth(2).findOnce().parent().click()
    }
    sleep(500)
    text("总资产(元)").depth(4).findOne(2000).parent().click()
    sleep(500)
    let jijin = text("基金").findOne(10000)
    sleep(500)
    text("基金").findOnce().parent().click()
}


//此代码由飞云脚本圈整理提供(www.feiyunjs.com)
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    ;
    // log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    //log(xxy);
    gesture.apply(null, xxy);
};
//1111