132 lines
3.8 KiB
JavaScript
132 lines
3.8 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
|
||
// force
|
||
} = opt;
|
||
this.config = 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);
|
||
this._setUrlKey({
|
||
targetKey: "from",
|
||
targetUrl: from,
|
||
jumpUrl: currentJumpUrl
|
||
});
|
||
this._setUrlKey({
|
||
targetKey: "to",
|
||
targetUrl: to,
|
||
jumpUrl: currentJumpUrl
|
||
});
|
||
const isLogin = this.checkEnv({ showMsg, msgText });
|
||
if (jumpToLogin && !isLogin) {
|
||
if (currentJumpUrl.origin === location.origin) {
|
||
history.pushState("", "", currentJumpUrl.href);
|
||
} else {
|
||
location.href = currentJumpUrl.href;
|
||
}
|
||
}
|
||
}
|
||
// 设置 URL 中的值
|
||
/**
|
||
* @param opt targetUrl 目标链接,目标链接的 key 值,sourceUrl: 需要添加参数的链接
|
||
*/
|
||
_setUrlKey(opt) {
|
||
const {
|
||
targetUrl,
|
||
targetKey,
|
||
jumpUrl
|
||
} = opt;
|
||
let _targetUrl = targetUrl || location.origin + location.pathname;
|
||
this.checkUrl(_targetUrl, targetKey);
|
||
if (!jumpUrl.searchParams.get(targetKey)) {
|
||
jumpUrl.searchParams.set(targetKey, _targetUrl);
|
||
}
|
||
}
|
||
/**
|
||
* 判端是否登录
|
||
* @param _opt showMsg 是否提示 | msgText 提示文案
|
||
* @returns boolean
|
||
*/
|
||
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(jumpUrl, config) {
|
||
this.removeToken();
|
||
const { from, to } = config || this.config || {};
|
||
const targetUrl = new URL(jumpUrl);
|
||
this._setUrlKey({
|
||
targetKey: "from",
|
||
targetUrl: from,
|
||
jumpUrl: targetUrl
|
||
});
|
||
this._setUrlKey({
|
||
targetKey: "to",
|
||
targetUrl: to,
|
||
jumpUrl: targetUrl
|
||
});
|
||
location.href = targetUrl.href;
|
||
}
|
||
setToken(val, tokenKey = this.authTokenDefine) {
|
||
val && localStorage.setItem(tokenKey, val);
|
||
}
|
||
removeToken(cb, tokenKey = this.authTokenDefine) {
|
||
localStorage.removeItem(tokenKey);
|
||
cb == null ? void 0 : cb();
|
||
}
|
||
getToken(tokenKey = this.authTokenDefine) {
|
||
return localStorage.getItem(tokenKey);
|
||
}
|
||
};
|
||
var src_default = new Slave();
|