login.js 3.44 KB
var localhostPaht = curWwwPath.substring(0, pos);
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="phone"></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();
  window.onbeforeunload = function () {
    var n = window.event.screenX - window.screenLeft;
    var b = n > document.documentElement.scrollWidth - 20;
    if (b && window.event.clientY < 0 || window.event.altKey) {
      delAllCookie();
    }
  }
})

// 点击登陆的时候
$(document).on('click', '#loginButton', function () {
  var userLoginRequest = {}
  var username = $('#phone').val()
  var password = $('#IDcard').val()
  userLoginRequest.phone = username
  userLoginRequest.password = password
  clicklogin(userLoginRequest)
});

// cancellation
$(document).on('click', '#cancellation', function () {
  layui.use('layer', function () {
    layer.confirm('此操作将进行注销', {
      btn: ['是','否'] //按钮
    }, function(){
      delCookie();
      window.open('' + localhostPaht + '/index.html', '_self')
    }, function(){
      layer.msg('已取消');
    });
  })
});
function delCookie () {
  var keys = document.cookie.match(/[^ =;]+(?==)/g)
  if (keys) {
    for (var i = keys.length; i--;) {
      document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如 .m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString() // 清除一级域名下的或指定的,例如 .ratingdog.cn
    }
  }
}


$(function () {
  $('.PersonLogin').html(getCookie('myCookie').username)
  if (getCookie('myCookie').username) {
    $('.personalCenter').addClass('show')
  }
})
// 点击注册的时候
$(document).on('click', '#registerButton', function () {
  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('myCookie',result.data,60);
        $('.personalCenter').addClass('show');
        $('.PersonLogin').html(getCookie('myCookie').username);
        layer.closeAll();
      } else if (result.code == 210) {
        layer.msg(result.message);
      }
    }
  });
}

// 点击判断是否登录
function isLogin() {
  let login1;
  if (getCookie('myCookie').username) {
    login1 = true;
  } else {
    login();
    login1 = false;
  }
  return login1;
}