login.js
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var localhostPaht = curWwwPath.substring(0, pos);
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', '.PersonLogin', function () {
login();
})
// 点击登陆的时候
$(document).on('click', '#loginButton', function () {
var userLoginRequest = {};
var username = $('#phone').val();
var password = $('#IDcard').val();
userLoginRequest.phone = username;
userLoginRequest.password = password;
clicklogin(userLoginRequest);
});
function fetchDataReview() {
// cancellation
$(document).on('click', '#cancellation', function () {
layui.use('layer', function () {
layer.confirm('此操作将进行注销', {
btn: ['是', '否'] //按钮
}, function () {
$('.loginButton').addClass('PersonLogin');
window.open('' + localhostPaht + '/index.html', '_self');
$('.loginAfter').hide();
$('.isLogin').show();
sessionStorage.clear()
}, function () {
layer.msg('已取消');
});
})
});
}
$(function () {
fetchDataReview();
if (getItem('myCookie') && getItem('myCookie').username) {
$('.loginButton').html(getItem('myCookie').username);
$('.loginButton').removeClass('PersonLogin')
$('.personalCenter').addClass('show');
$('.loginAfter').show();
$('.isLogin').hide();
} else {
$('.loginAfter').hide();
$('.isLogin').show();
}
})
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 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) {
$('.loginAfter').show();
$('.isLogin').hide();
setItem('myCookie', result.data)
$('.loginButton').removeClass('PersonLogin')
$('.personalCenter').addClass('show');
$('.loginButton').html(result.data.username);
layer.closeAll();
} else if (result.code == 210) {
layer.msg(result.message);
}
}
});
}
// 点击注册的时候
$(document).on('click', '#registerButton', function () {
window.open('' + localhostPaht + '/staticViews/register.html', '_self')
});
// 点击判断是否登录
function isLogin() {
let login1;
if (getItem('myCookie') && getItem('myCookie').username) {
login1 = true;
} else {
login();
login1 = false;
}
return login1;
}