需要在templateUrl中添加加密的动态路径变量,以便使用异步的Web-Crypto加密并返回Promise,因为我尝试从resolve调用Promise函数,因为它为此提供了support。但是无法从templateUrl
块更新resolve
。
$routeProvider.when('/portal/page/get', {
templateUrl: '/portal/long/path/page/get', // default path
/*templateUrl: function() {
const response = getSecretData();
// obviously this is not going to work as function is async
return '/portal/long/path/page/' + response + '/get';
}*/
resolve: {
urlUpdater: function($route) {
getSecretData().then(response => {
// As this is async function this line executes after setting templateUrl
// So its not updating with new urlTemplate
$route.current.templateUrl = '/portal/long/path/page/' + response + '/get';
});
}
}
});
使用Web-Crypto
获取加密值的基本方法,
async function getSecretData() {
let sKey;
let sIV = window.crypto.getRandomValues(new Uint8Array(12));
let output;
await window.crypto.subtle.generateKey({
name: "AES-GCM",
length: 256,
}, true, ["encrypt"]).then(function(key) {
sKey = key;
});
await crypto.subtle.encrypt({
name: "AES-GCM",
iv: sIV,
tagLength: 128
}, heaowInsightsAPISecretKey, "nonSecretValue")).then(function (cipherText) {
output = btoa(String.fromCharCode.apply(null, new Uint8Array(cipherText)));
});
return output;
}
在$route.reload()
中使用resolve
将无济于事,因为它将变得递归。
那么如何通过异步功能设置/更新templateUrl?