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'); 输出的
相关推荐
kyh10033811206 小时前
Cocos Creator 《打螺丝消除游戏》源码+实现
游戏·微信小程序·小程序·打螺丝小游戏源码·微笑小游戏源码
烂不烂问厨房13 小时前
支付宝小程序camera录制视频并上传注意事项
小程序·音视频
PeanutSplsh13 小时前
wx.setStorage 存的数据,没你以为的那么安全
微信小程序
黄同学real15 小时前
uni-app 真机调试:手动代理环境下访问内网 API 的解决方案
uni-app
我是伪码农16 小时前
小程序125-150
小程序
帅次16 小时前
讯飞与腾讯云:Android 实时语音识别服务对比选择
android·ios·微信小程序·小程序·android studio·android runtime
Hoshizola16 小时前
uniapp与蓝牙设备连接详细步骤
前端·uni-app
优雅格子衫16 小时前
uniapp 拍照相册选取后超级好用的裁剪组件,增加水印完全自定义
开发语言·前端·javascript·uni-app·vue
路光.17 小时前
uniapp中解决webview在app中调用,有过渡空白问题,增加过渡动效
uni-app·vue·app·uniapp
Chengbei1117 小时前
小程序 AI 渗透新工具MCP!打通调试与安全检测、网络抓包、接口分析、越权检测一站式实现
人工智能·安全·web安全·搜索引擎·网络安全·小程序·系统安全