feat: 首页是否登录识别

This commit is contained in:
xiongxiaoyang 2023-05-13 13:11:10 +08:00
parent d9ad02e3f5
commit dce8a21c0d
3 changed files with 109 additions and 8 deletions

View File

@ -1,3 +1,48 @@
var needLoginPath = ['/user/favorites.html','/user/comment.html','/user/feedback.html',
'/user/feedback_list.html','/user/read_history.html','/user/set_name.html',
'/user/set_password.html','/user/set_sex.html','/user/setup.html','/user/userinfo.html',
"/pay/index.html," +
"/author/register.html","/author/index.html"];
var isLogin = false;
jQuery.cookie = function (name, value, options) {
if (typeof value != 'undefined') {
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
@ -12,15 +57,54 @@ Array.prototype.remove = function (val) {
}
};
var token = localStorage.getItem("token");
if (token) {
$.get("/user/isLogin", {"token": token}, function (data) {
if (data.code != 1) {//未登录
localStorage.removeItem("token");
var token = $.cookie('Authorization');
if(!token){
if(needLoginPath.indexOf(window.location.pathname) != -1){
location.href = '/user/login.html?originUrl='+decodeURIComponent(location.href);
}
$(".user_link").html("<a href=\"/user/login.html\">登录</a><a href=\"/user/register.html\">注册</a>");
}else{
$.ajax({
type: "POST",
url: "/user/refreshToken",
data: {},
dataType: "json",
success: function(data){
if(data.code == 200){
$(".user_link").html("<a href=\"/user/userinfo.html\">"+(data.data.nickName.substring(0,3))+"...</a>&nbsp;&nbsp;<a href=\"javascript:logout()\">退出</a>");
;
if("/user/login.html" == window.location.pathname){
var orginUrl = getSearchString("originUrl");
window.location.href = orginUrl == undefined || orginUrl.isBlank() ? "/" : orginUrl;
return;
}
isLogin = true;
if(localStorage.getItem("autoLogin") == 1){
$.cookie('Authorization', data.data.token, { expires: 7 ,path: '/' });
}else {
$.cookie('Authorization', data.data.token,{ path: '/' });
}
}else{
if(needLoginPath.indexOf(window.location.pathname) != -1){
location.href = '/user/login.html';
}
$(".user_link").html("<a href=\"/user/login.html\">登录</a><a href=\"/user/register.html\">注册</a>");
}
},
error: function () {
layer.alert('网络异常');
}
})
});
}
function logout() {
$.cookie('Authorization', null,{ path: '/' });
location.reload();
}
function readHistory() {

View File

@ -1,3 +1,3 @@
<script th:fragment="js" src="/mobile/js/jquery-1.9.1.js"></script>
<script th:fragment="js" src="/mobile/js/common.js"></script>
<script th:fragment="js" src="/mobile/layui/layui.all.js"></script>
<script th:fragment="js" src="/mobile/js/common.js"></script>

View File

@ -40,6 +40,20 @@
-webkit-transition: all .3s;
}
.user_link{
position: relative;
display: inline-block;
vertical-align: middle;
line-height: 50px;
padding: 0 20px;
color: #fff;
float: right;
}
.user_link a{
color: #fff;
}
</style>
@ -85,7 +99,7 @@
<dd><a href="/book/book_ranking.html?sortBy=visit_count&catId=7">女频小说</a></dd>
</dl>
</li>
<li class="layui-nav-item"><a href="/user/favorites.html">书架</a></li>
<li class="user_link"></li>
</ul>
<ul class="layui-nav pc" lay-filter="" style="padding:0 36px;text-align: center">
@ -120,9 +134,12 @@
<dd><a href="/book/book_ranking.html?sortBy=visit_count&catId=7">女频小说</a></dd>
</dl>
</li>
<li class="user_link"></li>
</ul>
<div class="layui-container">
<div class="layui-row">