• 青果教务系统适配小爱课程表


    前言:

    课程表的适配主要是对dom层的提取归类处理,本人技术水平较低,这里简单分享下自己的代码

    仓库地址小爱课程表

    过程:

    课程表的适配主要是通过操作scheduleHtmlProvider,scheduleHtmlParser,scheduleTimer函数完成的,他们的作用分别是获取dom,处理dom,设置课表节次时间。

    首先获取课程表内容,这里我使用js获取接口内容。

    scheduleHtmlProvider函数:

    在这里插入图片描述

    function scheduleHtmlProvider(iframeContent = "", frameContent = "", dom = document) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://jwgl.hist.edu.cn/wsxk/xkjg.ckdgxsxdkchj_data10319.jsp?params=eG49MjAyMiZ4cT0wJnhoPTIwMjEwMDAwNTExMg==', false);
        xhr.send();
        return xhr.responseText;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    scheduleHtmlParser函数:

    function scheduleHtmlParser(html){
        let course = [];
        const $ = cheerio.load(html, {decodeEntities: false}); // 避免中文乱码
        $(' tbody>tr ').each(function () {
            //大杂烩
            var name = $(this).eq(0).children(":eq(1)").text();
            //这是课程名字
            console.log("name"+name)
            var teacher = $(this).eq(0).children(":eq(5)").text();
            //这是教师名字
            var allData = $(this).eq(0).children(":eq(8)").text();
            //数据位置大杂烩
            var sumTime = allData.split(",");
            var sumLength = sumTime.length;
            //时间个数
            console.log("输出时间个数:"+sumLength);
    
    
            if (sumLength == 1) {
    
                console.log("allTime"+allTime)
    
                //在这里提前取出来单双周
                var flag = 0;
    
                if (sumTime[0].search("单")!=-1){
                    flag = 1;
                    sumTime[0]=sumTime[0].replace('(单)', '');
                }
                else if (sumTime[0].search("双")!=-1) {
                    flag =2;
                    sumTime[0]=sumTime[0].replace('(双)', '');
                }
                console.log("flag的值"+flag);
                var allTime = sumTime[0].split(" ");
                var weeks = getWeeks(sumTime[0],flag);
                var day = getDay(sumTime[0]);
                var sections = getSection(sumTime[0]);
                var position = allTime[allTime.length-1];
    
    
    
                var obj = {
                    name: name,
                    position: position,
                    teacher: teacher,
                    weeks: weeks,
                    day: day,
                    sections: sections,
                }
                course.push(obj);
    
    
            }
            else if (sumLength > 1) {
                var i = 0;
                while (i <= sumLength-1) {
                    console.log("第i段时间"+i);
    
                    //在这里提前取出来单双周
                    var flag = 0;
    
                    if (sumTime[i].search("单")!=-1){
                        flag = 1;
                        sumTime[i]=sumTime[i].replace('(单)', '');
                    }
                    else if (sumTime[i].search("双")!=-1) {
                        flag =2;
                        sumTime[i]=sumTime[i].replace('(双)', '');
                    }
                    var allTime = sumTime[i].split(" ");
                    var weeks = getWeeks(allTime[0],flag);
                    var day = getDay(allTime[i]);
                    var sections = getSection(sumTime[i]);
                    var position = allTime[allTime.length-1];
    
                    var obj = {
                        name: name,
                        position: position,
                        teacher: teacher,
                        weeks: weeks,
                        day: day,
                        sections: sections,
                    }
                    course.push(obj);
                    i++;
    
    
                }
            }
        })
        console.log(course)
        return course;
    }
    
    
    function getDay(strDay){
            var chineseDay = strDay.slice(0,1);
            if (chineseDay.search("一") != -1 ){
                return 1;
            }
            else if (chineseDay.search("二") != -1){
                return 2;
            }
            else if (chineseDay.search("三") != -1){
                return 3;
            }
            else if (chineseDay.search("四") != -1){
                return 4;
            }
            else if (chineseDay.search("五") != -1){
                return 5;
            }
            else if (chineseDay.search("六") != -1){
                return 6;
            }
            else if (chineseDay.search("日") != -1){
                return 7;
            }
    
        }
    
    
    function getWeeks(weekStr,flag){
        var week = [];
        weekStr = weekStr.replace('周','');
    
        if (flag == 1) {
            week = weekStr2IntList(weekStr).filter(v => v % 2 != 0);
        } else if (flag == 2) {
            week = weekStr2IntList(weekStr).filter(v => v % 2 == 0);
        } else {
            week = weekStr2IntList(weekStr);
        }
        return week;
    
    }
    function weekStr2IntList(week) {
        // 将全角逗号替换为半角逗号
        var a = week.split('-');
        var index = a[0];
        var end = a[1];
        let weeks =[];
        for(i = index;i<=end;i++){
            weeks.push(i);
        }
        return weeks;
    
    }
    
    
    function getSection(strSection){
        var sections = [];
        var indexSection = strSection.search("[");
        var section = strSection.slice(indexSection+1,indexSection+2);
        console.log("section"+section)
        sections.push(parseInt(section),parseInt(section)+1);
        return sections;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159

    scheduleTimer函数:

    这个函数主要是进行了夏冬两季的时间填充,通过时间返回夏冬的课程表时间。

    /**
     * 时间配置函数,此为入口函数,不要改动函数名
     */
    async function scheduleTimer({
                                     providerRes,
                                     parserRes
                                 } = {}) {
    
        var selections=[];
        var sumSections = [
            { section: 1, startTime: "08:00", endTime: "08:45" },
            { section: 2, startTime: "08:88", endTime: "9:40" },
            { section: 3, startTime: "10:10", endTime: "10:55" },
            { section: 4, startTime: "11:05", endTime: "11:50" },
            { section: 5, startTime: "15:00", endTime: "15:45" },
            { section: 6, startTime: "15:55", endTime: "16:40" },
            { section: 7, startTime: "17:10", endTime: "17:55" },
            { section: 8, startTime: "18:05", endTime: "18:50" },
            { section: 9, startTime: "20:00", endTime: "20:45" },
            { section: 10, startTime: "20:55", endTime: "21:40"}
        ]
        var winSections=[
            { section: 1, startTime: "08:00", endTime: "08:45" },
            { section: 2, startTime: "08:88", endTime: "9:40" },
            { section: 3, startTime: "10:10", endTime: "10:55" },
            { section: 4, startTime: "11:05", endTime: "11:50" },
            { section: 5, startTime: "14:30", endTime: "15:15" },
            { section: 6, startTime: "15:25", endTime: "16:10" },
            { section: 7, startTime: "16:40", endTime: "16:10" },
            { section: 8, startTime: "17:35", endTime: "18:20" },
            { section: 9, startTime: "19:30", endTime: "20:15" },
            { section: 10, startTime: "20:25", endTime: "21:10"}
    
        ]
        var myDate = new Date();//获取系统当前时间
        var nowMonth=myDate.getMonth();
        if(nowMonth>=5&&nowMonth<=9){
            selections=sumSections;
        }
        else {
            selections=winSections;
        }
        return {
            totalWeek: 20, // 总周数:[1, 30]之间的整数
            startSemester: '', // 开学时间:时间戳,13位长度字符串,推荐用代码生成
            startWithSunday: false, // 是否是周日为起始日,该选项为true时,会开启显示周末选项
            showWeekend: true, // 是否显示周末
            forenoon: 4, // 上午课程节数:[1, 10]之间的整数
            afternoon: 4, // 下午课程节数:[0, 10]之间的整数
            night: 2, // 晚间课程节数:[0, 10]之间的整数
            sections:sumSections, // 课程时间表,注意:总长度要和上边配置的节数加和对齐
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    导入成功:

    因为能力有限,不能适配的很完美,一起学习一起进步。
    在这里插入图片描述

  • 相关阅读:
    redis-migrate-tool数据迁移工具测试
    【深度学习21天学习挑战赛】9、生成对抗网络(GAN)手写数字生成
    【SQLite】一、SQLite简介——MySQL的简洁版
    力扣每日一题 1106. 解析布尔表达式
    PyQt5快速开发与实战 5.3 多线程
    01- 从零开始完整实现-循环神经网络RNN
    998. Maximum Binary Tree II
    力扣(LeetCode)82. 删除排序链表中的重复元素 II(C语言)
    关于wake on lan远程唤醒主机的问题,长时间关机无法远程唤醒
    SSM框架--Spring配置文件
  • 原文地址:https://blog.csdn.net/faker1234546/article/details/126328155