HomeApe.js 10.1 KB
//*
// 首页模块
// */

import Loger from "../Loger";
import Ape from "./Ape";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import MessageTypes from "../MessageTypes";
const classifyList = `<div class="classifyList {_classifyListR}" data="{_id}">
                            <img class="curriculumListImg" src="{_msgImg}" alt="">
                            <p class="curriculumListTit">{_classroomTitle}</p>
                            <p class="curriculumListInfo">
                                <span class="curriculumListTime">{_schoolTime}</span>
                                <span class="curriculumListNum">{_subscribeNum}</span>
                            </p>
                        </div>`;

const teacherList = `<div class="classifyList {_classifyListR}" data="{_id}">
                            <img class="curriculumListImg" src="{_msgImg}" alt="">
                            <p class="curriculumListTit">{_classroomTitle}</p>
                            <p class="curriculumListInfo">
                                <span class="curriculumListTime">{_schoolTime}</span>
                                <span class="curriculumListNum">{_subscribeNum}</span>
                            </p>
                        </div>`;
let loger = Loger.getLoger('PC-HomeApe');
class HomeApe extends Ape {
    constructor() {
        super();
        this.classDateList = {};//课堂的所有数据
        //判断列表在右侧时取消边距
        this._judgeListPlace();
        this.isLocalStorage();
        this.addEvent();
        this.init();
    }

    init() {

        $('#header_list li').on('click',this.switchHeaderHandler.bind(this));

        $('#liveBtnMove').on('click',this.liveBtnMoveHandler.bind(this));//最新直播课程
        $('#lanclassBtnMove').on('click',this.lanclassBtnMoveHandler.bind(this));//最新互动课程
        $('#teacherBtnMove').on('click',this.teacherBtnMoveHandler.bind(this));//最新老师推荐

        $('#homeCenterBtn').on('click',this._homeCenterBtnHandler.bind(this));//个人中心
    }

