创建js-base64.js文件

(function(global) {'use strict';var global = global || {};var _Base64 = global.Base64;var version = "1.0.0";var buffer;var b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var b64tab = function(bin) {var t = {};for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;return t;}(b64chars);var fromCharCode = String.fromCharCode;// encoder stuffvar cb_utob = function(c) {if (c.length < 2) {var cc = c.charCodeAt(0);return cc < 0x80 ? c :cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +fromCharCode(0x80 | (cc & 0x3f))) :(fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +fromCharCode(0x80 | (cc & 0x3f)));} else {var cc = 0x10000 +(c.charCodeAt(0) - 0xD800) * 0x400 +(c.charCodeAt(1) - 0xDC00);return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +fromCharCode(0x80 | (cc & 0x3f)));}};var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob = function(u) {return u.replace(re_utob, cb_utob);};var cb_encode = function(ccc) {var padlen = [0, 2, 1][ccc.length % 3],ord = ccc.charCodeAt(0) << 16 |((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8) |((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),chars = [b64chars.charAt(ord >>> 18),b64chars.charAt((ord >>> 12) & 63),padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),padlen >= 1 ? '=' : b64chars.charAt(ord & 63)];return chars.join('');};var btoa = global.btoa ? function(b) {return global.btoa(b);} : function(b) {return b.replace(/[\s\S]{1,3}/g, cb_encode);};var _encode = buffer ? function(u) {return (u.constructor === buffer.constructor ? u : new buffer(u)).toString('base64')} :function(u) { return btoa(utob(u)) };var encode = function(u, urisafe) {return !urisafe ?_encode(String(u)) :_encode(String(u)).replace(/[+\/]/g, function(m0) {return m0 == '+' ? '-' : '_';}).replace(/=/g, '');};var encodeURI = function(u) { return encode(u, true) };// decoder stuffvar re_btou = new RegExp(['[\xC0-\xDF][\x80-\xBF]','[\xE0-\xEF][\x80-\xBF]{2}','[\xF0-\xF7][\x80-\xBF]{3}'].join('|'), 'g');var cb_btou = function(cccc) {switch (cccc.length) {case 4:var cp = ((0x07 & cccc.charCodeAt(0)) << 18) |((0x3f & cccc.charCodeAt(1)) << 12) |((0x3f & cccc.charCodeAt(2)) << 6) |(0x3f & cccc.charCodeAt(3)),offset = cp - 0x10000;return (fromCharCode((offset >>> 10) + 0xD800) +fromCharCode((offset & 0x3FF) + 0xDC00));case 3:return fromCharCode(((0x0f & cccc.charCodeAt(0)) << 12) |((0x3f & cccc.charCodeAt(1)) << 6) |(0x3f & cccc.charCodeAt(2)));default:return fromCharCode(((0x1f & cccc.charCodeAt(0)) << 6) |(0x3f & cccc.charCodeAt(1)));}};var btou = function(b) {return b.replace(re_btou, cb_btou);};var cb_decode = function(cccc) {var len = cccc.length,padlen = len % 4,n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) |(len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) |(len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) |(len > 3 ? b64tab[cccc.charAt(3)] : 0),chars = [fromCharCode(n >>> 16),fromCharCode((n >>> 8) & 0xff),fromCharCode(n & 0xff)];chars.length -= [0, 0, 2, 1][padlen];return chars.join('');};var atob = global.atob ? function(a) {return global.atob(a);} : function(a) {return a.replace(/[\s\S]{1,4}/g, cb_decode);};var _decode = buffer ? function(a) {return (a.constructor === buffer.constructor ?a : new buffer(a, 'base64')).toString();} :function(a) { return btou(atob(a)) };var decode = function(a) {return _decode(String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' }).replace(/[^A-Za-z0-9\+\/]/g, ''));};var noConflict = function() {var Base64 = global.Base64;global.Base64 = _Base64;return Base64;};// export Base64global.Base64 = {VERSION: version,atob: atob,btoa: btoa,fromBase64: decode,toBase64: encode,utob: utob,encode: encode,encodeURI: encodeURI,btou: btou,decode: decode,noConflict: noConflict};if (typeof Object.defineProperty === 'function') {var noEnum = function(v) {return { value: v, enumerable: false, writable: true, configurable: true };};global.Base64.extendString = function() {Object.defineProperty(String.prototype, 'fromBase64', noEnum(function() {return decode(this)}));Object.defineProperty(String.prototype, 'toBase64', noEnum(function(urisafe) {return encode(this, urisafe)}));Object.defineProperty(String.prototype, 'toBase64URI', noEnum(function() {return encode(this, true)}));};}Ïmodule.exports = global.Base64;
})(this);

创建 base64.modified.js

