pageHome.js 7.98 KB
//注意:导航 依赖 element 模块,否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element;
});
var recentlyDay;
function getDate() {
    var date = new Date();
    var base = new Date(date).getTime();
    var oneDay = 24 * 3600 * 1000;
    var date = [];
    var data = [Math.random() * 300];
    var time = new Date(base);
    var mon = (time.getMonth() + 1) >= 10 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1);
    var day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate();
    date.push([time.getFullYear(), mon, day].join('/'));
    for (var i = 1; i < 7; i++) {//控制需要的天数
        var now = new Date(base -= oneDay); //这里控制往前一周还是往后一周
        var nowMon = (now.getMonth() + 1) >= 10 ? now.getMonth() + 1 : '0' + (now.getMonth() + 1);
        var nowDay = now.getDate() >= 10 ? now.getDate() : '0' + now.getDate();
        date.push([now.getFullYear(), nowMon, nowDay].join('/'));
        data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
    }
    return date.reverse();
}

//初始化方法
$(function () {
    $('.loadheader').hide();
    addTabs('#index');
    initData();
    recentlyDay = getDate();
    NewsData();
    NoticeData();
    LawsData();
    policiesData();
});
function initData() {
    // 在这里处理置顶的头部
    let topTitle = $('.top-title').html();
    if(strLength(topTitle)>7) {
        let html = topTitle.slice(0, 7) + '<br/>' + topTitle.slice(7);
        $('.top-title').html(html)
    }
    $('#appointment_inquiry').click(function () {
        if (isLogin()) {
            window.open('./staticViews/wdyy.html', '_self')
        }
    })
    $('#onlineBooking').click(function () {
        if (isLogin()) {
            window.open('./staticViews/wdyy.html', '_self')
        }
    })
    // 新闻头部切换
    $('.news-title .item-title').click (function() {
        $('.news-title .item-title').removeClass('news-active');
        $(this).addClass('news-active');
        let indexId = $(this).attr('data-id');
        $('.item-list').hide();
        if (indexId =='1') {
            $('.hotspot-list').show();
        }else {
            $('.news-information-list').show();
        }
    })
}
// 获取汉字长度
function strLength(str) {
    ///<summary>获得字符串实际长度,中文2,英文1</summary>
    ///<param name="str">要获得长度的字符串</param>
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128)
            realLength += 1;
        else
            realLength += 2;
    }
    return realLength;
};
//加载新闻数据
function NewsData() {
    var ulNews = $("#ulNews");
    ulNews.empty();
    var li, publishTime;
    $.ajax({
        type: "get",  //提交方式  
        url: portal.api_url + "/portal/MhNews/getNewsList",//路径  
        dataType: "json",
        //数据,这里使用的是Json格式进行传输  
        success: function (result) {//返回数据根据结果进行相应的处理         
            if (result.code == 200 && result.data != null) {
                if (result.data[0].preViewUrl) {
                    $('.news-img img').attr('src', result.data[0].preViewUrl);
                } else {
                    $('.news-img img').attr('src', './staticImages/news_demo.jpg');
                }
                for (var i = 0; i < result.data.length; i++) {
                    if (i == 6) return;
                    publishTime = CutDate(result.data[i].publishTime);
                    if (result.data[i].preViewUrl) {
                        li = '<li url="' + result.data[i].preViewUrl + '"><a href="./staticViews/news/' + result.data[i].id + '.html" title="' + result.data[i].headline + '" target="_blank">';
                    } else {
                        li = '<li><a href="./staticViews/news/' + result.data[i].id + '.html" title="' + result.data[i].headline + '" target="_blank">';
                    }
                    li += '<div class="news_item">';
                    if (recentlyDay.indexOf(publishTime) > -1) {
                        li += '<svg class="icon svg-icon" aria-hidden="true"> <use xlink:href="#iconzuixin"></use> </svg>';
                    }
                    li += result.data[i].headline + '</div>';
                    li += '<span>' + publishTime + '</span></a></li>';
                    ulNews.append(li);
                }
            }
        }
    });

    if ($('#ulNews li').eq(0).attr('url')) {
        $('.news-img img').attr('src', $('#ulNews li').eq(0).attr('url'));
    } else {
        $('.news-img img').attr('src', './staticImages/news_demo.jpg');
    }
    $(document).on("mouseover", "#ulNews li", function (e) {
        if ($(this).attr('url')) {
            $('.news-img img').attr('src', $(this).attr('url'));
        } else {
            $('.news-img img').attr('src', './staticImages/news_demo.jpg');
        }
    })
}

//加载公告数据
function NoticeData() {
    var ulTag = $("#ulNotice");
    ulTag.empty();
    var li;
    $.ajax({
        type: "get",  //提交方式  
        url: portal.api_url + "/portal/MhNotice/getMHNotice",//路径  
        dataType: "json",
        //数据,这里使用的是Json格式进行传输  
        success: function (result) {//返回数据根据结果进行相应的处理           
            if (result.code == 200 && result.data != null) {
                for (var i = 0; i < result.data.length; i++) {
                    if (i == 6) return;
                    li = '<li class="item_notice"><a href="./staticViews/notice/' + result.data[i].id + '.html" title="' + result.data[i].title + '" target="_blank">';
                    li += '<p>' + result.data[i].title + '</P>';
                    if (result.data[i].noticeType == 1) {
                        li += '<svg class="icon svg-icon" aria-hidden="true"> <use xlink:href="#icongongshi"></use> </svg>';
                    }
                    else {
                        li += '<svg class="icon svg-icon" aria-hidden="true"> <use xlink:href="#icongonggao1"></use> </svg>';
                    }
                    li += '</a></li>';
                    ulTag.append(li);
                }
            }
        }
    });
}


//加载法律法规数据
function LawsData() {
    var ulTag = $("#ulLaws");
    ulTag.empty();
    var li;
    $.ajax({
        type: "get",  //提交方式  
        url: portal.api_url + "/portal/MhLaws/getLawsList",//路径  
        dataType: "json",
        //数据,这里使用的是Json格式进行传输  
        success: function (result) {//返回数据根据结果进行相应的处理           
            if (result.code == 200 && result.data != null) {
                for (var i = 0; i < result.data.length; i++) {
                    if (i == 6) return;
                    li = '<li><a href="./staticViews/law/' + result.data[i].id + '.html" title="' + result.data[i].headline + '" target="_blank">' + result.data[i].headline;
                    li += '</a></li>';
                    ulTag.append(li);
                }
            }
        }
    });
}
//加载相应政策数据
function policiesData() {
    var policies = $("#policies");
    policies.empty();
    var li;
    $.ajax({
        type: "get",  //提交方式  
        url: portal.api_url + "/portal/MhPolicy/getPolicyList",//路径  
        dataType: "json",
        //数据,这里使用的是Json格式进行传输  
        success: function (result) {//返回数据根据结果进行相应的处理           
            if (result.code == 200 && result.data != null) {
                for (var i = 0; i < result.data.length; i++) {
                    if (i == 6) return;
                    li = '<li><a href="./staticViews/policy/' + result.data[i].id + '.html" title="' + result.data[i].headline + '" target="_blank">' + result.data[i].headline;
                    li += '</a></li>';
                    policies.append(li);
                }
            }
        }
    });
}