编辑代码

const $ = new Env("群管易");
const axios = require("axios").default
const Notify = 0;
const split = "&"
const IS_PROXY = false;
const proxy_url = '';
const CryptoJS = require("crypto-js");
const token_name = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJSSzoiOiIxODUyNjI0NDAyODQ0NzQ5ODI1IiwiZXhwIjoxNzMxMTM5ODUyLCJpYXQiOjE3MzA1MzUwNTJ9.KXnWpPt1Q2Z8k13LPVlvDyGOlg1Ta09DI8VKGAqGxEg'; // xtoken变量
const aid_name = '6f385c998619413c81d05789d2695ae0'; // 活动id变量
const host = 'sg-api.zjt1.com';
const referer = 'https://sg-m.zjt1.com/';
function getRandom(n, m) {
    var result = Math.random() * (m + 1 - n) + n;
    while (result > m) {
        result = Math.random() * (m + 1 - n) + n;
    }
    return Math.round(result);
}
function AES_Encrypt(word) {
    var key = CryptoJS.enc.Utf8.parse("39fac687b17446f88de6f7cd395f47ca");
    var iv = CryptoJS.enc.Utf8.parse("5ad1ff0d20074e65");
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}
class MainProgram {
    constructor(index, token) {
        this.index = index;
        this.xtoken = token[0];
        this.host = host;
        this.referer = referer;
        this.aid = process.env[aid_name];
        this.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c0f) XWEB/8555 Flue',
            'Accept': 'application/json, text/plain, */*',
            'X-Token': this.xtoken,
            'Content-Type': 'application/json;charset=UTF-8',
            'Origin': this.referer,
            'Sec-Fetch-Site': 'same-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': this.referer,
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        this.questions = [];
        this.questionAnswerIds = {};
    }
    log(msg) {
        console.log(`[账号${this.index}:${this.nickName || ""}]:${msg}`);
    }
    push_question(a) {

        for (const i of a) {

            // let str = `${i.questionTitle}\n${i.questionAnswerDTOS[0].number}.${i.questionAnswerDTOS[0].name}\n${i.questionAnswerDTOS[1].number}.${i.questionAnswerDTOS[1].name}\n${i.questionAnswerDTOS[2].number}.${i.questionAnswerDTOS[2].name}\n${i.questionAnswerDTOS[3].number}.${i.questionAnswerDTOS[3].name}`

            let str = `${i.questionTitle}\n`;

            for (const question of i.questionAnswerDTOS) {

                str += `${question.number}.${question.name}\n`

            }



            // console.log(str)

            this.questions.push({ problem: i.questionTitle, content: str, options: i.questionAnswerDTOS })

        }

    }

    async update_proxy() {

        if (!IS_PROXY) return;

        let res = await $.get(proxy_url);

        let tmp = res.split(":");

        this.proxy = {

            protocol: 'http',

            host: tmp[0],

            port: tmp[1]

        }

        this.log(JSON.stringify(this.proxy))

    }



    update_date(t) {

        function get_iso_date(date) {

            const year = date.getFullYear();

            const month = String(date.getMonth() + 1).padStart(2, '0');

            const day = String(date.getDate()).padStart(2, '0');

            const hours = String(date.getHours()).padStart(2, '0');

            const minutes = String(date.getMinutes()).padStart(2, '0');

            const seconds = String(date.getSeconds()).padStart(2, '0');

            return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;

        }



        let date = new Date();

        this.videoPlayStartTime = get_iso_date(date);

        date.setMilliseconds(date.getMilliseconds() + t * 1000)

        this.videoPlayEndTime = get_iso_date(date);

    }



    async answerDetail() {

        let url = `https://${this.host}/applet/live/answerDetail?id=${this.aid}`;



        let data = { "dataJson": AES_Encrypt("{}") };

        let headers = this.headers;

        let res = await $.post(url, data, { headers }, 5);

        this.title = res.data.title;

        if (!this.title) return;

        this.log(`title -> ${this.title}`)

        this.courseId = res.data.courseId

        this.seconds = res.data.seconds

        this.shareId = res.data.shareId

        this.groupMgrId = res.data.groupMgrId

        this.dealerId = res.data.dealerId

        this.periodId = res.data.periodId

        this.groupMgrId = res.data.groupMgrId

        this.wxappEntityId = undefined

        this.videoPlayStartTime = undefined

        this.videoPlayEndTime = undefined

        this.courseStartDate = res.data.playStartTime

        this.courseEndDate = res.data.playEndTime

        this.videoId = res.data.videoEntityId



        this.q_index = 0;  // 当前题目索引

        this.push_question(res.data.appletQuestionDtos);



    }



    async getDealerConfig() {

        let url = `https://${this.host}/applet/dealer-member/getDealerConfig?dealerId=${this.dealerId}`;

        let headers = this.headers;

        let res = await $.get(url, { headers }, 5);

        this.log(res)

        this.wxappEntityId = res.data.wxappEntityId;

    }



    async updateUserInfo() {

        let url = `https://${this.host}/applet/updateUserInfo`;

        let data = { "dataJson": AES_Encrypt("{}") };

        let headers = this.headers;

        let res = await $.post(url, data, { headers }, 5);

        this.nickName = res.data.nickName

        this.id = res.data.id // createBy

    }

    async send_answer2(i) {

        this.questionAnswerIds = {};

        await $.wait(getRandom(10, 20));

        let answer = answers[this.q_index++];



        this.log(`${i.problem} -> ${answer}`);



        let a = this.questions.find(c => c.problem == i.problem);

        let b = a.options.find(c => c.number == answer)

        if(!b) return this.isEnd = true;

        this.questionAnswerIds[b.questionId] = [b.id]

    }



    async videoProcessV2(durationTime) {

        await $.wait(!durationTime ? 1 : durationTime);

        let url = `https://${this.host}/applet/anchorPoint/videoProcessV2`;

        this.update_date(!durationTime ? 1 : durationTime);

        let data = {

            "dataJson": AES_Encrypt(JSON.stringify({

                "data": {

                    "courseId": this.courseId,

                    "playFlag": 2,

                    "shareId": this.shareId,

                    "wxappEntityId": this.wxappEntityId,

                    "createBy": this.id,

                    "groupMgrId": this.groupMgrId,

                    "dealerId": this.dealerId,

                    "videoPlayStartTime": this.videoPlayStartTime,

                    "videoPlayEndTime": this.videoPlayEndTime,

                    "courseStartDate": this.courseStartDate,

                    "courseEndDate": this.courseEndDate,

                    "isLiveCycle": 0,

                    "durationTime": !durationTime ? 0 : durationTime,

                    "videoId": this.videoId,

                    "periodPercent": 90,

                    "periodInterval": 600

                }

            }))

        };

        let headers = this.headers;

        let res = await $.post(url, data, { headers }, 5);

        this.log(`videoProcessV2 -> ${JSON.stringify(res)}`)

    }



    async getWatchSeconds() {

        let url = `https://${this.host}/applet/getWatchSeconds`;

        this.update_date(46);

        let data = {

            "dataJson": AES_Encrypt(JSON.stringify({

                "data": {

                    "createBy": this.id,

                    "courseId": this.courseId

                }

            }))

        };

        let headers = this.headers;

        let res = await $.post(url, data, { headers }, 5);

        this.log(`getWatchSeconds -> ${JSON.stringify(res)}`)



        this.current_seconds = res.data



    }



    async answerQuestions() {

        let url = `https://${this.host}/applet/answerQuestions`;

        let data = {

            "dataJson": AES_Encrypt(JSON.stringify({

                "data": {

                    "courseId": this.courseId,

                    "shareId": this.shareId,

                    "videoCode": "",

                    "videoId": this.videoId,

                    "questionAnswerIds": this.questionAnswerIds,

                    "createBy": this.id,

                    "groupMgrId": this.groupMgrId,

                    "dealerId": this.dealerId,

                    "wxappEntityId": this.wxappEntityId,

                    'playType': 0

                }

            }))

        };

        this.log(this.questionAnswerIds)

        let headers = this.headers;

        let res = await $.post(url, data, { headers }, 5);

        this.log(JSON.stringify(res));

        if (res.data.newMemberMoney || res.message.includes("已领取") || res.message.includes("超过预设次数") || res.message.includes("已经提交过正确答案")) {

            this.isEnd = true;

        }

    }



    async doTask() {

        

        await this.updateUserInfo();

        if (!this.nickName) {

            return this.log("token过期");

        }

        await this.answerDetail();



        try {

            await this.getDealerConfig();

        } catch (e) {

            return this.log(`id错误 => ${this.aid}`);

        }

        await this.getWatchSeconds();



        let t = getRandom(50, 60);

        for (let i = 0; i < (this.seconds - this.current_seconds) / t; i++) {

            await this.videoProcessV2(t);

        }

        await this.videoProcessV2(0);





        for (let i = 0; i < 4; i++) {

            await this.send_answer2(this.questions[0]);

            if (this.isEnd) break

            await this.answerQuestions();

            if (this.isEnd) break

        }



    }

}



