98 lines
3.0 KiB
JavaScript
98 lines
3.0 KiB
JavaScript
var __defProp = Object.defineProperty;
|
||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||
var __export = (target, all) => {
|
||
for (var name in all)
|
||
__defProp(target, name, { get: all[name], enumerable: true });
|
||
};
|
||
var __copyProps = (to, from, except, desc) => {
|
||
if (from && typeof from === "object" || typeof from === "function") {
|
||
for (let key of __getOwnPropNames(from))
|
||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||
}
|
||
return to;
|
||
};
|
||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||
|
||
// src/index.tsx
|
||
var src_exports = {};
|
||
__export(src_exports, {
|
||
default: () => src_default
|
||
});
|
||
module.exports = __toCommonJS(src_exports);
|
||
var import_antd = require("antd");
|
||
var import_func = require("@zhst/func");
|
||
var Slave = class {
|
||
// 设置参数
|
||
constructor() {
|
||
this.authTokenDefine = "ZHST_AUTH_TOKEN";
|
||
}
|
||
async init(opt) {
|
||
const {
|
||
jumpToLogin = true,
|
||
jumpUrl,
|
||
tokenKey,
|
||
showMsg = true,
|
||
msgText,
|
||
from,
|
||
to
|
||
} = opt;
|
||
this.checkUrl(jumpUrl, "jumpUrl");
|
||
let currentUrl = new URL(location.href);
|
||
let currentJumpUrl = new URL(jumpUrl);
|
||
let _token = currentUrl.searchParams.get(tokenKey || "token") || this.getToken();
|
||
this.setToken(_token);
|
||
let _fromUrl = from || location.origin + location.pathname;
|
||
this.checkUrl(_fromUrl, "from");
|
||
if (!currentJumpUrl.searchParams.get("from")) {
|
||
currentJumpUrl.searchParams.set("from", _fromUrl);
|
||
}
|
||
let _toUrl = to || location.origin + location.pathname;
|
||
this.checkUrl(_toUrl, "to");
|
||
if (!currentJumpUrl.searchParams.get("to")) {
|
||
currentJumpUrl.searchParams.set("to", _toUrl);
|
||
}
|
||
this.checkEnv({ showMsg, msgText });
|
||
if (jumpToLogin && jumpUrl && !_token) {
|
||
if (currentJumpUrl.origin === location.origin) {
|
||
history.pushState("", "", currentJumpUrl.href);
|
||
} else {
|
||
location.href = currentJumpUrl.href;
|
||
}
|
||
}
|
||
}
|
||
// 判端是否登录
|
||
checkEnv(_opt) {
|
||
const _token = this.getToken() || String(this.getToken());
|
||
if (!_token && _token !== "null" && _opt.showMsg) {
|
||
import_antd.message.error(_opt.msgText || "请先登录!");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
// 检查是否为url,并且跑出错误
|
||
checkUrl(url, label) {
|
||
if (!(0, import_func.isUrl)(url)) {
|
||
throw Error(`请输入正确的 ${label} 链接地址,以http(s)://开头!'`);
|
||
}
|
||
}
|
||
// 退出登录
|
||
logOut() {
|
||
this.removeToken();
|
||
location.replace(location.origin + location.pathname);
|
||
}
|
||
setToken(val) {
|
||
val && localStorage.setItem(this.authTokenDefine, val);
|
||
}
|
||
removeToken(cb) {
|
||
localStorage.removeItem(this.authTokenDefine);
|
||
cb == null ? void 0 : cb();
|
||
}
|
||
getToken() {
|
||
return localStorage.getItem(this.authTokenDefine);
|
||
}
|
||
};
|
||
var src_default = new Slave();
|