feat(安全): 添加HTTP转HTTPS功能并增强URL参数解析
- 新增toHttpsUrl工具函数用于将http链接转换为https - 添加parseQueryParameters函数支持复杂URL参数解析 - 在地址选择面板中应用https转换功能
This commit is contained in:
parent
b471155bb9
commit
ec44a6b0fc
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* 将URL中的 http:// 替换为 https://
|
||||
* @param url 原始URL字符串
|
||||
* @returns 替换后的URL字符串
|
||||
*/
|
||||
export function toHttpsUrl(url: string): string {
|
||||
// 严格类型检查
|
||||
if (typeof url !== 'string') {
|
||||
return url;
|
||||
}
|
||||
// 检查是否以 http:// 开头
|
||||
if (!/^http:\/\//i.test(url)) {
|
||||
return url;
|
||||
}
|
||||
return url.replace(/^http:\/\//i, 'https://');
|
||||
}
|
||||
|
||||
/**
|
||||
* 终极版URL参数解析函数(支持哈希内参数和各种异常格式)
|
||||
* @param url 任意格式的URL字符串
|
||||
* @returns 解析后的参数对象
|
||||
*/
|
||||
export function parseQueryParameters(url: string): Record<string, string> {
|
||||
const params: Record<string, string> = {};
|
||||
if (!url) return params;
|
||||
|
||||
try {
|
||||
// 1. 分离URL主体和哈希部分
|
||||
const [urlMain, hashPart] = url.split('#');
|
||||
|
||||
// 2. 收集所有可能的参数来源
|
||||
const paramSources: string[] = [];
|
||||
|
||||
// 2.1 处理URL主体中的标准查询参数
|
||||
if (urlMain?.includes('?')) {
|
||||
paramSources.push(urlMain.split('?')[1]);
|
||||
}
|
||||
|
||||
// 2.2 处理哈希部分中可能存在的参数
|
||||
if (hashPart?.includes('?')) {
|
||||
paramSources.push(hashPart.split('?')[1]);
|
||||
}
|
||||
|
||||
// 3. 合并并解析所有参数
|
||||
if (paramSources.length > 0) {
|
||||
const allParams = paramSources.join('&');
|
||||
|
||||
allParams.split('&').forEach(param => {
|
||||
// 处理编码的等号(%3D)和普通等号
|
||||
const decodedParam = decodeURIComponent(param);
|
||||
const [key, value] = decodedParam.split('=');
|
||||
|
||||
if (key) {
|
||||
params[key] = value ? decodeURIComponent(value) : '';
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('参数解析失败:', e);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
@click="handleShopSelect(shop.shopId)"
|
||||
>
|
||||
<van-image
|
||||
:src="shop.coverImg || `${publicPath}product-image.png`"
|
||||
:src="toHttpsUrl(shop.coverImg) || `${publicPath}product-image.png`"
|
||||
class="shop-cover-img"
|
||||
fit="cover"
|
||||
/>
|
||||
|
|
@ -43,6 +43,7 @@
|
|||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import { publicPath } from "@/common/utils/path"
|
||||
import { toHttpsUrl } from "@/common/utils/common"
|
||||
|
||||
// Props
|
||||
interface Props {
|
||||
|
|
|
|||
Loading…
Reference in New Issue