sa-token/sa-token-doc/static/is-star-plugin.js

260 lines
7.4 KiB
JavaScript
Raw Normal View History

2023-07-27 06:36:55 +08:00
//
// 声明 docsify 插件
var isStarPlugin = function(hook, vm) {
// 钩子函数:解析之前执行
hook.beforeEach(function(content) {
return content;
});
// 钩子函数:每次路由切换时,解析内容之后执行
hook.afterEach(function(html) {
return html;
});
2023-08-07 10:47:34 +08:00
// 钩子函数:每次路由切换时数据全部加载完成后调用,没有参数。
2023-07-27 06:36:55 +08:00
hook.doneEach(function() {
2023-12-07 15:36:26 +08:00
//isStarRepo(vm);
2023-07-27 06:36:55 +08:00
});
// 钩子函数:初始化并第一次加载完成数据后调用,没有参数。
hook.ready(function() {
});
}
// 应用参数
const client_id = '0cc618beb08db99bff50e500e38c2144d95ada9abb51c00c44592726ecd583f4';
2023-09-12 19:26:28 +08:00
const client_secret = 'xxx';
2023-07-27 06:36:55 +08:00
const redirect_uri = 'https://sa-token.cc/doc.html';
const docDomain = 'sa-token.cc';
2023-08-07 10:47:34 +08:00
// const redirect_uri = 'http://127.0.0.1:8848/sa-token-doc/doc.html';
// const docDomain = '127.0.0.1:8848';
2023-07-27 06:36:55 +08:00
// 检查成功后,多少天不再检查
2023-08-30 11:26:27 +08:00
const allowDisparity = 1000 * 60 * 60 * 24 * 30 * 3;
2023-07-27 06:36:55 +08:00
// const allowDisparity = 1000 * 10;
// 判断当前是否已 star
2023-08-07 10:47:34 +08:00
function isStarRepo(vm) {
2023-07-27 06:36:55 +08:00
// 非PC端不检查
if(document.body.offsetWidth < 800) {
console.log('small screen ...');
return;
}
// 判断是否在主域名下
if(location.host !== docDomain) {
2023-09-12 19:26:28 +08:00
console.log('not domain, no check...');
2023-09-03 12:52:40 +08:00
return;
2023-07-27 06:36:55 +08:00
}
// 判断是否近期已经判断过了
try{
const isStarRepo = localStorage.isStarRepo;
if(isStarRepo) {
// 记录 star 的时间,和当前时间的差距
const disparity = new Date().getTime() - parseInt(isStarRepo);
// 差距小于一月,不再检测,大于一月,再检测一下
if(disparity < allowDisparity) {
console.log('checked ...');
return;
}
}
}catch(e){
console.error(e);
}
2023-08-07 10:47:34 +08:00
// 白名单路由不判断
2023-09-12 19:26:28 +08:00
const whiteList = ['/a', '/more/link', '/more/demand-commit', '/more/join-group', '/more/sa-token-donate',
2023-08-07 10:47:34 +08:00
'/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;
}
2023-07-27 06:36:55 +08:00
// 开始获取 code
2023-08-07 10:47:34 +08:00
$('body').css({'overflow': 'hidden'});
2023-07-27 06:36:55 +08:00
getCode();
}
// 去请求授权
function getCode() {
// 检查url中是否有code
const code = getParam('code');
if(code) {
// 有 code进一步去请求 access_token
getAccessToken(code);
} else {
// 不存在code弹窗提示询问
confirmStar();
}
}
// 弹窗提示点 star
function confirmStar() {
// 弹窗提示文字
2023-08-09 13:33:01 +08:00
const tipStr = `
<div>
<p><b>同学来支持一下 Sa-Token 为项目点个 star </b></p>
<div>仅需两步即可完成<br>
<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>
2023-09-03 12:51:58 +08:00
<p><b>本章节文档将在 star 后正常开放展示</b></p>
<p style="color: green;">开源不易希望您不吝支持激励开源项目走的更加长远 😇😇😇</p>
2023-08-09 13:33:01 +08:00
</div>
`;
2023-08-07 10:47:34 +08:00
2023-08-09 13:33:01 +08:00
const index = layer.confirm(tipStr, {
2023-08-07 10:47:34 +08:00
title: '提示',
btn: ['同意授权检测'],
// btn: ['同意授权检测', '暂时不要,我先看看文档'],
2023-09-03 12:51:58 +08:00
area: '460px',
2023-08-07 10:47:34 +08:00
offset: '25%',
closeBtn: false
},
function(index) {
//
layer.close(index);
// 用户点了确认,去 gitee 官方请求授权获取
goAuth();
}
);
2023-08-30 11:26:27 +08:00
// 源码注释提示
2023-08-09 13:33:01 +08:00
const closeLayer =
`
<!--
f12 控制台 执行一下
localStorage.isStarRepo = new Date().getTime()
即可取消弹窗 执行完刷新一下页面
-->
`;
$('#layui-layer' + index).prepend(closeLayer)
2023-07-27 06:36:55 +08:00
}
// 跳转到 gitee 授权界面
function goAuth() {
const authUrl = "https://gitee.com/oauth/authorize" +
"?client_id=" + client_id +
"&redirect_uri=" + redirect_uri +
2023-08-24 12:58:45 +08:00
"&response_type=code";
2023-07-27 06:36:55 +08:00
location.href = authUrl;
}
// 获取 access_token
function getAccessToken(code) {
// 根据 code 获取 access_token
$.ajax({
2023-08-07 10:47:34 +08:00
url: 'https://sa-token.cc/server/oauth/token',
2023-07-27 06:36:55 +08:00
method: 'post',
data: {
grant_type: 'authorization_code',
code: code,
client_id: client_id,
redirect_uri: redirect_uri,
client_secret: client_secret,
},
success: function(res) {
2023-08-07 10:47:34 +08:00
// 如果返回的不是 200
if(res.code !== 200) {
return layer.alert(res.msg, {closeBtn: false}, function(){
// 刷新url去掉 code 参数
location.href = 'doc.html';
});
}
2023-07-27 06:36:55 +08:00
// 拿到 access_token
const access_token = res.access_token;
// 根据 access_token 判断是否 star 了仓库
$.ajax({
url: 'https://gitee.com/api/v5/user/starred/dromara/sa-token',
method: 'get',
data: {
access_token: access_token
},
success: function(res) {
// success 回调即代表已经 stargitee API 请求体不返回任何数据
console.log('-> stared ...');
// 记录本次检查时间
localStorage.isStarRepo = new Date().getTime();
//
layer.alert('感谢你的支持 ❤️ ❤️ ❤️ Sa-Token 将努力变得更加完善!', function(index) {
layer.close(index);
// 刷新url去掉 code 参数
location.href = location.href.replace("?code=" + code, '');
})
},
error: function(e) {
// console.log('ff请求错误 ', e);
// 如下返回,代表没有 star
if(e.statusText = 'Not Found'){
console.log('not star ...');
2023-08-07 10:47:34 +08:00
layer.alert('未检测到 star 数据...', {closeBtn: false}, function() {
2023-07-27 06:36:55 +08:00
// 刷新url去掉 code 参数
location.href = location.href.replace("?code=" + code, '');
});
}
}
});
},
error: function(e) {
console.log('请求错误 ', e);
2023-08-09 11:29:43 +08:00
// 如果请求地址有错,可能是服务器宕机了,暂停一天检测
2023-08-07 10:47:34 +08:00
if(e.status === 0 || e.status === 502) {
return layer.alert(JSON.stringify(e), {closeBtn: false}, function(){
2023-08-09 11:29:43 +08:00
// 一天内不再检查
2023-08-07 10:47:34 +08:00
const ygTime = allowDisparity - (1000 * 60 * 60 * 24);
localStorage.isStarRepo = new Date().getTime() - ygTime;
2023-08-09 11:29:43 +08:00
// 刷新 url去掉 code 参数
2023-08-07 10:47:34 +08:00
location.href = location.href.replace("?code=" + code, '');
});
}
2023-07-27 06:36:55 +08:00
// 无效授权,可能是 code 无效
2023-08-07 10:47:34 +08:00
const errorMsg = (e.responseJSON && e.responseJSON.error) || JSON.stringify(e);
if(errorMsg == 'invalid_grant') {
2023-07-27 06:36:55 +08:00
console.log('无效code', code);
}
2023-08-07 10:47:34 +08:00
layer.alert('check error... ' + errorMsg, function(index) {
2023-07-27 06:36:55 +08:00
layer.close(index);
// 刷新url去掉 code 参数
let url = location.href.replace("?code=" + code, '');
url = url.replace("&code=" + code, '');
location.href = url;
});
}
})
}
// 疑问
function authDetails() {
2023-08-07 10:47:34 +08:00
const str = "用于检测的凭证信息将仅保存你的浏览器本地Sa-Token 文档已完整开源,源码可查";
2023-07-27 06:36:55 +08:00
alert(str);
}
// 获取 url 携带的参数
function getParam(name, defaultValue){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == name){return pair[1];}
}
return(defaultValue == undefined ? null : defaultValue);
}