关于微信小程序端base64解码问题

由于atob是浏览器端的,对于微信小程序不支持,导致模拟器【开发工具】显示正常,但真机异常解析失败问题,微信小程序原有的api,官方文档中也废弃了

解决方案:

调用:

javascript 复制代码
const decodedString = base64_url_decode(res.data);
const jsonData = JSON.parse(decodedString);

weAtob 解密处理文件包 :

javascript 复制代码
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
exports.weBtoa = function (string) {
  string = String(string);
  var bitmap, a, b, c, result = "", i = 0, rest = string.length % 3;
  for (; i < string.length;) {
    if ((a = string.charCodeAt(i++)) > 255 ||
      (b = string.charCodeAt(i++)) > 255 ||
      (c = string.charCodeAt(i++)) > 255)
      throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
    bitmap = (a << 16) | (b << 8) | c;
    result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63) +
      b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
  }
  return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
};
exports.weAtob = function (string) {
  string = String(string).replace(/[\t\n\f\r ]+/g, "");
  if (!b64re.test(string))
    throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
  string += "==".slice(2 - (string.length & 3));
  var bitmap, result = "", r1, r2, i = 0;
  for (; i < string.length;) {
    bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12 |
      (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
    result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255) :
      r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255) :
        String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
  }
  return result;
};
function b64DecodeUnicode(str) {
  return decodeURIComponent(exports.weAtob(str).replace(/(.)/g, function (p) {
    var code = p.charCodeAt(0).toString(16).toUpperCase();
    if (code.length < 2) {
      code = "0" + code;
    }
    return "%" + code;
  }));
}
exports.base64_url_decode = function (str) {
  var output = str.replace(/-/g, "+").replace(/_/g, "/");
  switch (output.length % 4) {
    case 0:
      break;
    case 2:
      output += "==";
      break;
    case 3:
      output += "=";
      break;
    default:
      throw "Illegal base64url string!";
  }
  try {
    return b64DecodeUnicode(output);
  }
  catch (err) {
    return exports.weAtob(output);
  }
}
function weappJwtDecode(token, options) {
  if (typeof token !== "string") {
    throw ("Invalid token specified");
  }
  options = options || {};
  var pos = options.header === true ? 0 : 1;
  try {
    return JSON.parse(base64_url_decode(token.split(".")[pos]));
  }
  catch (e) {
    throw ("Invalid token specified: " + e.message);
  }
}
exports.default = weappJwtDecode;
相关推荐
weixin_177297220695 小时前
旧物二手回收小程序:引领绿色消费,开启时尚生活新方式
小程序·盲盒
2501_916007476 小时前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
韩立学长7 小时前
【开题答辩实录分享】以《植物病虫害在线答疑小程序的设计与实现》为例进行答辩实录分享
spring boot·小程序·vue
好想早点睡.8 小时前
vue2+UniApp微信小程序集成高德地图
微信小程序·小程序·uni-app
李慕婉学姐9 小时前
【开题答辩过程】以《基于微信小程序的线上讲座管理系统》为例,不会开题答辩的可以进来看看
javascript·mysql·微信小程序
2501_9159184110 小时前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
Mr.Aholic11 小时前
分享几个开源的系统,包括小程序、商城系统、二手交易等常见的系统、很容易进行二次开发 【可以参考学习】
微信小程序·小程序·毕业设计·课程设计
2501_9159090612 小时前
HTTPS 错误排查实战,从握手到应用层的工程化流程
网络协议·http·ios·小程序·https·uni-app·iphone
JIngJaneIL17 小时前
口腔健康系统|口腔医疗|基于java和小程序的口腔健康系统小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·口腔医疗小程序
小光学长1 天前
基于微信小程序的背单词系统x1o5sz72(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·微信小程序·小程序