Skip to content

Commit 497d536

Browse files
author
Taois
committed
feat:url安全的base64编码解码
1 parent 5659853 commit 497d536

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

dashboard/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@fastify/static": "^6.12.0",
2323
"artplayer": "^5.3.0",
2424
"axios": "1.12.0",
25+
"crypto-js": "^4.2.0",
2526
"echarts": "^5.6.0",
2627
"fastify": "^4.29.1",
2728
"flv.js": "^1.6.2",

dashboard/pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dashboard/src/api/utils/index.js

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,69 @@
22
* API工具函数
33
* 提供常用的数据处理、验证和转换功能
44
*/
5-
5+
import CryptoJS from 'crypto-js'
66
/**
7-
* Base64编码
8-
* @param {string} str - 需要编码的字符串
7+
* Base64编码(使用CryptoJS)
8+
* @param {string} text 待编码文本
99
* @returns {string} Base64编码结果
1010
*/
11-
export const base64Encode = (str) => {
12-
try {
13-
return btoa(unescape(encodeURIComponent(str)))
14-
} catch (error) {
15-
console.error('Base64编码失败:', error)
16-
return str
17-
}
11+
export const base64Encode = (text) => {
12+
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
1813
}
1914

2015
/**
21-
* Base64解码
22-
* @param {string} str - 需要解码的Base64字符串
16+
* Base64解码(使用CryptoJS)
17+
* @param {string} text Base64编码文本
2318
* @returns {string} 解码结果
2419
*/
25-
export const base64Decode = (str) => {
26-
try {
27-
return decodeURIComponent(escape(atob(str)))
28-
} catch (error) {
29-
console.error('Base64解码失败:', error)
30-
return str
20+
export const base64Decode = (text) => {
21+
return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
22+
}
23+
24+
/**
25+
* URL安全的Base64编码(Base64URL)
26+
* 将 + 替换为 -,将 / 替换为 _,移除填充字符 =
27+
* @param {string} text 待编码文本
28+
* @returns {string} URL安全的Base64编码结果
29+
*/
30+
export const base64EncodeUrl = (text) => {
31+
const base64 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
32+
return base64
33+
.replace(/\+/g, '-') // 将 + 替换为 -
34+
.replace(/\//g, '_') // 将 / 替换为 _
35+
.replace(/=/g, ''); // 移除填充字符 =
36+
}
37+
38+
/**
39+
* URL安全的Base64解码(Base64URL)
40+
* 将 - 替换为 +,将 _ 替换为 /,补充填充字符 =
41+
* @param {string} text URL安全的Base64编码文本
42+
* @returns {string} 解码结果
43+
*/
44+
export const base64DecodeUrl = (text) => {
45+
// 恢复标准Base64格式
46+
let base64 = text
47+
.replace(/-/g, '+') // 将 - 替换为 +
48+
.replace(/_/g, '/'); // 将 _ 替换为 /
49+
50+
// 补充填充字符 =
51+
const padding = base64.length % 4;
52+
if (padding === 2) {
53+
base64 += '==';
54+
} else if (padding === 3) {
55+
base64 += '=';
3156
}
57+
58+
return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(base64));
59+
}
60+
61+
/**
62+
* MD5哈希
63+
* @param {string} text 待哈希文本
64+
* @returns {string} MD5哈希值
65+
*/
66+
export function md5(text) {
67+
return CryptoJS.MD5(text).toString();
3268
}
3369

3470
/**
@@ -268,6 +304,9 @@ export const throttle = (func, delay) => {
268304
export default {
269305
base64Encode,
270306
base64Decode,
307+
base64EncodeUrl,
308+
base64DecodeUrl,
309+
md5,
271310
encodeFilters,
272311
decodeFilters,
273312
buildQueryString,

dashboard/src/utils/proxyPlayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export function buildProxyPlayUrl(originalUrl, proxyAddress, headers = {}) {
5757

5858
// 替换模板字符串中的${url}和${headers}
5959
let proxyUrl = cleanProxyAddress
60-
.replace(/\$\{url\}/g, encodedUrl)
61-
.replace(/\$\{headers\}/g, encodedHeaders)
60+
.replace(/\$\{url\}/g, encodeURIComponent(encodedUrl))
61+
.replace(/\$\{headers\}/g, encodeURIComponent(encodedHeaders))
6262
.replace(/\$\{type\}/g, encodedType)
6363

6464
console.log('🔄 [代理播放] 构建代理URL:')

0 commit comments

Comments
 (0)