nsn-util
Version:
NSN工具辅助组件
96 lines (71 loc) • 3.37 kB
JavaScript
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; }
import { NConst } from 'nsn-const';
import { RequestCxt } from 'nsn-enum';
import { parse } from 'querystring';
import * as NStore from './NStore';
import * as NType from './NType';
/**
* 获取当前 url 中的所有查询参数
*/
var getPageQuery = function getPageQuery() {
return parse(window.location.href.split('?')[1]);
};
/**
* 获取当前 url 中指定的查询参数
* @param name
*/
var getParam = function getParam(name) {
return getPageQuery()[name];
};
/** 获取 /api/nsn/${ctx}/${uri} 或 /api/${ctx}/${uri}*/
var getUrl = function getUrl(ctx, uri, nonPrefix) {
var mapping = uri || NConst.EMPTY;
if (NType.isString(uri)) {
mapping = "".concat(uri.startsWith(NConst.SLASH) ? NConst.EMPTY : NConst.SLASH).concat(uri);
}
return "".concat(nonPrefix ? '/api' : NConst.API_PREFIX, "/").concat(ctx).concat(mapping);
};
/**
* 设置 数据返回是 二进制数据的 api
* @param urls 如:/api/nsn/files/download
*/
var setBlobUrl = function setBlobUrl(urls) {
if (urls) {
NStore.set(_defineProperty({}, NConst.BLOB_URLS, urls));
}
};
/**
* 判断 url 是否
*/
var isBlobUrl = function isBlobUrl(url) {
var urls = [getUrl(RequestCxt.FILES, '/download')].concat(_toConsumableArray(NStore.get(NConst.BLOB_URLS) || []));
if (url) {
return !!urls.find(function (u) {
return url.includes(u);
});
}
return false;
};
/**
* 设置 请求后端时,不需要前端检查token的api
* @param urls 如:/api/nsn/auth/login
*/
var setIgnoreUrl = function setIgnoreUrl(urls) {
if (urls) {
NStore.set(_defineProperty({}, NConst.ANONYMOUS_URLS, urls));
}
};
/**
* 判断 url 是否在鉴权忽略列表中
*/
var isIgnoreUrl = function isIgnoreUrl(url) {
var urls = [getUrl(RequestCxt.AUTH, '/login')].concat(_toConsumableArray(NStore.get(NConst.ANONYMOUS_URLS) || []));
return urls.includes(url);
};
export { getPageQuery, setBlobUrl, isBlobUrl, getParam, setIgnoreUrl, isIgnoreUrl, getUrl };