分页

/**
 * Created by zy on 2017/8/11.
 */
require.config(
    {
        paths: {
            'jquery': '../lib/jquery-3.2.1.min',
            "clientServer":"clientServer",
            "pagination":"../lib/pagination"
        }
    }
);
require(['jquery', 'header', 'footer', 'common',"alert","clientServer",'pagination'], function ($) {

    var pagination = null;
    //切换-在线问答与常见问题
    var change = function() {
        var $btn1 = $('#btn1');
        var $btn2 = $('#btn2');
        var $Jqar = $('#J-qar');
        var $Jqar2 = $('#J-qar2');
        var $Jwrap = $('#j-wrap');
        $btn1.click(function () {
            $btn1.addClass('selected');
            $btn2.removeClass('selected');
            $Jqar.css('display', 'block');
            $Jqar2.css('display', 'none');
        });
        $btn2.click(function () {
            $btn2.addClass('selected');
            $btn1.removeClass('selected');
            $Jqar.css('display', 'none');
            $Jqar2.css('display', 'block');
            $Jwrap.css('margin-left', '46px');
        })
    }

    //获取老师
    var objTeacher={};//存id用于tId和name
    function getTeacher() {
        $.ajax({
            type: "get",
            cache:false,
            url: baseURL + "/teacher/get.do",
            dataType: "json",
            contentType: "applcation/x-www-form-urlencoded; charset=utf-8",
            success: function (result) {
                if (result.success == true) {
                    var $teacher = $('#teacher');
                    var $Jteacher = $('#J-teacher');
                    var str = '<option value="">请选择老师</option>';
                    var str1='<li>全部回答</li>';
                    for (var i = 0; i < result.data.length; i++) {
                        var key=result.data[i].id;
                        var value=result.data[i].name;
                        objTeacher[key]=value;//存tId和name对象
                        str += '<option value="' + result.data[i].id + '">' + result.data[i].name + '</option>\
                    ';
                        str1+='<li class="ho">' + result.data[i].name + '</li>';
                    }
                    $teacher.html(str);
                    $Jteacher.html(str1);
                }
                change();
                list();
            },
            error: function () {
                alert("错误");
            }
        });
    }

    //在线问答-发送数据
    function ask() {
        var $ask = $('#ask');
        var $submit = $('#submit');
        $submit.click(function () {
            $.ajax({
                url: baseURL + "/interlocution/add.do?ti="+(new Date()).getTime(),
                data:{"tId":$('#teacher option:selected').val(),
                        "question":$ask.val()
                },
                type: 'post',
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                success: function (result) {
                    var json = JSON.parse(result);
                    if (json[0].success) {
                        list();
                    }else{
                        $.MsgBox.Alert("消息",json[0].message);
                    };
                },
                error: function (xhr) {
                    tool.error(xhr.responseText);
                }
            })
        });
    }

    //回复提问
    var list = function() {
        $.ajax({
            // 'http://192.168.2.223:8080'
            type: "get",
            async: true,
            cache:false,
            url: baseURL + "/interlocution/list.do?index=1&size=5&type=1",
            dataType: "json",
            contentType: "applcation/x-www-form-urlencoded; charset=utf-8",
            success: function (result) {
                var $Janswer=$('#J-answer');
                var str = '';
                if (result.success && result.data) {
                    for (var i = 0; i < result.data.length; i++) {
                        str += '<div class="comment cf">\
                        <div class="f1">\
                            <img src="../images/qa-little.png" alt="">\
                            <span class="tel">'+result.data[i].userPhone+'</span>\
                            <span>'+getLocalTime(result.data[i].createTime)+'</span>\
                        </div>\
                        <div class="f2 cf">\
                            <div class="f2-ask">'+result.data[i].question+'</div>\
                            <div class="f2-ans cf">\
                             <div class="pho"><img src="../images/qa-pho.png" alt=""></div>\
                                <div class="descr">\
                                    <div class="head">\
                                        <span class="name">'+objTeacher[result.data[i].tId]+'</span>\
                                        <span class="time">'+getLocalTime(result.data[i].answerTime)+'</span>\
                                    </div>\
                                    <div class="teach-ask">\
                                        '+result.data[i].answer+'\
                                    </div>\
                                </div>\
                            </div>\
                        </div>\
                    </div>\
                        ';
                        //$Janswer.html(str+'<div class="page">上一页 | 下一页</div>');
                        $Janswer.html(str);
                    }
                }
            },
            error: function () {
                alert("错误");
            }
        });
    }

    //获取常见问题数据
    function comment(index) {
        var pageSize = 2;//手动修改每页展示多少条数据
        if(!index) index = 1;
        $.ajax({
            type: "get",
            async: true,
            url: baseURL + "/interlocution/list.do?index="+index+"&size="+pageSize+"&type=2",
            dataType: "json",
            contentType: "applcation/x-www-form-urlencoded; charset=utf-8",
            success: function (result) {
                console.log(result);
                var Count=result.totalCount;//记录条数
                var pageCount=Math.ceil(Count/pageSize);//计算总页数
                var $Jwrap = $('#j-wrap');
                var str = '';

                if (result.success == true) {
                    for (var i = 0; i < result.data.length; i++) {
                        str += '<div class="comment cf">\
                    <div class="f1">\
                        <img src="../images/qa-little.png" alt="">\
                        <span class="tel">'+result.data[i].userPhone+'</span>\
                        <span>'+getLocalTime(result.data[i].createTime)+'</span>\
                    </div>\
                    <div class="f2 cf">\
                        <div class="f2-ask">\
                            <p>'+result.data[i].question+'</p>\
                        </div>\
                        <div class="f2-ans cf">\
                            <div class="pho">\
                                <img src="../images/qa-pho.png" alt="">\
                            </div>\
                            <div class="descr">\
                                <div class="head">\
                                    <span class="name">'+objTeacher[result.data[i].tId]+'</span>\
                                    <span></span>\
                                </div>\
                                <div class="teach-ask">\
                                    <p>'+result.data[i].answer+'</p>\
                                </div>\
                            </div>\
                        </div>\
                    </div>\
                </div>\
                    ';
                    }
                    $Jwrap.html(str);

                    addPagination("#J-page",pageCount,comment);
                }
            },
            error: function () {
                alert("错误");
            }
        });
    }

    function addPagination(id,total,successfn) {
        if(pagination) return;
        pagination = $(id).pagination({
            coping:true,
            homePage:'首页',
            endPage:'末页',
            current:1,
            prevContent:'上页',
            nextContent:'下页',
            pageCount:total,
            callback:function(api){
                console.log("current page:"+api.getCurrent());
                successfn(api.getCurrent());
            }
        });
    }
    //时间戳转时间
    function getLocalTime(nS) {
        return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
    }

    /**
     * 判断用户是否登陆
     */
    function isLogin(){
        CCTVJY.clientServer.alaxPhoneDES(function(result){
            console.log(result);
            if(!result.success){
                $("#ask").val("请先登陆");
            }
        },null,null);
    }

    $(document).ready(function () {
        addHover();
        isLogin();
        change();
        getTeacher();
        ask();
        comment();
    });
});

  

posted @ 2017-08-28 17:15  透明的月儿  阅读(233)  评论(0编辑  收藏  举报