| | |
| | | import * as CryptoJS from "crypto-js"; |
| | | |
| | | /** |
| | | * Base64 编码(按 UTF-8 处理) |
| | | * 注意:btoa 只在浏览器(H5)存在,App 和小程序里没有,直接调用会报 btoa is not defined。 |
| | | * 这里在非 H5 环境用纯 JS 实现,不依赖任何平台 API。 |
| | | */ |
| | | const B64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| | | |
| | | function base64Encode(str) { |
| | | const text = String(str); |
| | | // UTF-8 字节序列 |
| | | const utf8 = unescape(encodeURIComponent(text)); |
| | | let out = ""; |
| | | for (let i = 0; i < utf8.length; i += 3) { |
| | | const c1 = utf8.charCodeAt(i); |
| | | const c2 = utf8.charCodeAt(i + 1); |
| | | const c3 = utf8.charCodeAt(i + 2); |
| | | out += B64_CHARS[c1 >> 2]; |
| | | out += B64_CHARS[((c1 & 3) << 4) | (isNaN(c2) ? 0 : c2 >> 4)]; |
| | | out += isNaN(c2) ? "=" : B64_CHARS[((c2 & 15) << 2) | (isNaN(c3) ? 0 : c3 >> 6)]; |
| | | out += isNaN(c3) ? "=" : B64_CHARS[c3 & 63]; |
| | | } |
| | | return out; |
| | | } |
| | | |
| | | /** |
| | | *加密处理 |
| | | */ |
| | | export const encryption = params => { |
| | |
| | | const result = JSON.parse(JSON.stringify(data)); |
| | | if (type === "Base64") { |
| | | param.forEach(ele => { |
| | | result[ele] = btoa(result[ele]); |
| | | result[ele] = base64Encode(result[ele]); |
| | | }); |
| | | } else { |
| | | param.forEach(ele => { |