Source: web.js

/**
  * some js function
*/
/**
  * @see collect(thesis_id='')
  * @param thesis_id thesis's identification
  * @brief add thesis to my_collect
*/
function collect(thesis_id='')
{
  console.log(thesis_id);
  $.ajax({
    type:"GET",
    data:{
      thesis_id:thesis_id,
    },
    url:"?m=User&c=Query&a=collect_thesis",
    success:function(data){
      console.log(data);
      switch(data)
      {
        case 1:
          alert('已收藏');
          break;
        case 2:
          alert('收藏成功');
          break;
      }
    },
    error:function(request){
      alert("Connection error");
      console.log(request);
    },
  });
}
    /**
      * @see sign_up()
      * @brief check if user_name or password is legal
      */
    function sign_up() {
      //var a=JSON.parse('('+params+')');
      var user_name = $('#user_name').val();
      if (user_name.length < 6) alert("昵称少于6个字符");
      else if (user_name.length > 16) alert("昵称多于16个字符")
      else {
        if ($.trim($('#password').val()) == '') alert("密码不得为空");
        else if ($.trim($('#repassword').val()) == '') alert("未确认密码");
        else
          $.ajax({
            type: "post",
            data: {
              user_name: $.trim($('#user_name').val()),
              password: $.trim($('#password').val()),
              repassword: $.trim($('#repassword').val()),
            },
            url: "?m=user&c=register&a=add",
            success: function(data) {
              alert(data);
              if (data == '注册成功') {
                window.location = "?m=user&c=login&a=login";
              }
            },
            error: function(request) {
              alert("注册失败");
              console.log(request);
            }
          });
      }
    }


    /**
      * @see collect(thesis_id = '')
      * @param thesis_id thesis's id
      * @brief add thesis to my_collect
      */
    function collect(thesis_id = '') 
    {
      console.log(thesis_id);
      $.ajax({
        type: "GET",
        data: {
          thesis_id: thesis_id,
        },
        url: "?m=User&c=Query&a=collect_thesis",
        success: function(data) {
          console.log(data);
          switch (data) {
            case 1:
              alert('已收藏');
              break;
            case 2:
              alert('收藏成功');
              break;
          }
        },
        error: function(request) {
          alert("Connection error");
          console.log(request);
        },

      });
    }
/**
  * @see thesis_download()
  * @brief download thesis (ajax)
*/
    function thesis_download() {
      var params = $("#download_thesis").serialize();
      console.log(params + 'pp');
      $.ajax({
        type: "POST",
        data: params,
        token: 'hhh',
        url: "?m=user&c=download&a=split",

        error: function(request) {
          console.log("Connection error");
          console.log(request);
        },
        success: function(data) {
          console.log("pp");
          console.log(data);
        },
      });
    }
/**
  * @see Out()
  * @brief log out
  * @brief get user_id by cookie
*/
    function Out() {
      setCookie('uid', '', 0);
      $.ajax({
        type: "POST",
        url: "?m=user&c=login&a=logout",

        success: function(data) {
          console.log(data);
          alert('注销成功');
          location.reload();
          clearCookie('uid');
        },
        error: function(request) {
          alert("Connection error");
        },
      });

    }

/**
  * @see add_collect()
  * @brief log out
  * @brief get user_id by cookie
*/
    function add_collect() {
      var params = $("#add_thesis").serialize();
      console.log(params);
      var bool = true;
      for (var key in params) {
        if (key == '')
          bool = false;

      }
      if (!bool) {
        alert('未填完整');
      } else {
        $.ajax({
          type: "POST",
          data: params,
          token: 'hhh',
          //  url:"?m=user&c=query&a=add_thesis",

          error: function(request) {
            console.log("Connection error");
            console.log(request);
          },
          success: function(data) {
            console.log("pp");
            console.log(data);
          },
        });
      }
    }
/**
  * @see add_my_thesis()
  * @brief add thesis to myy_thesis
  * brief save information 
*/
    function add_my_thesis() 
    {
      var params = new FormData($('#add_thesis')[0]);
      //   var params = $("#add_thesis").serialize();
      var params_test = $("#add_thesis").serializeArray();
      console.log(params_test);
      var flag = 1;
      for (var key in params_test) {
        if (params_test[key]['value'] == '') {
          flag = 0;

        }
        console.log(params_test[key]['value']);

      }
      if (!flag) {
        alert('未填完整');
      } else {
        $("#add_thesis").ajaxForm({
          type: "POST",
          data: params,
          token: 'hhh',
          url: "?m=user&c=Upload&a=upload_my_thesis",
          error: function(request) {
            console.log("Connection error");
            console.log(request);
          },
          success: function(data) {
            //   alert("添加成功");
            //         window.location.href = '?User/Index/index_center';
          },


        });
      }
    }
