使用Crypto-Js的代码如下

define([../crypto-js.js], function(cryptoJs)) {
     //....
    const navNewPageWithJson = (json) => {
       let words = cryptoJs.enc.Utf8.parse(JSON.stringify(json)); //convert json to WordArray objects
       let base64Str = cryptoJs.enc.Base64.stringify(words); //convert WordArray obj into base64 string
       window.open("/app/accounting/transactions/salesord.nl?init=' + base64Str + '&entity=' + entity.value);  //open the url in a new window with some extra params
    }
    
}

但是使用过程中发现crypto-js暂时不支持URL Safe的Base64,如果得到的Base64中含有加号,在新窗口中拿到的值加号就会变成空格,导致解码出错。使用js-base64解决了这个问题,代码如下。

define([../base64.min.js], function(Base64)) {
     //....
    const navNewPageWithJson = (json) => {
        Base64.extendString(); // extend String.prototype
        let base64Str = JSON.stringify(json).toBase64URL() //convert json string into base64
       window.open("/app/accounting/transactions/salesord.nl?init=' + base64Str + '&entity=' + entity.value);  //open the url in a new window with some extra params
    }
    
}

以此成功解决了Base64中plus sign在url传递中丢失导致解码失败。

Last modified: 24/04/2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.