GitHub 上使用 JavaScript 来生成公钥私钥对的库:https://github.com/travist/jsencrypt/
原因是这个第三方库使用了 window 对象,而微信小程序的页面的脚本逻辑是在 JsCore 中运行,JsCore 是一个没有窗口对象的环境,所以不能在脚本中使用 window,也无法在脚本中操作组件。
经过了一段时间的挣扎,终于找到了解决方法:
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global.JSEncrypt = {}))); }(this, (function (exports) { 'use strict'; //--------------------- 这里开始添加--------------------- // 用来替换 navigator var navigator2 = { appName: 'Netscape', userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1' }; // 用来替换window var window2 = { ASN1: null, Base64: null, Hex: null, crypto: null, href: null }; //-------------