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'); 输出的
相关推荐
结网的兔子5 小时前
【前端开发】Web端迁移至 uni-app 及鸿蒙扩展方案对比
前端·uni-app·harmonyos
2501_915909066 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
AI多Agent协作实战派9 小时前
AI多Agent协作系统实战(二十八):顶栏统一战争——从1个页面异常到31个页面全量对齐
数据库·人工智能·uni-app
00后程序员张9 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川9 小时前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余1 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008891 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313181 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
name好难取诶1 天前
通过对云基座和离线基座的对比,帮助开发者理解如何在不同场景下选择合适的打包方案。 什么是“基座” 在 UniApp 中,基座(也称“ ...
开发语言·uni-app·php
2501_915921431 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode