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'); 输出的
相关推荐
梦曦i1 小时前
uni-router新推useUniEventChannel,彻底解决页面通信难题
前端·uni-app
宠友信息1 小时前
内容社区源码多端数据协议与 Spring Boot 状态流转实现思路
java·spring boot·websocket·uni-app
PedroQue994 小时前
uni-router新推useUniEventChannel,彻底解决页面通信难题
前端·uni-app
码农客栈14 小时前
小程序学习(二十六)之“订单详情”
小程序
weixin_4400583116 小时前
2026知识付费小程序:年费全包零抽成,录播题库全功能覆盖
小程序·apache·小程序开发
小羊Yveesss18 小时前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾18 小时前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
HackTwoHub20 小时前
ARL灯塔重构版:支持APP/小程序/WEB资产同步扫描
人工智能·安全·web安全·网络安全·小程序·重构·自动化
投票竞赛1 天前
避雷按单场活动扣费平台,2026 在线投票测评不限频次新建投票评选活动
小程序
2501_915909061 天前
IPA 深度混淆是什么意思?从混淆强度到实际效果的解读
android·ios·小程序·https·uni-app·iphone·webview