(function(global) {'use strict';var global = global || {};var _Base64 = global.Base64;var version = "1.0.0";var buffer;var b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var b64tab = function(bin) {var t = {};for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;return t;}(b64chars);var fromCharCode = String.fromCharCode;var cb_utob = function(c) {if (c.length < 2) {var cc = c.charCodeAt(0);return cc < 0x80 ? c :cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +fromCharCode(0x80 | (cc & 0x3f))) :(fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +fromCharCode(0x80 | (cc & 0x3f)));} else {var cc = 0x10000 +(c.charCodeAt(0) - 0xD800) * 0x400 +(c.charCodeAt(1) - 0xDC00);return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +fromCharCode(0x80 | (cc & 0x3f)));}};var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob = function(u) {return u.replace(re_utob, cb_utob);};var cb_encode = function(ccc) {var padlen = [0, 2, 1][ccc.length % 3],ord = ccc.charCodeAt(0) << 16 |((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8) |((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),chars = [b64chars.charAt(ord >>> 18),b64chars.charAt((ord >>> 12) & 63),padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),padlen >= 1 ? '=' : b64chars.charAt(ord & 63)];return chars.join('');};var btoa = global.btoa ? function(b) {return global.btoa(b);} : function(b) {return b.replace(/[\s\S]{1,3}/g, cb_encode);};var _encode = buffer ? function(u) {return (u.constructor === buffer.constructor ? u : new buffer(u)).toString('base64')} :function(u) { return btoa(utob(u)) };var encode = function(u, urisafe) {return !urisafe ?_encode(String(u)) :_encode(String(u)).replace(/[+\/]/g, function(m0) {return m0 == '+' ? '-' : '_';}).replace(/=/g, '');};var encodeURI = function(u) { return encode(u, true) };var re_btou = new RegExp(['[\xC0-\xDF][\x80-\xBF]','[\xE0-\xEF][\x80-\xBF]{2}','[\xF0-\xF7][\x80-\xBF]{3}'].join('|'), 'g');var cb_btou = function(cccc) {switch (cccc.length) {case 4:var cp = ((0x07 & cccc.charCodeAt(0)) << 18) |((0x3f & cccc.charCodeAt(1)) << 12) |((0x3f & cccc.charCodeAt(2)) << 6) |(0x3f & cccc.charCodeAt(3)),offset = cp - 0x10000;return (fromCharCode((offset >>> 10) + 0xD800) +fromCharCode((offset & 0x3FF) + 0xDC00));case 3:return fromCharCode(((0x0f & cccc.charCodeAt(0)) << 12) |((0x3f & cccc.charCodeAt(1)) << 6) |(0x3f & cccc.charCodeAt(2)));default:return fromCharCode(((0x1f & cccc.charCodeAt(0)) << 6) |(0x3f & cccc.charCodeAt(1)));}};var btou = function(b) {return b.replace(re_btou, cb_btou);};var cb_decode = function(cccc) {var len = cccc.length,padlen = len % 4,n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) |(len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) |(len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) |(len > 3 ? b64tab[cccc.charAt(3)] : 0),chars = [fromCharCode(n >>> 16),fromCharCode((n >>> 8) & 0xff),fromCharCode(n & 0xff)];chars.length -= [0, 0, 2, 1][padlen];return chars.join('');};var atob = global.atob ? function(a) {return global.atob(a);} : function(a) {return a.replace(/[\s\S]{1,4}/g, cb_decode);};var _decode = buffer ? function(a) {return (a.constructor === buffer.constructor ?a : new buffer(a, 'base64')).toString();} :function(a) { return btou(atob(a)) };var decode = function(a) {return _decode(String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' }).replace(/[^A-Za-z0-9\+\/]/g, ''));};var noConflict = function() {var Base64 = global.Base64;global.Base64 = _Base64;return Base64;};global.Base64 = {VERSION: version,atob: atob,btoa: btoa,fromBase64: decode,toBase64: encode,utob: utob,encode: encode,encodeURI: encodeURI,btou: btou,decode: decode,noConflict: noConflict};// if ES5 is available, make Base64.extendString() availableif (typeof Object.defineProperty === 'function') {var noEnum = function(v) {return { value: v, enumerable: false, writable: true, configurable: true };};global.Base64.extendString = function() {Object.defineProperty(String.prototype, 'fromBase64', noEnum(function() {return decode(this)}));Object.defineProperty(String.prototype, 'toBase64', noEnum(function(urisafe) {return encode(this, urisafe)}));Object.defineProperty(String.prototype, 'toBase64URI', noEnum(function() {return encode(this, true)}));};}module.exports = global.Base64;
})(this);

使用方法

