feat: 添加TextEncoder和TextDecoder的polyfill实现
添加polyfill.js文件实现TextEncoder和TextDecoder的兼容性支持 修改app.js引入polyfill 将mock数据注释标记为TODO
Showing
3 changed files
with
71 additions
and
1 deletions
| ... | @@ -7,6 +7,7 @@ | ... | @@ -7,6 +7,7 @@ |
| 7 | */ | 7 | */ |
| 8 | import { createApp } from 'vue' | 8 | import { createApp } from 'vue' |
| 9 | import { createPinia } from 'pinia' | 9 | import { createPinia } from 'pinia' |
| 10 | +import './utils/polyfill' | ||
| 10 | import './app.less' | 11 | import './app.less' |
| 11 | import { saveCurrentPagePath, needAuth, silentAuth, navigateToAuth } from '@/utils/authRedirect' | 12 | import { saveCurrentPagePath, needAuth, silentAuth, navigateToAuth } from '@/utils/authRedirect' |
| 12 | import Taro from '@tarojs/taro' | 13 | import Taro from '@tarojs/taro' | ... | ... |
| ... | @@ -35,7 +35,7 @@ const toHome = () => { | ... | @@ -35,7 +35,7 @@ const toHome = () => { |
| 35 | Taro.reLaunch({ url: '/pages/index/index' }); | 35 | Taro.reLaunch({ url: '/pages/index/index' }); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -// Mock Data as per requirement | 38 | +// TODO: Mock Data as per requirement |
| 39 | const getMockData = () => { | 39 | const getMockData = () => { |
| 40 | return [ | 40 | return [ |
| 41 | { | 41 | { | ... | ... |
src/utils/polyfill.js
0 → 100644
| 1 | + | ||
| 2 | +if (typeof TextEncoder === 'undefined') { | ||
| 3 | + class TextEncoder { | ||
| 4 | + encode(str) { | ||
| 5 | + const len = str.length; | ||
| 6 | + const res = []; | ||
| 7 | + for (let i = 0; i < len; i++) { | ||
| 8 | + let point = str.charCodeAt(i); | ||
| 9 | + if (point <= 0x007f) { | ||
| 10 | + res.push(point); | ||
| 11 | + } else if (point <= 0x07ff) { | ||
| 12 | + res.push(0xc0 | (point >>> 6)); | ||
| 13 | + res.push(0x80 | (0x3f & point)); | ||
| 14 | + } else if (point <= 0xffff) { | ||
| 15 | + res.push(0xe0 | (point >>> 12)); | ||
| 16 | + res.push(0x80 | (0x3f & (point >>> 6))); | ||
| 17 | + res.push(0x80 | (0x3f & point)); | ||
| 18 | + } else { | ||
| 19 | + point = 0x10000 + ((point - 0xd800) << 10) + (str.charCodeAt(++i) - 0xdc00); | ||
| 20 | + res.push(0xf0 | (point >>> 18)); | ||
| 21 | + res.push(0x80 | (0x3f & (point >>> 12))); | ||
| 22 | + res.push(0x80 | (0x3f & (point >>> 6))); | ||
| 23 | + res.push(0x80 | (0x3f & point)); | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + return new Uint8Array(res); | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + if (typeof globalThis !== 'undefined') { | ||
| 31 | + globalThis.TextEncoder = TextEncoder; | ||
| 32 | + } | ||
| 33 | + if (typeof global !== 'undefined') { | ||
| 34 | + global.TextEncoder = TextEncoder; | ||
| 35 | + } | ||
| 36 | + if (typeof window !== 'undefined') { | ||
| 37 | + window.TextEncoder = TextEncoder; | ||
| 38 | + } | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +if (typeof TextDecoder === 'undefined') { | ||
| 42 | + class TextDecoder { | ||
| 43 | + decode(view, options) { | ||
| 44 | + if (!view) { | ||
| 45 | + return ''; | ||
| 46 | + } | ||
| 47 | + let string = ''; | ||
| 48 | + const arr = new Uint8Array(view); | ||
| 49 | + for (let i = 0; i < arr.length; i++) { | ||
| 50 | + string += String.fromCharCode(arr[i]); | ||
| 51 | + } | ||
| 52 | + try { | ||
| 53 | + // 简单的 UTF-8 解码尝试 | ||
| 54 | + return decodeURIComponent(escape(string)); | ||
| 55 | + } catch (e) { | ||
| 56 | + return string; | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + if (typeof globalThis !== 'undefined') { | ||
| 61 | + globalThis.TextDecoder = TextDecoder; | ||
| 62 | + } | ||
| 63 | + if (typeof global !== 'undefined') { | ||
| 64 | + global.TextDecoder = TextDecoder; | ||
| 65 | + } | ||
| 66 | + if (typeof window !== 'undefined') { | ||
| 67 | + window.TextDecoder = TextDecoder; | ||
| 68 | + } | ||
| 69 | +} |
-
Please register or login to post a comment