1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-12 19:51:39 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

Gitee授权登录成功

This commit is contained in:
2022-04-04 20:49:20 +08:00
parent 54911675a1
commit 5acd55c687
16 changed files with 229 additions and 63 deletions

View File

@@ -11,9 +11,6 @@ html,
body {
margin: 0;
font-family: bookshelfplusFont;
/* 字体加载前先隐藏,不然文字会闪一下 */
opacity: 0;
}
a,

View File

@@ -103,15 +103,32 @@ router.get('/dashboard/:group/:page', function (req, res) {
}, {
name: "我的收藏",
url: "/dashboard/user/myCollection"
}, {
name: "账号设置",
url: "/dashboard/user/myAccount"
}
];
}
var headText = req.params.group === "admin" ? "后台管理" : "用户中心";
var title = getPageTitle(headText);
var headSubTextArr = {
// 管理员
"UserManage": "用户管理",
"BookManage": "书籍管理",
"CategoryManage": "分类管理",
"Debug": "调试",
// 用户
"myBookshelf": "我的书架",
"myCollection": "我的收藏",
"myAccount": "账号设置"
};
// 仪表盘
if (req.params.page == "index") {
res.render(`dashboard/${req.params.group}/index`, {
title: getPageTitle(req.params.group === "admin" ? "后台管理" : "用户中心"),
headText: req.params.group === "admin" ? "后台管理" : "用户中心",
title: title,
headText: headText,
headSubTextArr: {},
links: navbarLinks,
group: req.params.group,
@@ -120,27 +137,32 @@ router.get('/dashboard/:group/:page', function (req, res) {
return;
}
// 后台管理 其他管理页面
// 后台管理 新增或者修改页面
if ((req.params.group === "admin" && ["UserManage", "BookManage", "CategoryManage", "Debug"].indexOf(req.params.page) > -1) ||
(req.params.group === "user" && ["myBookshelf", "myCollection"].indexOf(req.params.page) > -1)) {
res.render(`dashboard/${req.params.group}/manage`, {
title: getPageTitle(req.params.group === "admin" ? "后台管理" : "用户中心"),
headSubTextArr: {
// 管理员
"UserManage": "用户管理",
"BookManage": "书籍管理",
"CategoryManage": "分类管理",
"Debug": "调试",
// 用户
"myBookshelf": "我的书架",
"myCollection": "我的收藏",
},
title: title,
headSubTextArr: headSubTextArr,
links: navbarLinks,
group: req.params.group,
page: req.params.page,
});
return;
}
// 后台管理 其他管理页面
if ((req.params.group === "admin" && [].indexOf(req.params.page) > -1) ||
(req.params.group === "user" && ["myAccount"].indexOf(req.params.page) > -1)) {
res.render(`dashboard/${req.params.group}/${req.params.page}`, {
title: title,
headSubTextArr: headSubTextArr,
links: navbarLinks,
group: req.params.group,
page: req.params.page,
});
return;
}
throw new Error("404 Not Found");
});

View File

@@ -21,21 +21,41 @@
正在跳转中,请稍后...
</p>
<script>
getRequest("/third-party/callback/<%=platform%>" + location.search)
// 带 token 的为绑定第三方账号,不带 token 的为第三方登录
getRequest("/third-party/callback/<%=platform%>" + location.search + (localStorage.token ? ("&token=" + localStorage.token) : ""))
.then(function (response) {
var axiosData = response.data;
var status = axiosData.status;
var data = axiosData.data;
if (status === "success") {
console.log(data)
// 默认直接跳转 user 后台,如果是管理员则由 user 后台跳转
if(localStorage.token) {
// 绑定第三方账号
// 绑定第三方账号成功
alert("绑定成功");
location.href = "/dashboard/user/myAccount";
} else {
// 第三方登录成功
localStorage.setItem("token", data.token);
// alert("登录成功");
if(data.group === "ADMIN") {
localStorage.setItem("is_admin", "true");
window.location.href = "/dashboard/admin/index";
} else {
localStorage.setItem("is_admin", "false");
window.location.href = "/dashboard/user/index";
}
}
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
location.replace("/login");
}
}).catch(function (error) {
console.log(error);
document.getElementById("displayText").innerHTML="系统错误,请稍后再试!";
document.getElementById("displayText").style.color="red";
document.getElementById("displayText").innerHTML = "系统错误,请稍后再试!";
document.getElementById("displayText").style.color = "red";
});
</script>
</body>

View File

@@ -13,4 +13,8 @@
// API地址
const APIHOST = '<%= global.site.api.prefix %>';
axios.defaults.baseURL = APIHOST;
</script>
</script>
<style>
/* 字体加载前先隐藏,不然文字会闪一下 */
html, body { opacity: 0; }
</style>

View File

@@ -0,0 +1,20 @@
<button type="button" onclick="thirdPartyLogin('gitee')">Gitee</button>
<button type="button" onclick="thirdPartyLogin('qq')">QQ</button>
<script>
// 第三方授权登录逻辑
function thirdPartyLogin(type) {
getRequest("/third-party/login", { platform: type })
.then(function (response) {
var axiosData = response.data;
var status = axiosData.status;
var data = axiosData.data;
if (status === "success") {
location.href = data;
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
}
}).catch(function (error) {
console.log(error);
});
}
</script>

View File

@@ -52,7 +52,7 @@
if(linkRoute.length > 2) {
var linkGroup = linkRoute[linkRoute.length-2];
var linkPage = linkRoute[linkRoute.length-1];
console.log(element, linkGroup, linkPage);
// console.log(element, linkGroup, linkPage);
if(page == linkPage) {
$(element).addClass("active");
}

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%- include("../component/header.html"); %>
<%- include("../component/header-user.html"); %>
</head>
<body>
<%- include("../component/navbar.html"); %>
<main class="main">
<h1><%= headSubTextArr[page] || page %></h1>
<div id="container">
<div>
第三方账号绑定:
<%- include("../../component/third-party-login-button.html"); %>
</div>
</div>
</main>
<%- include("../component/footer-user.html"); %>
</body>
</html>

View File

@@ -72,9 +72,7 @@
</div>
<button class="btn-submit">登录</button>
<p>
快捷登录:
<button type="button" onclick="thirdPartyLogin('gitee')">Gitee</button>
<button type="button" onclick="thirdPartyLogin('qq')">QQ</button>
快捷登录:<%- include("./component/third-party-login-button.html"); %>
</p>
<p>
<a href="/register">注册</a>
@@ -130,23 +128,5 @@
});
});
</script>
<script>
// 第三方授权登录逻辑
function thirdPartyLogin(type) {
getRequest("/third-party/login", { platform: type })
.then(function (response) {
var axiosData = response.data;
var status = axiosData.status;
var data = axiosData.data;
if (status === "success") {
location.href = data;
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
}
}).catch(function (error) {
console.log(error);
});
}
</script>
</body>
</html>