public.js 9.2 KB
$(function() {
    var host = 'http://' + window.location.host;
    // 收藏
    $('.collections').click(function() {
        if ($(this).children('img').attr('src') == host + '/themes/simpleboot3/public/assets/starImg/bicon_02.png') {
            var login = operation('/portal/star/collection');
            if (login == 0) {
                return false;
            }
            $(this).children('img').attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_15.png');
        } else {
            var login = operation('/portal/star/cancelCollection');
            if (login == 0) {
                return false;
            }
            $(this).children('img').attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_02.png');
        }
    });
    //点赞
    $('.likes').click(function() {
        if ($(this).children('img').attr('src') == host + '/themes/simpleboot3/public/assets/starImg/bicon_03.png') {
            var login = operation('/portal/star/like');
            if (login == 0) {
                return false;
            }
            $(this).children('img').attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_16.png');
        } else {
            var login = operation('/portal/star/cancelLike');
            if (login == 0) {
                return false;
            }
            $(this).children('img').attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_03.png');
        }
    });

    // 收藏更多视频
    $('#collections').click(function() {
        if ($(this).attr('src') == host + '/themes/simpleboot3/public/assets/starImg/bicon_02.png') {
            var login = operation('/portal/star/collection', 1);
            if (login == 0) {
                return false;
            }
            $(this).attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_15.png');
        } else {
            var login = operation('/portal/star/cancelCollection', 1);
            if (login == 0) {
                return false;
            }
            $(this).attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_02.png');
        }
    });

    //点赞
    $('#likes').click(function() {
        if ($(this).attr('src') == host + '/themes/simpleboot3/public/assets/starImg/bicon_031.png') {
            var login = operation('/portal/star/like', 1);
            if (login == 0) {
                return false;
            }
            $(this).attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_161.png');
        } else {
            var login = operation('/portal/star/cancelLike', 1);
            if (login == 0) {
                return false;
            }
            $(this).attr('src', host + '/themes/simpleboot3/public/assets/starImg/bicon_031.png');
        }
    });


    // 评论判断长度显示与隐藏
    if ($('.show_2_pinL').length < 3) {
        $('.show_main_more').hide();
        $('.show_main2_show ul').removeClass('show_main2_show_H');
    } else {
        $('.show_main_more').show();
    }

    // 点击查看更多,与提示
    $('.show_main_more').click(function() {
        if ($(this).text() == "查看更多评论") {
            $('.show_main2_show ul').removeClass('show_main2_show_H');
            $(this).text('收起');
        } else {
            $('.show_main2_show ul').addClass('show_main2_show_H');
            $(this).text('查看更多评论');
        }
    });

    //头部导航栏切换颜色
    var str_url = window.location.pathname;
    $('.home_nav_act,.slide_vo').children('a').removeClass('home_nav_act');
    var star = 'star'; //星球奇境
    var region = 'region'; //星域秀场
    var enjoy = 'enjoy'; //星享体验
    var scout = 'scout'; //星探推荐
    var active = 'active'; //星际活动
    if (str_url.indexOf(star) != -1) {
        $('.slide_vo').eq(0).children('a').addClass('home_nav_act');
    } else if (str_url.indexOf(region) != -1) {
        $('.slide_vo').eq(1).children('a').addClass('home_nav_act');
    } else if (str_url.indexOf(enjoy) != -1) {
        $('.slide_vo').eq(2).children('a').addClass('home_nav_act');
    } else if (str_url.indexOf(scout) != -1) {
        $('.slide_vo').eq(3).children('a').addClass('home_nav_act');
    } else if (str_url.indexOf(active) != -1) {
        $('.slide_vo').eq(4).children('a').addClass('home_nav_act');
    } else {
        $('.home_nav_act').children('a').addClass('home_nav_act');
    }

    var maxwidth = 4;//设置最多显示的字数
    var user_nickname = $('#user_nickname').text();
    if(user_nickname.length > maxwidth){
        var user = user_nickname.substring(0,maxwidth);
        $('#user_nickname').text(user+'...');
    }
    //改变分页图标
    $('.page-item').eq(0).children().text('<');
    $('.page-item:last').children().text('>');

    //注册页面
    $('#register').click(function() {
        window.location.href = '/portal/login/register';
    });

    //登录页面
    $('#login').click(function() {
        window.location.href = '/portal/login/login';
    });

    //个人中心
    $('.login_person').click(function() {
        window.location.href = '/portal/login/info';
    });

    //修改资料页面
    $('#editInfo').click(function() {
        window.location.href = '/portal/login/editInfo';
    });

    //我的收藏页面
    $('#myCollection').click(function() {
        window.location.href = '/portal/login/myCollection';
    });

    //评论
    $('#comment').click(function() {
        var content = $('#comment_content').val();
        var article_id = getUrlParam('id');
        var url = window.location.pathname + window.location.search;
        $.ajax({
            type: 'POST',
            url: "/portal/star/comment",
            data: {
                'content': content,
                'article_id': article_id,
                'url': url

            },
            dataType: 'json',
            success: function(data) {
                console.log(data);
                if (data.code == 1) {
                    mask(data.msg);
                    $('#comment_content').val('');
                } else {
                    mask(data.msg);
                }
            }
        });
    });
    //关于我们
    $('#about_us,#about_us1').click(function() {
        window.location.href = '/portal/index/aboutUs';
    });
    //搜索
    $("#keywords").keypress(function(e) {
        if (e.which == 13) {
            $("#searchForm").submit(); //处理事件
        }
    });

    var share_url = encodeURIComponent(location.href);
    var share_title = encodeURIComponent(document.title);
    //分享到微博弃用
    $('.weibo').click(function() {
        var param = {
            url: share_url,
            title: share_title,
            rnd: new Date().valueOf()
        };
        var temp = [];
        for (var p in param) {
            temp.push(p + '=' + encodeURIComponent(param[p] || ''))
        }
        window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
    });

    //微博分享
    $('.wb_share').click(function() {
        thirdShare('/portal/login/wb_share');
    });

    //微博登录
    $('#wb_login').click(function(){
        $('#wb_form').submit();
    });
});

//第三方分享
function thirdShare(url){
    var title = $(document).attr("title");
    var share_url = window.location.href;
    var article_id = getUrlParam('id');
    $.ajax({
        type: 'POST',
        url: url,
        data: {
            'id': article_id,
            'title': title,
            'share_url': share_url
        },
        dataType: 'json',
        async: false,
        success: function(data) {
            if (data.code == 1) {
                mask(data.msg);
            } else {
                mask(data.msg);
            }
        }
    });
}

//点赞收藏
function operation(url, id) {
    if (id == '' || id == undefined) {
        var article_id = getUrlParam('id');
    } else {
        var article_id = $('#li_id').val();
    }
    var is_login = 1;
    var post_url = window.location.pathname + window.location.search;
    $.ajax({
        type: 'POST',
        url: url,
        data: {
            'article_id': article_id,
            'post_url': post_url
        },
        dataType: 'json',
        async: false,
        success: function(data) {
            if (data.code == 1) {
                mask(data.msg);
            } else {
                mask(data.msg);
                is_login = data.code;
            }
        }
    });
    return is_login;
}
//弹框
function mask(msg) {
    $('.mask').text(msg);
    $('.mask').show();
    setTimeout(function() {
        $('.mask').hide();
        $('.mask').text('');
    }, 4000);
}

//弹框
function video_mask(msg) {
    $('.mask').text(msg);
    $('.mask').show();
    setTimeout(function() {
        $('.mask').hide();
        $('.mask').text('');
    }, 6000);
}

//获取url的参数
function getUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    } else {
        return "";
    }
}