Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
click33 committed Aug 7, 2023
1 parent 05e8e8d commit 2ef8a82
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sa-token-doc/doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h1 class="logo-text">Sa-Token</h1>

<!-- -->
<script src="./static/docsify-plugin.js"></script>
<script src="./static/is-star-plugin.js"></script>
<script src="./static/is-star-plugin.js?v=2"></script>
<script>
var saTokenTopVersion = '1.35.0.RC'; // Sa-Token最新版本
var name = '<img style="width: 60px; height: 60px; vertical-align: middle;" src="logo.png" alt="logo" /> ';
Expand Down
85 changes: 61 additions & 24 deletions sa-token-doc/static/is-star-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ var isStarPlugin = function(hook, vm) {

// 钩子函数:每次路由切换时,解析内容之后执行
hook.afterEach(function(html) {
changePage++;
if(changePage % 15 === 0) {
isStarRepo();
}
// changePage++;
// if(changePage % 15 === 0) {
// isStarRepo();
// }
return html;
});

// 每钩子函数:次路由切换时数据全部加载完成后调用,没有参数。
// 钩子函数:每次路由切换时数据全部加载完成后调用,没有参数。
hook.doneEach(function() {

isStarRepo(vm);
});

// 钩子函数:初始化并第一次加载完成数据后调用,没有参数。
Expand All @@ -35,18 +35,18 @@ var isStarPlugin = function(hook, vm) {
// 应用参数
const client_id = '0cc618beb08db99bff50e500e38c2144d95ada9abb51c00c44592726ecd583f4';
const client_secret = '2574c2aac8ce2142e34752dc5957dddcb30bc68df5c61de64251a3a6b11a51e5';
// const redirect_uri = 'http://local.dev33.cn:8848/sa-token-doc/doc.html';
const redirect_uri = 'https://sa-token.cc/doc.html';
// const docDomain = 'local.dev33.cn:8848';
const docDomain = 'sa-token.cc';
// const redirect_uri = 'http://127.0.0.1:8848/sa-token-doc/doc.html';
// const docDomain = '127.0.0.1:8848';

// 检查成功后,多少天不再检查
const allowDisparity = 1000 * 60 * 60 * 24 * 30;
// const allowDisparity = 1000 * 10;


// 判断当前是否已 star
function isStarRepo() {
function isStarRepo(vm) {
// 非PC端不检查
if(document.body.offsetWidth < 800) {
console.log('small screen ...');
Expand Down Expand Up @@ -76,7 +76,16 @@ function isStarRepo() {
console.error(e);
}

// 白名单路由不判断
const whiteList = ['/', '/more/link', '/more/demand-commit', '/more/join-group', '/more/sa-token-donate',
'/sso/sso-pro', '/more/update-log', '/more/common-questions', '/fun/sa-token-test', '/fun/issue-template'];
if(whiteList.indexOf(vm.route.path) >= 0 && getParam('code') === null) {
console.log('white route ...');
return;
}

// 开始获取 code
$('body').css({'overflow': 'hidden'});
getCode();
}

Expand All @@ -103,14 +112,24 @@ function confirmStar() {
'<div>1、打开 Sa-Token <a href="https://gitee.com/dromara/sa-token" target="_blank">开源仓库主页</a>,在右上角点个 star 。</div>' +
'<div>2、点击下方 [ 同意授权检测 ] 按钮,同意 Sa-Token 获取 API 权限进行检测。<a href="javascript:authDetails();" style="text-decoration: none;">?</a></div>' +
"</div>";
tipStr += "<p><b>不点也没关系 ❤️ ❤️ ❤️,Sa-Token 将努力变得更加完善!😇</b></p>";
tipStr += "<p><b>本章节文档将在 star 后正常开放展示</b></p>";
tipStr = '<div>' + tipStr + '</div>'
layer.confirm(tipStr, {title: '提示', btn: ['同意授权检测', '暂时不要,我先看看文档'], area: '450px', offset: '20%'}, function(index) {
//
layer.close(index);
// 用户点了确认,去 gitee 官方请求授权获取
goAuth();
});

layer.confirm(tipStr, {
title: '提示',
btn: ['同意授权检测'],
// btn: ['同意授权检测', '暂时不要,我先看看文档'],
area: '450px',
offset: '25%',
closeBtn: false
},
function(index) {
//
layer.close(index);
// 用户点了确认,去 gitee 官方请求授权获取
goAuth();
}
);
}


Expand All @@ -129,7 +148,7 @@ function goAuth() {
function getAccessToken(code) {
// 根据 code 获取 access_token
$.ajax({
url: 'https://gitee.com/oauth/token',
url: 'https://sa-token.cc/server/oauth/token',
method: 'post',
data: {
grant_type: 'authorization_code',
Expand All @@ -139,6 +158,14 @@ function getAccessToken(code) {
client_secret: client_secret,
},
success: function(res) {
// 如果返回的不是 200
if(res.code !== 200) {
return layer.alert(res.msg, {closeBtn: false}, function(){
// 刷新url,去掉 code 参数
location.href = 'doc.html';
});
}

// 拿到 access_token
const access_token = res.access_token;

Expand Down Expand Up @@ -166,7 +193,7 @@ function getAccessToken(code) {
// 如下返回,代表没有 star
if(e.statusText = 'Not Found'){
console.log('not star ...');
layer.alert('未检测到 star 数据...', function() {
layer.alert('未检测到 star 数据...', {closeBtn: false}, function() {
// 刷新url,去掉 code 参数
location.href = location.href.replace("?code=" + code, '');
});
Expand All @@ -177,18 +204,30 @@ function getAccessToken(code) {
},
error: function(e) {
console.log('请求错误 ', e);
// 如果请求地址有错,则暂停一天
if(e.status === 0 || e.status === 502) {
return layer.alert(JSON.stringify(e), {closeBtn: false}, function(){
// 一天内不再检查,否则
const ygTime = allowDisparity - (1000 * 60 * 60 * 24);
localStorage.isStarRepo = new Date().getTime() - ygTime;
// 刷新url,去掉 code 参数
location.href = location.href.replace("?code=" + code, '');
});
}

// 无效授权,可能是 code 无效
if(e.responseJSON.error == 'invalid_grant') {
const errorMsg = (e.responseJSON && e.responseJSON.error) || JSON.stringify(e);
if(errorMsg == 'invalid_grant') {
console.log('无效code', code);
}
layer.alert('check error...' + e.responseJSON.error, function(index) {
layer.alert('check error... ' + errorMsg, function(index) {
layer.close(index);
// 刷新url,去掉 code 参数
let url = location.href.replace("?code=" + code, '');
url = url.replace("&code=" + code, '');
location.href = url;
});
// 一天内不再检查,否则容易引起无限重定向
// 一天内不再检查,否则
// const ygTime = allowDisparity - (1000 * 60 * 60 * 24);
// localStorage.isStarRepo = new Date().getTime() - ygTime;
// // 刷新url,去掉 code 参数
Expand All @@ -197,14 +236,12 @@ function getAccessToken(code) {
})
}


// 疑问
function authDetails() {
const str = "用于检测的凭证信息将仅保存你的浏览器本地,不发往任何第三方服务器,Sa-Token 文档已完整开源,源码可查";
const str = "用于检测的凭证信息将仅保存你的浏览器本地,Sa-Token 文档已完整开源,源码可查";
alert(str);
}


// 获取 url 携带的参数
function getParam(name, defaultValue){
var query = window.location.search.substring(1);
Expand Down

0 comments on commit 2ef8a82

Please sign in to comment.