/** * 获取url?后参数 */ export const getQueryString = (key: string, searchStr?: string) => { var n = new RegExp(`[&,?]${key}=([^\\&]*)`, 'i'), oSearch = n.exec(searchStr || window.location.search), oHash = n.exec(searchStr || window.location.hash); // 兼容hash传递的参数 return (oSearch ? oSearch[1] : '') || (oHash ? oHash[1] : ''); }; export const getQueryMap = (url: string) => { var e, n, o: any = {}, a = /[\?\&][^\?\&]+=[^\?\&#]+/g, i = /[\?\&]([^=\?]+)=([^\?\&#]+)/; if (((url = url || window.location.href), (e = url.match(a)), !e)) { return o; } for (var r = 0, l = e.length; r < l; r++) { n = e[r].match(i); null !== n && (o[n[1]] = n[2]); } return o; }; export const setLocal = (name: string, obj: Object) => { let user: any = localStorage.getItem(name); let moreInfo = Object.assign({}, JSON.parse(user), obj); localStorage.setItem(name, JSON.stringify(moreInfo)); }; export const getLocal = (name: string, key?: string) => { let str = localStorage.getItem(name); let obj = str && JSON.parse(str); if (key) { return obj && obj[key]; } else { return obj; } }; export const getSources = () => { let sources = getQueryString('sources', location.href) || ''; return sources; }; //跳转 export const jump = (pathname: string, redictUrl?: string) => { const { origin, href, hash, search } = redictUrl ? new URL(redictUrl) : window.location; const resultUrl = redictUrl ? redictUrl : href; const urlParams = hash ? hash.replace('#', '&') : search.replace('?', '&'); let url = `${origin}${pathname}?redirectUrl=${encodeURIComponent( resultUrl )}${urlParams}`; window.location.href = url; }