login.js 5.28 KB
function login() {
  layui.use('layer', function () {
    layer.open({
      type: 1,
      title: '登录信息',
      shadeClose: false,
      area: ['500px', '280px'],
      scrollbar: false,
      skin: 'confimInfo',
      content: '<div class="loginINfo">'
        + '<ul>'
        + '<li><span><a class="require">*</a>手机号</span>:<input class="loginInput" id="phone"></li>'
        + '<li><span><a class="require">*</a>密码</span>:<input class="loginInput" type="password" id="IDcard"></li>'
        + '</ul>'
        + '<div class="btns">'
        + '<button type="button" class="layui-btn layui-btn-normal" id="loginButton">登录</button>'
        + '<button type="button" class="layui-btn layui-btn-primary" id="registerButton">注册</button></div>'
        + '</div>'
    });
  })
}
$(document).on('click', '#login', function () {
  login();
})
// 点击登陆的时候
$(document).on('click', '#loginButton', function () {
  var userLoginRequest = {};
  var username = $('#phone').val();
  var password = $('#IDcard').val();
  userLoginRequest.phone = username;
  userLoginRequest.password = password;
  handleLogin(userLoginRequest);
});
function fetchDataReview() {
  $(document).on('click', '#cancellation', function () {
    layui.use('layer', function () {
      layer.confirm('此操作将进行注销', {
        btn: ['是', '否'] //按钮
      }, function () {
        $('.loginButton').addClass('PersonLogin');
        window.open('/index.html', '_self');
        $('.loginAfter').hide();
        $('.isLogin').show();
        sessionStorage.clear()
      }, function () {
        layer.msg('已取消');
      });
    })
  });
}


$(function () {
  fetchDataReview();
  if (getItem('myCookie') && getItem('myCookie').username) {
    appendData();
    $('.loginInfo').html(getItem('myCookie').username);
  } else {
    beforeLogin();
  }
})
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 handleLogin(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) {
        appendData();
        setItem('myCookie', result.data)
        $('.loginInfo').html(result.data.username);
        layer.closeAll();
      } else if (result.code == 210) {
        layer.msg(result.message);
      }
    }
  });
}
// 登录成功之前数据操作
function beforeLogin() {
  $('.login-con').empty();
  $('.login-con').append('<span class="loginButton" id="login">登录</span>' +
    '<p class="loginButton register" id="register"><a href="/staticViews/register.html">注册</a></p>')
}
// 登录成功之后数据操作
function appendData() {
  $('.login-con').empty();
  $('.login-con').append(
    '<li class="layui-nav-item sysNews">' +
    '<p class="sysNewsItem">' +
    '<img class="userImage" src="/staticImages/xiaoxi.png">' +
    '<span class="sysNewsNum">12</span>' +
    '</p>' +
    '<dl class="layui-nav-child sysNewsList">' +
    '<div class="sysNewsTitle">最新消息</div>' +
    '<div class="sysNewsOperation">' +
    '<span>全部标记为已阅</span><a href="/staticViews/sysnews-center.html">查看全部</a>' +
    '</div>' +
    '<dd>' +
    '<a>' +
    '<p class="newsDetail ellipsis">您申请办理的XXX业务已成功提交您申请办理的XXX业务已成功提交</p>' +
    '<div class="newsDetail-time"><span>2020-12-32</span> <span>已读</span></div>' +
    '</a>' +
    '</dd>' +
    '<dd>' +
    '<a>' +
    '<p class="newsDetail ellipsis">您申请办理的XXX业务已成功提交您申请办理的XXX业务已成功提交</p>' +
    '<div class="newsDetail-time"><span>2020-12-32</span> <span>已读</span></div>' +
    '</a>' +
    '</dd>' +
    '</dl>' +
    '</li>' +
    '<img class="userImage" src="/staticImages/user.png">' +
    '<p class="personalCenter"><a href="/staticViews/grzx.html">个人中心</a></p>' +
    '<li class="layui-nav-item loginSelect">' +
    '<p class="loginInfo">登录</p>' +
    '<dl class="layui-nav-child userInfo">' +
    '<dd><a>个人信息</a></dd>' +
    '<dd><a id="cancellation"><img src="/staticImages/zhuxiao.png">退出</a>' +
    '</dd>' +
    '</dl>' +
    '</li>'
  )
  layui.use(['element'], function () {
    layui.element.init();//手动调用初始化方法
  })
}

// 点击注册的时候
$(document).on('click', '#registerButton', function () {
  window.open('/staticViews/register.html', '_self')
});

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