    addEvent() {

    }
    isLocalStorage(){
        this.homeAllDate();
    }
    switchHeaderHandler(evt){
        $(evt.currentTarget).addClass('header_listMsgCheck').siblings().removeClass('header_listMsgCheck');
        let ind = $(evt.target).index();
        this._emit(MessageTypes.SWITCH_HEADER,ind);
    }
    liveBtnMoveHandler(){
        this.switchClass(1);
        this._emit(MessageTypes.SWITCH_HEADER,1);
    }
    lanclassBtnMoveHandler(){
        this.switchClass(2);
        this._emit(MessageTypes.SWITCH_HEADER,2);
    }
    teacherBtnMoveHandler(){
        this.switchClass(3);
        this._emit(MessageTypes.SWITCH_HEADER,3);
    }
    switchClass(ind){
        $('#header_list li').eq(ind).addClass('header_listMsgCheck').siblings().removeClass('header_listMsgCheck');
    }
    _judgeListPlace(){
        let i;
        let curriculumList = document.getElementsByClassName('curriculumList');
        for(i=0;i<curriculumList.length;i++){
            if((i%4) == 3){
                $(curriculumList[i]).addClass('classifyListR');
            }
        }
    }
    homeAllDate(){
        //直播信息
        this.liveModule();
        //互动信息
        this.interactModule();
        //老师信息
        this.teacherModule();
    }
    //直播信息
    liveModule(){
        let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/meeting/getMeeting';
        let data = {siteId:ClassDataProxy.siteId,meetingType:2};
        let that = this;
        this.detailPage(_url,data,8,1,function(_data){
            if(_data){
              //  loger.log('首页直播信息',_data)
                that._liveClassData(_data.returnData.data.meetingInfo);
            }
        })
    }
    //互动信息
    interactModule(){
        let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/meeting/getMeeting';
        let data = {siteId:ClassDataProxy.siteId,meetingType:1};
        let that = this;
        this.detailPage(_url,data,8,1,function(_data){
            if(_data){
               // loger.log('首页互动信息',_data)
                that._gainClassData(_data.returnData.data.meetingInfo);
            }
        })
    }
    //老师信息
    teacherModule(){
        let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/users/getUserByUserType';
        let data = {siteId:ClassDataProxy.siteId,userType :1}
        let that = this;
        this.detailPage(_url,data,8,1,function(_data){
            if(_data){
             //   loger.log('首页老师信息',_data)
               that._teacherDate(_data.returnData.data.users);
            }
        })
    }
    detailPage(url,data,num,ind,callback){
        let that = this;
        $.ajax({
            type: "POST",
            url: url,
            timeout:5000,
            data : data,
            headers: {siteId:ClassDataProxy.siteId,pageno:num, page:ind,
                'token':ClassDataProxy.token},
            success:function(_data){

                //获取我的课程数据
                if(_data && _data.code == 200){
                    if(callback){
                        callback(_data);
                    }
                }else{
                    if(callback){
                        callback(null);
                    }
                }
            },
            error:function(error){
                //alert('直播数据获取失败')
                if(callback){
                    callback(null);
                }
                loger.log(error,'直播数据获取失败')
            }
        })
    }
    _liveClassData(_data){
        let liveList = $('#liveList');
        liveList.empty();
        if(_data){
            for(let i = 0;i<_data.length;i++){
                if(i<8){
                    let j = (i%4)==3;
                    let _getTime = ClassDataProxy.getTimeDifference(_data[i].beginTime,_data[i].endTime)
                    let _img = '../images/u373.png';
                    let _classImg = _data[i].meetingPhoto;
                    let _classifyList = this._format(classifyList,{
                        _classifyListR : j?'classifyListR':'',
                        _id : _data[i].id,
                        _msgImg :_classImg?_classImg:_img,
                        _classroomTitle : _data[i].meetingName,
                        _schoolTime :  _getTime,
                        _subscribeNum : _data[i].reservationNumber+'人预约'
                    })
                    liveList.append(_classifyList);
                }
            }
        }
    }
    _gainClassData(_data){
        let lanclassList = $('#lanclassList');
        lanclassList.empty();
        if(_data){
            for(let i = 0;i<_data.length;i++){
                if(i<8){
                    let j = (i%4)==3;
                    let _getTime = ClassDataProxy.getTimeDifference(_data[i].beginTime,_data[i].endTime)
                    let _img = '../images/u373.png';
                    let _classImg = _data[i].meetingPhoto;
                    let _classifyList = this._format(classifyList,{
                        _classifyListR : j?'classifyListR':'',
                        _id : _data[i].id,
                        _msgImg :_classImg?_classImg:_img,
                        _classroomTitle : _data[i].meetingName,
                        _schoolTime :  _getTime,
                        _subscribeNum : _data[i].reservationNumber+'人预约'
                    })
                    lanclassList.append(_classifyList);
                }
            }
        }

    }
    _teacherDate(_data){
        let teacherIntro = $('#teacherIntro');
        teacherIntro.empty();
        if(_data){
            for(let i = 0;i<_data.length;i++){
                let j = (i%4)==3;
                let _getTime = ClassDataProxy.getTimeDifference(_data[i].create_time,_data[i].end_time);
                let _img = '../images/u373.png';
                let _classImg = _data[i].meetingPhoto;
                let _classifyList = this._format(teacherList,{
                    _classifyListR : j?'classifyListR':'',
                    _id : _data[i].id,
                    _msgImg :_classImg?_classImg:_img,
                    _classroomTitle : _data[i].user_name,
                    _schoolTime : _getTime.split('-')[0],
                    _subscribeNum : _data[i].reservationNumber+'人预约'
                })
                teacherIntro.append(_classifyList);
            }
        }
    }
    _gainClassDataErr(){
        let liveList = $('#liveList');
        let lanclassList = $('#lanclassList');
        let teacherIntro = $('#teacherIntro');
        liveList.empty();
        lanclassList.empty();
        teacherIntro.empty();

        let _classifyList = '<div class="dataHomeErrorTitle">数据获取失败~</div>'
        liveList.append(_classifyList)
        lanclassList.append(_classifyList)
        teacherIntro.append(_classifyList)
    }

    //首页点击 进入个人中心
    _homeCenterBtnHandler(){
        $('#homeUserLoginBtn').attr('data','');
        if(ClassDataProxy.userType == ClassDataProxy.USER_TYPE_0){
            this.toLogin();
        }else if(ClassDataProxy.userType == ClassDataProxy.USER_TYPE_8){
            $('#slideshow').hide();
            $('#defaultBox').hide();
            $('#classifyListBox').hide();
            $('#lanclassBox').hide();
            $('#liveContentAllBox').hide();
            $('#lanclassAllBox').hide();
            $('#teacherIntroAllBox').hide();
            $('#teacherDetailBox').hide();
            $('#myCenterBox').show();
        }
    }
    studentClick(){
        $('#slideshow').show();
        $('#defaultBox').show();
        $('.curriculumBox').show();
        $('#classifyListBox').hide();
        $('#teacherDetailBox').hide();
        $('#lanclassBox').hide();
        $('#myCenterBox').hide();
        $('#lanclassAllBox').hide();
    }
    toLogin(){
        ClassDataProxy.getMarginTopHandler($('#userLogin'));
        $('#userLoginBox').show();
    }
    //工具类
    _format(str, obj) {
        return str.replace(/\{(\w+)\}/g, function (match, group, index) {
            return obj[group];
        });
    };
}
export default HomeApe;