var Base64 = require('../../../../utils/base64.modified.js');
console.log(Base64.encode('微信小程序'));
console.log(Base64.decode('5b6u5L+h5bCP56iL5bqP'));

如有侵权,请联系我删除!!!

微信小程序中使用Base64编码解码相关推荐

  1. 【微信小程序】引入Base64 图标库

    场景描述 小程序icon图标需要使用base64编码后的icon,不使用png.jpg等格式的图片 解决方法 1.进入阿里图标库 https://www.iconfont.cn/ 2. 创建一个项目 ...

  2. 微信小程序中引入ttf字体

    微信小程序中引入ttf字体 解决常规引入ttf不生效问题 第一步:将ttf字体转为base64(转码网址:https://transfonter.org) 第二步:在static文件夹下新增font文 ...

  3. 微信小程序中使用SVG

    最近在作一个微信小程序的项目,需要使用svg引入矢量图标,总结一下svg在小程序中的使用. SVG 简介 什么是SVG SVG是"Scalable Vector Graphics" ...

  4. 微信小程序中使用wxss加载图片并实现动画

    微信小程序中使用wxss加载图片并实现动画 记录微信小程序中使用wxss加载图片并实现动画的方式,最终实现loading效果. 代码 .weui-loading { margin: 0 5px; wi ...

  5. 在微信小程序中使用字体图标

    方法一:转化为base64 第一步:在阿里巴巴矢量图标库下载需要的图标 QQ截图20180316191514.png 点击下载代码,将下载后的download.zip解压 第二步:进入https:// ...

  6. 微信小程序中通过Basic Auth传递token

    npm导入用于Base64加密的工具包js-base64 // package.js "dependencies": {"js-base64": "^ ...

  7. 微信小程序--图片转base64

    前言 在很多时候,我们需要图片的base64码,一般利用自己封装的function,但是微信小程序中可以利用 wx.getFileSystemManager().readFile 代码 //转base ...

  8. 微信小程序中使用阿里巴巴图标库

    这里写自定义目录标题 微信小程序中使用阿里巴巴图标库 第一步 第二步 第三步 微信小程序中使用阿里巴巴图标库 第一步 阿里巴巴图标正常加入自己的项目,并下载到本地: 注意:这里的选择 ![在这里插入图 ...

  9. 微信小程序中进行图片压缩

    微信小程序中进行图片压缩 问题: 一般情况下,小程序在进行拍照识别的时候,上传图片的大小会使小程序出现冗余,这个时候,为了减少小程序的冗余 并且使上传的图片可以分辨出该图片中的内容是那些内容,我们就需 ...

最新文章

  1. POJ 3041 Asteroids 二分图匹配
  2. MySQL高级 - 锁 - InnoDB行锁 - 介绍及背景知识
  3. 深度学习(三十六)——R-FCN, FPN, RetinaNet, CornerNet
  4. oracle 导入excel时间格式,将.xls或者.excel格式的数据导入到Oracle中
  5. 【渝粤教育】国家开放大学2019年春季 0320-22T学前教育学 参考试题
  6. python面向对象-1方法、构造函数
  7. 泥塑课c语言,【C】泥塑课(From http://www.jisuanke.com/)
  8. python切换虚拟环境和全局_为什么python虚拟环境启动后依然使用全局的python和pip...
  9. Windows学习总结(18)——Windows运行命令与Win键+快捷键再总结
  10. 安装axios时报错
  11. UltraEdit 注册机使用激活方法 更新:暴力破解
  12. Android多媒体功能开发(7)——使用SoundPool类播放音频
  13. JavaScript里的语句用分号结尾是个选项吗
  14. 【网络】路由器集成锐捷认证
  15. 【Linux】磁盘类型设备驱动介绍
  16. 电动汽车的新问题,保险公司赔不起,车主养不起
  17. 数据结构:八大常见数据结构
  18. 吴恩达的2022年终盘点:生成式AI、ViT、大模型
  19. Mysql同步数据到Elasticsearch(实时Canal)
  20. 2022-2028全球及中国触摸屏人机界面(HMI)行业研究及十四五规划分析报告

热门文章

  1. 微信摇一摇php,微信“摇一摇”功能是怎么实现的?
  2. 图像对抗学习笔记:复现DPatch
  3. 禾瑞亚科触摸屏驱动程序移植过程与遇到的问题--egalax_i2c
  4. uIP学习的参考资料
  5. Programming Exercise5:Regularized Linera Regression and Bias v.s Variance
  6. 示波器测量汽车进气压力传感器信号及波形分析
  7. C语言strchr()函数以及strstr()函数的实现
  8. vue中前端实现pdf预览(含vue-pdf插件用法)
  9. Linux如何复制文件和文件夹到另一个目录
  10. Homebrew快速安装教程