feat(安全): 添加HTTP转HTTPS功能并增强URL参数解析

- 新增toHttpsUrl工具函数用于将http链接转换为https
- 添加parseQueryParameters函数支持复杂URL参数解析
- 在地址选择面板中应用https转换功能
This commit is contained in:
dzq 2025-12-26 08:48:10 +08:00
parent b471155bb9
commit ec44a6b0fc
2 changed files with 65 additions and 1 deletions

View File

@ -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;
}

View File

@ -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 {