UNPKG

@witlink/utils

Version:

witlink的工具函数库

2 lines (1 loc) 2.96 kB
(function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.witlinkUtils={}))})(this,function(n){"use strict";const i="Witlink-Usercenter-QureyParams";function l(){const e=localStorage.getItem(i)||"{}",{token:t,lang:r,usercenterUrl:o}=JSON.parse(e);return{token:t,lang:r,usercenterUrl:o}}function y(e){const t=l();localStorage.setItem(i,JSON.stringify({...t,...e}))}function C(){const e=window.location.href,t={},r=e.split("?")[1];return r&&r.split("&").forEach(o=>{const[a,c]=o.split("=");a&&c&&(t[a]=decodeURIComponent(c))}),t}const O="Witlink-Usercenter";function E(){const{usercenterUrl:e}=l();window.location.href=e}function p(){window.open(l().usercenterUrl,O),window.close()}const N="",u="ACCESS_TOKEN",f="locale",g="userInfo",h="menuInfo";function v(e){const t=new URL(e);return t.hash&&t.hash.startsWith("#/")?`/${t.hash.substring(2).split("?")[0]}`.replace(/\/+/g,"/"):t.pathname}function U(e,t={}){const r=Array.isArray(e)?e:[e];for(const o of r){if(o.path&&o.name!==void 0){const a=o.path.startsWith("/")?o.path:`/${o.path}`;t[a]=o.name.toString()}o.children&&o.children.length>0&&U(o.children,t)}return t}function s(e){return N+e}function T(e){localStorage.setItem(s(u),e)}function M(){return localStorage.getItem(s(u))}function w(){localStorage.removeItem(s(u))}function b(e){localStorage.setItem(s(f),e)}function K(){return localStorage.getItem(s(f))}function L(){localStorage.removeItem(s(f))}function A(e={}){e={...I()||{},...e},localStorage.setItem(s(g),JSON.stringify(e))}function I(e){const t=localStorage.getItem(s(g));if(e){if(typeof e=="string")return(t?JSON.parse(t):{})[e]||null;if(Array.isArray(e)){const r=t?JSON.parse(t):{},o={};return e.forEach(a=>{o[a]=r[a]||null}),o}}return t?JSON.parse(t):null}function _(e){localStorage.setItem(s(h),JSON.stringify(e))}function d(){const e=localStorage.getItem(s(h));return e?JSON.parse(e):null}const S={},m={};function j(){if(Object.keys(S).length===0){const e=d()||[];U(e,S)}return S}function P(){return Object.keys(m).length===0&&((I()||{}).role.permissions||[]).forEach(r=>{var o;if(r.actionEntitySet!=null&&((o=r.actionEntitySet)==null?void 0:o.length)>0){const a=r.actionEntitySet.map(c=>c.action);m[r.permissionId]=a}}),m}function k(e){const t=P(),r=v(window.location.href),a=j()[r]||"";return(t[a]||[]).includes(e)}function J(){localStorage.removeItem(s(g)),localStorage.removeItem(s(h)),localStorage.removeItem(s(u)),localStorage.removeItem(s(f))}n.getButtonPermissions=P,n.getLocale=K,n.getMenuInfo=d,n.getToken=M,n.getUserCenterParams=l,n.getUserCenterParamsByUrl=C,n.getUserInfo=I,n.hasPermission=k,n.logoutToUserCenter=p,n.removeAll=J,n.removeLocale=L,n.removeToken=w,n.setLocale=b,n.setMenuInfo=_,n.setToken=T,n.setUserCenterParams=y,n.setUserInfo=A,n.toUserCenterConsole=E,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});