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'); 输出的
相关推荐
喜崽2 小时前
uniapp消息会话界面【消息组件一左一右】-01
uni-app
一渊之隔2 小时前
uniapp蓝牙搜索连接展示蓝牙设备包含信号显示
前端·网络·uni-app·bluetooth
舟遥遥娓飘飘3 小时前
面向零基础初学者,从环境搭建到发布上线,手把手教你开发第一个微信小程序(第3章-认识项目结构)
微信小程序·小程序·notepad++
喜崽3 小时前
uniapp消息会话界面【消息滚动到底部】-02
uni-app
优睿远行4 小时前
微信小程序自定义组件开发实战:从封装到发布的全流程指南
微信小程序·小程序·notepad++
27669582924 小时前
某白山小程序限制PC端调试
python·小程序·apache·小程序逆向·某白山·限制pc调试
Greg_Zhong5 小时前
微信小程序中使用云函数调用豆包免费模型,部署云函数设置(触发器)执行每日自动生成书籍的文章赏析,完整过程
微信小程序·ai工程师·小程序中豆包模型调用·云函数配置触发器生成每日文章·微信云函数
eric*16885 小时前
微信小程序全局安全水印组件实践:支持动态更新、全局生效、自定义样式
微信小程序·小程序
Geek_Vison5 小时前
三款小程序容器技术选型对比分析——融媒新闻APP如何进行技术选型~
小程序·uni-app·app开发·finclip·小程序开发平台·跨端开发·小程序容器