wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { storeToRefs } from 'pinia';
import type { RouteLocationNamedRaw } from 'vue-router';
import router from '/@/router';
import { useKeepALiveNames } from '/@/stores/keepAliveNames';
 
type RouteLocationName = {
    [K in keyof RouteLocationNamedRaw]: K extends 'name' ? string : RouteLocationNamedRaw[K];
};
 
/**
 * 按路由名称跳转至指定路由
 * 路由已缓存时,会清除缓存
 * @param routeLocation 路由位置,与 router.push 参数一致
 */
export const gotoRoute = (routeLocation: RouteLocationName) => {
    const storesKeepAliveNames = useKeepALiveNames();
    const routeName = routeLocation.name;
    const { cachedViews } = storeToRefs(storesKeepAliveNames);
    const isInclude = cachedViews.value.includes(routeName);
    if (isInclude) {
        storesKeepAliveNames.delCachedView({
            name: routeName,
        });
    }
 
    router.push(routeLocation);
};
 
/**
 * 获取 url 上的参数
 * let href = 'https://www.baidu.com/s?wd=哈哈哈&rsv_spt=1'
 * getAllUrlParams(href )
 * @param urlStr
 */
export const getAllUrlParams = (urlStr) => {
    const url = urlStr || location.href;
    // 用JS拿到URL,如果函数接收了URL,那就用函数的参数。如果没传参,就使用当前页面的URL
    let queryString = url ? url.split('?')[1] : window.location.search.slice(1);
    // 用来存储我们所有的参数
    const obj = {};
    // 如果没有传参,返回一个空对象
    if (!queryString) {
        return obj;
    }
    // stuff after # is not part of query string, so get rid of it
    queryString = queryString.split('#')[0];
    // 将参数分成数组
    const arr = queryString.split('&');
    for (let i = 0; i < arr.length; i++) {
        // 分离成key:value的形式
        const a = arr[i].split('=');
        // 将undefined标记为true
        const paramName = a[0];
        const paramValue = typeof a[1] === 'undefined' ? true : a[1];
        if (paramName.match(/\[(\d+)?\]$/)) {
            // 如果paramName不存在,则创建key
            const key = paramName.replace(/\[(\d+)?\]/, '');
            if (!obj[key]) obj[key] = [];
            // 如果是索引数组 e.g. colors[2]
            if (paramName.match(/\[\d+\]$/)) {
                // 获取索引值并在对应的位置添加值
                const index = /\[(\d+)\]/.exec(paramName)[1];
                obj[key][index] = paramValue;
            } else {
                // 如果是其它的类型,也放到数组中
                obj[key].push(paramValue);
            }
        } else {
            // 处理字符串类型
            if (!obj[paramName]) {
                // 如果如果paramName不存在,则创建对象的属性
                obj[paramName] = paramValue;
            } else if (obj[paramName] && typeof obj[paramName] === 'string') {
                // 如果属性存在,并且是个字符串,那么就转换为数组
                obj[paramName] = [obj[paramName]];
                obj[paramName].push(paramValue);
            } else {
                // 如果是其它的类型,还是往数组里丢
                obj[paramName].push(paramValue);
            }
        }
    }
    return obj;
};