|
@@ -0,0 +1,36 @@
|
|
|
+/**
|
|
|
+ * Base64加解码类
|
|
|
+ */
|
|
|
+class Base64 {
|
|
|
+ /**
|
|
|
+ * Base64编码
|
|
|
+ * @param {string} plaintext 明文
|
|
|
+ * @returns 密文
|
|
|
+ */
|
|
|
+ encode(plaintext) {
|
|
|
+ let res = '';
|
|
|
+
|
|
|
+ // 浏览器兼容
|
|
|
+ if (typeof btoa === 'function' && typeof unescape === 'function') {
|
|
|
+ res = btoa(unescape(encodeURIComponent(plaintext)));
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Base64解码
|
|
|
+ * @param {string} cipherText 密文
|
|
|
+ * @returns 明文
|
|
|
+ */
|
|
|
+ decode(cipherText) {
|
|
|
+ let res = '';
|
|
|
+
|
|
|
+ // 浏览器兼容
|
|
|
+ if (typeof btoa === 'function' && typeof escape === 'function') {
|
|
|
+ res = decodeURIComponent(escape(atob(cipherText)));
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+}
|
|
|
+export default new Base64();
|