login.js 2.02 KB
function login(){
  layui.use('layer', function () {
    layer.open({
      type: 1,
      title: '登录信息',
      shadeClose: false,
      area: ['500px', '320px'],
      scrollbar: false,
      skin: 'confimInfo',
      content: '<div class="loginINfo">'
        + '<ul>'
        + '<li><span><a class="require">*</a>用户名</span>:<input id="userName"></li>'
        + '<li><span><a class="require">*</a>密码</span>:<input id="IDcard"></li>'
        + '</ul>'
        + '<div class="btns">'
        +'<button type="button" id="loginButton">登录</button>'
        +'<button type="button" id="registerButton">注册</button></div>'
        + '</div>'
    });
  })
}
$(document).on('click', '.PersonLogin', function(){
  login()
})

// 点击登陆的时候
$(document).on('click', '#loginButton', function(){
  var userLoginRequest = {}
  var username =$('#userName').val()
  var password =$('#IDcard').val()
  userLoginRequest.username = username
  userLoginRequest.password = password
  clicklogin(userLoginRequest)
  layer.closeAll()
});
$(function(){
  $('.PersonLogin').html(getCookie().username)
  if(getCookie().username) {
    $('.personalCenter').addClass('show')
  }
})
// 点击注册的时候
$(document).on('click', '#registerButton', function(){
  // layer.closeAll()
  window.open(`${localhostPaht}/staticViews/register.html`,'_self')
});
function clicklogin(data){
  $.ajax({
    type: "post",  //提交方式  
    url: portal.api_url + "/protal/users/login",//路径  
    headers:{//***关键******
      'Content-Type': 'application/json;charset=UTF-8',
    },
    dataType: "json",
    data:JSON.stringify(data),//***关键******
    success: function (result) {//返回数据根据结果进行相应的处理
       if(result.code ==200 && result.data!==null){
        setCookie(result.data);
        $('.personalCenter').addClass('show')
        $('.PersonLogin').html(getCookie().username);
       }else if(result.code ==500){
        window.open(`${localhostPaht}/staticViews/register.html`,'_self')
       }
    }
  });
}