我有一个问题。我想到了这个逻辑:我得到了一个包含条件**if** x = 0 load **index.html** else load **index1.html**
的JavaScript文件。现在,index.html和index1.html是相同的,并且都加载相同的JavaScript文件,其中包含一个函数,对于条件x = 0
将用户带到应用程序,但在x = 1
时需要限制某些操作。我只是想在决定是采用这种方法还是完全改变我需要做的事情的逻辑之前知道它是否可能。还有另一种方法,创建两个版本的JavaScript文件,但它将代表大量重复的文件,因为这个注释和未注释的函数存在于应用程序的多个位置,并且由于它是一个MVC项目,我认为,复制的不仅仅是包含函数本身的文件。
这个想法是,我将提供一个具有两个计划的应用程序,一个免费计划和一个PRO计划。它们之间的独特区别是对某些功能的限制。所以我有这个功能:
if(userActive.isPro){
var payActive = JSON.parse(localStorage.getItem("payActive"));
if(payActive){
if(parseInt(payActive.status) === 1){
window.location.href= '../app/index.html';
}
}
else{
window.location.href= '../app/index1.html';
}
}
这就是每个计划与众不同的功能。
function openAddPopup() {
//check first if user is PRO Copy
const userStatus = localStorage.getItem("userKind");
const user = userStatus;
if (user) {
app.router.load('contactEdit', { 'isFavorite': state.isFavorite });
} else {
alert("You can only add 3 elements on the free version");
}
}
对于PRO计划,我需要执行应用程序的完整版本,对于免费计划,则需要执行受限版本。
那么,是否可以将JavaScript函数附加到索引.html和index1.html在免费计划的情况下,执行第二个,但在PRO 计划的情况下,执行第一个?
只要我今天早上醒来,脑海中有一个简单的解决方案,并希望与社区分享。
在index.html
中,我把这个:
userStatus = 'PRO';
localStorage.setItem( 'userKind', userStatus);
因此,在index1.html
中:
const userStatus = localStorage.getItem("userKind");
const user = userStatus;
if (user) {
localStorage.removeItem("userKind");
}
然后在app.html
中:
//check first if user is PRO
const userStatus = localStorage.getItem("userKind");
const user = userStatus;
if (user) {
document.body.append("Pro version")
} else {
alert("Solo se pueden añadir 3 elementos en la versión Éstandar");
}
它是如何工作的?当身份验证 API 将用户发送到应用程序时,如果是 PRO 用户,API会将他/她重定向到index.html
,该localStorage
存储具有**PRO**
值的userKind
变量,但如果它是标准用户,则API会将他/她重定向到index1.html
,该localStorage
中删除userKind
变量。这样就可以确保如果用户是PRO失去此条件,他/她只能访问标准版本。
这是一个工作示例:
索引.html
运行index.html
然后index1.html
,因为您将充当身份验证 API。
然后评论它是否正常或必须改进。