nsn-util
Version:
NSN工具辅助组件
125 lines (85 loc) • 5.27 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getUrl = exports.isIgnoreUrl = exports.setIgnoreUrl = exports.getParam = exports.isBlobUrl = exports.setBlobUrl = exports.getPageQuery = void 0;
var _nsnConst = require("nsn-const");
var _nsnEnum = require("nsn-enum");
var _querystring = require("querystring");
var NStore = _interopRequireWildcard(require("./NStore"));
var NType = _interopRequireWildcard(require("./NType"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* 获取当前 url 中的所有查询参数
*/
var getPageQuery = function getPageQuery() {
return (0, _querystring.parse)(window.location.href.split('?')[1]);
};
/**
* 获取当前 url 中指定的查询参数
* @param name
*/
exports.getPageQuery = getPageQuery;
var getParam = function getParam(name) {
return getPageQuery()[name];
};
/** 获取 /api/nsn/${ctx}/${uri} 或 /api/${ctx}/${uri}*/
exports.getParam = getParam;
var getUrl = function getUrl(ctx, uri, nonPrefix) {
var mapping = uri || _nsnConst.NConst.EMPTY;
if (NType.isString(uri)) {
mapping = "".concat(uri.startsWith(_nsnConst.NConst.SLASH) ? _nsnConst.NConst.EMPTY : _nsnConst.NConst.SLASH).concat(uri);
}
return "".concat(nonPrefix ? '/api' : _nsnConst.NConst.API_PREFIX, "/").concat(ctx).concat(mapping);
};
/**
* 设置 数据返回是 二进制数据的 api
* @param urls 如:/api/nsn/files/download
*/
exports.getUrl = getUrl;
var setBlobUrl = function setBlobUrl(urls) {
if (urls) {
NStore.set(_defineProperty({}, _nsnConst.NConst.BLOB_URLS, urls));
}
};
/**
* 判断 url 是否
*/
exports.setBlobUrl = setBlobUrl;
var isBlobUrl = function isBlobUrl(url) {
var urls = [getUrl(_nsnEnum.RequestCxt.FILES, '/download')].concat(_toConsumableArray(NStore.get(_nsnConst.NConst.BLOB_URLS) || []));
if (url) {
return !!urls.find(function (u) {
return url.includes(u);
});
}
return false;
};
/**
* 设置 请求后端时,不需要前端检查token的api
* @param urls 如:/api/nsn/auth/login
*/
exports.isBlobUrl = isBlobUrl;
var setIgnoreUrl = function setIgnoreUrl(urls) {
if (urls) {
NStore.set(_defineProperty({}, _nsnConst.NConst.ANONYMOUS_URLS, urls));
}
};
/**
* 判断 url 是否在鉴权忽略列表中
*/
exports.setIgnoreUrl = setIgnoreUrl;
var isIgnoreUrl = function isIgnoreUrl(url) {
var urls = [getUrl(_nsnEnum.RequestCxt.AUTH, '/login')].concat(_toConsumableArray(NStore.get(_nsnConst.NConst.ANONYMOUS_URLS) || []));
return urls.includes(url);
};
exports.isIgnoreUrl = isIgnoreUrl;