(async () => {

    let arr = $.getToken(token_name, "#");

    if (!arr) return await $.SendMsg("未填写token");

    let promises = [];

    for (let index = 0; index < arr.length; index++) {

        $.token = arr[index];

        promises.push(new MainProgram(index + 1, $.token).doTask());

    }

    Promise.all(promises);

    await $.SendMsg($._msg);

})();



function Env(name) {

    return new (class {

        constructor(name) {

            this.name = name;

            console.log(`\ud83d\udd14${this.name},\u5f00\u59cb!`);

        }



        async update_proxy() {

            if (!IS_PROXY) return;

            let res = (await axios.get(proxy_url)).data;

            let tmp = res.split(":");

            this.proxy = {

                protocol: 'http',

                host: tmp[0],

                port: tmp[1]

            }

            console.log(this.proxy)

        }



        async get(url, params, i = 5, s) {

            if (!i) return {}

            if (!IS_PROXY) delete params.proxy;

            if (IS_PROXY && !params.proxy && !this.proxy.host) await this.update_proxy(), params.proxy = this.proxy;

            if (IS_PROXY) params.proxy = this.proxy;

            try {

                let res = (await axios.get(url, params)).data;

                if (s && JSON.stringify(res).includes(s)) this.update_proxy();

                return res;

            } catch (err) {

                console.log(`get ${url} :${err.message}`);

                await $.wait(5)

                await this.update_proxy()

                params.proxy = this.proxy;

                return this.get(url, params, --i);

            }

        }



        async post(url, data, params, i = 5, s) {

            if (!i) return {}

            if (!IS_PROXY) delete params.proxy;

            if (IS_PROXY && !params.proxy && !this.proxy.host) await this.update_proxy(), params.proxy = this.proxy;

            if (IS_PROXY) params.proxy = this.proxy;

            try {

                // console.log(url,this.proxy)

                //console.log("proxy",params.proxy)

                let res = (await axios.post(url, data, params)).data;

                if (s && JSON.stringify(res).includes(s)) this.update_proxy();

                return res

            } catch (err) {

                console.log(`post ${url} :${err.message}`);

                await $.wait(5)

                await this.update_proxy()

                params.proxy = this.proxy;

                return this.post(url, data, params, --i);

            }

        }



        async SendMsg(message) {

            if (!message) return;

            if (Notify > 0) {

                var notify = require("./sendNotify");

                await notify.sendNotify(this.name, message);

            }

        }



        addMsg(msg) {

            if (!this._msg) this._msg = "";

            console.log(msg);

            this._msg += msg + "\n";

        }



        wait(delay) {

            return new Promise((res) => {

                setTimeout(res, delay * 1000);

            });

        }



        getToken(key, s) {

            let tmp = process.env[key];

            let arr = [];

            if (!tmp) return "";



            if (s) {

                for (const _ of tmp.split(split)) {

                    arr.push(_.split(s))

                }

            }

            return arr.length > 0 ? arr : "";

        }



        done() {

            console.log(`\ud83d\udd14${this.name},\u7ed3\u675f!`);

        }

    })(name);

}