提交 bac8468a 编写于 作者: 雪洛's avatar 雪洛

fix: 修复uni-id相关接口使用atob在不支持的平台导致报错的Bug

上级 0fbabebb
let realAtob: (b64Str: string) => string
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
const b64re =
/^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/
if (typeof atob !== 'function') {
realAtob = function (str: string) {
str = String(str).replace(/[\t\n\f\r ]+/g, '')
if (!b64re.test(str)) {
throw new Error(
"Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
)
}
// Adding the padding if missing, for semplicity
str += '=='.slice(2 - (str.length & 3))
var bitmap
var result = ''
var r1
var r2
var i = 0
for (; i < str.length; ) {
bitmap =
(b64.indexOf(str.charAt(i++)) << 18) |
(b64.indexOf(str.charAt(i++)) << 12) |
((r1 = b64.indexOf(str.charAt(i++))) << 6) |
(r2 = b64.indexOf(str.charAt(i++)))
result +=
r1 === 64
? String.fromCharCode((bitmap >> 16) & 255)
: r2 === 64
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
: String.fromCharCode(
(bitmap >> 16) & 255,
(bitmap >> 8) & 255,
bitmap & 255
)
}
return result
}
} else {
// 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
realAtob = atob
}
function b64DecodeUnicode(str: string): string {
return decodeURIComponent(
atob(str)
realAtob(str)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册