github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/blogweb_gin/static/js/blog.js (about)

     1  $(document).ready(function () {
     2      //注册
     3      $("#register-form").validate({
     4          rules: {
     5              username: {
     6                  required: true,
     7                  rangelength: [5, 10]
     8              },
     9              password: {
    10                  required: true,
    11                  rangelength: [5, 10]
    12              },
    13              repassword: {
    14                  required: true,
    15                  rangelength: [5, 10],
    16                  equalTo: "#register-password"
    17              }
    18          },
    19          messages: {
    20              username: {
    21                  required: "请输入用户名",
    22                  rangelength: "用户名必须是5-10位"
    23              },
    24              password: {
    25                  required: "请输入密码",
    26                  rangelength: "密码必须是5-10位"
    27              },
    28              repassword: {
    29                  required: "请确认密码",
    30                  rangelength: "密码必须是5-10位",
    31                  equalTo: "两次输入的密码必须相等"
    32              }
    33          },
    34          submitHandler: function (form) {
    35              var urlStr = "/register";
    36              // alert("urlStr:"+urlStr)
    37              console.info("urlStr:" + urlStr)
    38              $(form).ajaxSubmit({
    39                  url: urlStr,
    40                  type: "post",
    41                  dataType: "json",
    42                  success: function (data, status) {
    43                      // alert("data:" + data.message)
    44                      if (data.code == 1) {
    45                          setTimeout(function () {
    46                              window.location.href = "/login"
    47                          }, 1000)
    48                      }
    49                  },
    50                  error: function (data, status) {
    51                      alert("err:" + data.message + ":" + status)
    52                  }
    53              });
    54          }
    55      });
    56  
    57  
    58      //登录
    59      $("#login-form").validate({
    60          rules: {
    61              username: {
    62                  required: true,
    63                  rangelength: [5, 10]
    64              },
    65              password: {
    66                  required: true,
    67                  rangelength: [5, 10]
    68              }
    69          },
    70          messages: {
    71              username: {
    72                  required: "请输入用户名",
    73                  rangelength: "用户名必须是5-10位"
    74              },
    75              password: {
    76                  required: "请输入密码",
    77                  rangelength: "密码必须是5-10位"
    78              }
    79          },
    80          submitHandler: function (form) {
    81              var urlStr = "/login"
    82              // alert("urlStr:" + urlStr)
    83              console.info("urlStr:" + urlStr)
    84              $(form).ajaxSubmit({
    85                  url: urlStr,
    86                  type: "post",
    87                  dataType: "json",
    88                  success: function (data, status) {
    89                      alert("data:" + data.message + ":" + status)
    90                      if (data.code == 1) {
    91                          setTimeout(function () {
    92                              window.location.href = "/"
    93                          }, 1000)
    94                      }
    95                  },
    96                  error: function (data, status) {
    97                      alert("err:" + data.message + ":" + status)
    98  
    99                  }
   100              });
   101          }
   102      });
   103  
   104      //修改和添加文章的表单
   105      $("#write-art-form").validate({
   106          rules: {
   107              title: "required",
   108              tags: "required",
   109              short: {
   110                  required: true,
   111                  minlength: 2
   112              },
   113              content: {
   114                  required: true,
   115                  minlength: 2
   116              }
   117          },
   118          messages: {
   119              title: "请输入标题",
   120              tags: "请输入标签",
   121              short: {
   122                  required: "请输入简介",
   123                  minlength: "简介内容最少两个字符"
   124              },
   125              content: {
   126                  required: "请输入文章内容",
   127                  minlength: "文章内容最少两个字符"
   128              }
   129          },
   130          submitHandler: function (form) {
   131              var urlStr = "/article/add";
   132              //判断文章id确定提交的表单的服务器地址
   133              //若id大于零,说明是修改文章
   134              var artId = $("#write-article-id").val();
   135              console.log("artId:" + artId)
   136              if (artId > 0) {
   137                  urlStr = "/article/update"
   138              }
   139              console.log("urlStr:" + urlStr);
   140              $(form).ajaxSubmit({
   141                  url: urlStr,
   142                  type: "post",
   143                  dataType: "json",
   144                  success: function (data, status) {
   145                      console.log(":data:" + data.message);
   146                      setTimeout(function () {
   147                          window.location.href = "/"
   148                      }, 1000)
   149                  },
   150                  error: function (data, status) {
   151                      alert("err:" + data.message + ":" + status)
   152                  }
   153              });
   154          }
   155      });
   156  
   157  
   158      //文件
   159      $("#album-upload-button").click(function () {
   160          var filedata = $("#album-upload-file").val();
   161          if (filedata.length <= 0) {
   162              alert("请选择文件!");
   163              return
   164          }
   165          //文件上传通过Formdata去储存文件的数据
   166          var data = new FormData()
   167          data.append("upload", $("#album-upload-file")[0].files[0]);
   168          alert(data)
   169          var urlStr = "/upload"
   170          $.ajax({
   171              url: urlStr,
   172              type: "post",
   173              dataType: "json",
   174              contentType: false,
   175              data: data,
   176              processData: false,
   177              success: function (data, status) {
   178                  alert(":data:" + data.message);
   179                  if (data.code == 1) {
   180                      setTimeout(function () {
   181                          window.location.href = "/album"
   182                      }, 500)
   183                  }
   184              },
   185              error: function (data, status) {
   186                  alert("err:" + data.message + ":" + status)
   187              }
   188          })
   189      })
   190  
   191  });