/**
  * @see get_collect()
  * @brief get thesis from my_collect 
*/
    function get_collect() 
    {
      $.ajax({
        type: 'GET',
        //  data: params,
        token: 'hhh',
        url: "?m=user&c=query&a=get_collect",

        error: function(request) {
          console.log("Connection error");
          console.log(request);
        },
        success: function(data) {
          data = JSON.parse(data);
          console.log('ppp' + data[0]);
          console.log(data[0]['user_id']);
          $('#my_collect').empty("");
          /**
            * @brief print table
          */
          $('#my_collect').append('<thead><th><td>名称</td><td>作者</td><td>简介</td><td>时间</td><td>类别</td><td>链接</td><td>添加时间</td></th></thead>');

          for (var i in data) {
            $('#my_collect').append('<tr><td>' + data[i]['collect_name'] + '</td><td>' + data[i]['collect_author'] + '</td><td>' + data[i]['collect_brief_intro'] + '</td><td>' + data[i]['collect_author_time'] + '</td><td>' + data[i]['collect_type'] + '</td><td><a href="' + data[i]['collect_url'] + '">' + data[i]['collect_url'] + '</a></td><td>' + data[i]['collect_time'] + '</td></tr>');

          }
          for (var i in data) {
            console.log(data[i]["user_id"]);
          }
        },
      });
    }
    /**
      * @see Trim(str)
      * @param str string
      * @brief get rid of blank space (front and back)
    */
    function Trim(str) {
      return str.replace(/(^\s*)|(\s*$)/g, "");
    }
    /**
      * @see thesis_table(sort = '3', order = '1') 
      * @param sort keyword of ordering
      * @param order in order or inverted order
      * @brief table control
    */
    function thesis_table(sort = '3', order = '1') {
      console.log(sort);
      console.log(order);
      var params = $("#search_contain").val();
      var is_blank = $("#search_contain").val();

      // //   params = JSON.parse(params);
      console.log(is_blank);
      is_blank = Trim(is_blank);
      console.log(is_blank);
      if (is_blank != '') {
        $.ajax({
          type: "POST",
          data: {
            contain: params,
            sort: sort,
            order: order,
          },

          token: 'hhh',
          url: "?m=user&c=query&a=showthesis",

          //         error: function(request) {
          //             console.log("Connection error");
          //             console.log(request);
          //         },
          success: function(result) {
            //console.log(result);
            result = JSON.parse(result);
            data = result['data'];
            console.log('yes');
            if (data == 0) {
              $('#pp').empty("");
              $('#pp').append('<h5>搜索不到</h5>');

            } else {

              console.log(result['order']);

              console.log(result);
              console.log(result['sort']);
              $('#head_intro').html("");
              var head_intro = ["name", "author", "brief_intro", "create_time", "type", "url"];
              var ch_head_intro = ["名称", "作者", "简介", "添加时间", "类型", "链接"];
              var head_intro_info = '<tr>';
              //  var order = 'desc';
              console.log("i: " + i);
              for (var i = 0; i < head_intro.length; i++) {
                console.log("i: " + i);
                if (i != result['sort']) {
                  head_intro_info += '<td data-field="' + head_intro[i] + '" ><a href="#" onclick="thesis_table(' + i + ',' + '1' + ')">' + ch_head_intro[i] + '</a></th>';
                } else {
                  if (result['order'] == '1') {
                    head_intro_info += '<td data-field="' + head_intro[i] + '" ><a href="#" onclick="thesis_table(' + i + ',' + '2' + ')">' + ch_head_intro[i] + '</a></th>';
                  } else {
                    head_intro_info += '<td data-field="' + head_intro[i] + '" ><a href="#" onclick="thesis_table(' + i + ',' + '1' + ')">' + ch_head_intro[i] + '</a></th>';

                  }
                }
              }
              console.log(data);
              head_intro_info += '<td data-field="download_file"><input type="submit" name="down" value="下载"/> </th><td data-field="see_thesis">查看</th><td data-field="collect_file">收藏 </th><td data-field="download_bib"><input id="down_bib" type="submit" name="bib" value="导出" onclick="down_bib()"/></th>';
              head_intro_info += '</tr>';
              $('#pp').empty("");
              $('#pp').append(head_intro_info);

              for (var i in data) {
                console.log(data[i]['thesis_id']);
                $('#pp').append('<tr><td>' + data[i]['name'] + '</td><td>' + data[i]['author'] + '</td><td>' + data[i]['brief_intro'] + '</td><td>' + data[i]['create_time'] + '</td><td>' + data[i]['type'] + '</td><td>' + data[i]['url'] + '</td><td><a href="index.php?m=User&c=upload&a=downloadfile&thesis_id=' + data[i]['thesis_id'] + '">下载</a> <input type="checkbox" name="thesis[]" value="' + data[i]['save_name'] + '"/>' + data[i]['download_times'] + '</td><td><a href="index.php?m=User&c=Query&a=see_thesis&thesis_id=' + data[i]['thesis_id'] + '">查看</a>' + data[i]['click_times'] + '</td><td><input value="收藏" type="button" onclick="collect(' + data[i]['thesis_id'] + ')">' + data[i]['collect_times'] + '</td><td>导出bib<input type="checkbox" name="thesis_id[]" value="' + data[i]['thesis_id'] + '"/> </td></tr>');
              }

            }
          },
        });
      }
    }
/**
  * @see log_in()
  * @brief log in control
*/
    function log_in() {
      console.log('p');
      var params = $('#loginbox').serialize();
      console.log(params);
      $.ajax({
        type: "POST",
        url: "?m=user&c=login&a=checkLogin",
        data: params,
        success: function(data) {
          console.log(data);
          if (data == 0) {
            alert('登录失败');
          } else {
            alert('登录成功');
            window.location = '?m=User&c=index&a=index';
            //   window.location='?m=User&c=index&index_center';
          }
          //   location.reload();
          // clearCookie('uid');
        },
        error: function(request) {
          alert("Connection error");
        },
      });

    }
/**
  * @brief start web work
*/
    $(document).ready(function() {
      $(".select_all").change(function() {
        var checked = $(".select_all").prop("checked");
        $(":checkbox").prop("checked", checked);
      });
      $(":checkbox").change(function() {});

    });