uniapp 微信小程序使用TextEncoder,arrayBufferToBase64

uniapp 微信小程序使用TextEncoder,arrayBufferToBase64

有一些小程序会在真机报错TextEncoder main.js 第一行就加,不要放在后面

js 复制代码
// main.js 第一行就加,不要放在后面
if (typeof TextEncoder === 'undefined') {
  global.TextEncoder = class TextEncoder {
    /**
     * 将字符串转为UTF-8编码的Uint8Array
     * 兼容ASCII、中文、emoji等所有Unicode字符
     */
    encode(str) {
      const arr = [];
      for (let i = 0; i < str.length; i++) {
        const code = str.charCodeAt(i);
        // 处理单字节ASCII
        if (code < 0x80) {
          arr.push(code);
        } 
        // 处理双字节字符
        else if (code < 0x800) {
          arr.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
        } 
        // 处理三字节/emoji等字符
        else if (code < 0xd800 || code >= 0xe000) {
          arr.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
        } 
        // 处理四字节代理对字符
        else {
          i++;
          const nextCode = str.charCodeAt(i);
          const fullCode = 0x10000 + (((code & 0x3ff) << 10) | (nextCode & 0x3ff));
          arr.push(
            0xf0 | (fullCode >> 18),
            0x80 | ((fullCode >> 12) & 0x3f),
            0x80 | ((fullCode >> 6) & 0x3f),
            0x80 | (fullCode & 0x3f)
          );
        }
      }
      return new Uint8Array(arr);
    }
  };
}

输出base64 arrayBufferToBase64

js 复制代码
 const encoder = new TextEncoder();
 str = encoder.encode(str).buffer;
 let output = uni.arrayBufferToBase64(str)
 console.log(output,'output'); 输出的
相关推荐
spmcor2 天前
微信小程序 setStorageSync 踩坑实录:别让"顺手一存"变成"隐形炸弹"
微信小程序
用户4324281061142 天前
小程序埋点设计规范:如何设计可扩展的数据采集体系
微信小程序
m0_526119403 天前
iconfont我修改好颜色,但是在小程序项目是黑色的
小程序
用户6990304848753 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_3 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
2601_956743683 天前
2026 上海小程序开发甄选:源码、云函数、跨端兼容技术评判
小程序·开发经验·上海
IT_张三3 天前
CSDN-项目分享-暑期备考小程序
小程序
IsJunJianXin3 天前
pdd小程序 cdp 保存响应体
linux·服务器·小程序·pdd小程序·拼多多响应体解密·小程序cdp·拼多多rpc取响应体
Geek_Vison3 天前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
weikecms3 天前
聚合返利CPS小程序快速搭建教程
人工智能·微信·小程序