|
2 | 2 | * API工具函数 |
3 | 3 | * 提供常用的数据处理、验证和转换功能 |
4 | 4 | */ |
5 | | - |
| 5 | +import CryptoJS from 'crypto-js' |
6 | 6 | /** |
7 | | - * Base64编码 |
8 | | - * @param {string} str - 需要编码的字符串 |
| 7 | + * Base64编码(使用CryptoJS) |
| 8 | + * @param {string} text 待编码文本 |
9 | 9 | * @returns {string} Base64编码结果 |
10 | 10 | */ |
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)); |
18 | 13 | } |
19 | 14 |
|
20 | 15 | /** |
21 | | - * Base64解码 |
22 | | - * @param {string} str - 需要解码的Base64字符串 |
| 16 | + * Base64解码(使用CryptoJS) |
| 17 | + * @param {string} text Base64编码文本 |
23 | 18 | * @returns {string} 解码结果 |
24 | 19 | */ |
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 += '='; |
31 | 56 | } |
| 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(); |
32 | 68 | } |
33 | 69 |
|
34 | 70 | /** |
@@ -268,6 +304,9 @@ export const throttle = (func, delay) => { |
268 | 304 | export default { |
269 | 305 | base64Encode, |
270 | 306 | base64Decode, |
| 307 | + base64EncodeUrl, |
| 308 | + base64DecodeUrl, |
| 309 | + md5, |
271 | 310 | encodeFilters, |
272 | 311 | decodeFilters, |
273 | 312 | buildQueryString, |
|
0 commit comments