import * as qs from './querystring'; /** * 获取地址对应参数 * @param name * @param search * @returns */ export const getQuery = (name: string, search?: string): string => { const reg = new RegExp(`(\\?|&)${name}=([^&]*)(&|$)`, 'i'); let searchParams = window.location.href; const values = reg.exec((search || searchParams)); if (values != null) { return decodeURIComponent(values[2]) || ''; } return ''; }; /** * 获取地址所有参数 * @param search * @returns */ export const getQueries = (search?: string): Record => { let str = search || window.location.search; str = str.replace('?', ''); let result = qs.parse(str); return (result || {}) as Record; }; /** * 获取地址对应参数 * @param name * @param urlStr * @returns */ export const getQueryName = (name: string, urlStr?: string): string => { let url = urlStr || decodeURI(window.location.href); url = url.split('?')[1]; const querys: any = new Object(); if (url.indexOf('?') === -1) { const strs = url.split('&'); for (let i = 0; i < strs.length; i++) { const oneStr = strs[i]; const index = oneStr.indexOf('='); if (index > 0) { querys[oneStr.substr(0, index)] = decodeURI(oneStr.substr(index + 1)); } } } return querys[name] || ''; };