18、base64前端加解密方法

1、添加方法js方法

const Base64 = {
    //加密
    encode(str) {
        if (str===undefined || str === "" || str === null) {
            return str;
        }
        return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
            function toSolidBytes(match, p1) {
                return String.fromCharCode('0x' + p1);
            }));
    },
    //解密
    decode(str) {
        if (str===undefined || str === "" || str === null) {
            return str;
        }
        // Going backwards: from bytestream, to percent-encoding, to original string.
        return decodeURIComponent(atob(str).split('').map(function (c) {
            return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(''));
    }
}
export default Base64

2、设为全局变量

Vue.prototype.$Base64 = base64;
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