SAP UI5/Fiori 在另一个选项卡中检查同一 Web 应用程序的活动会话

SAP UI5/Fiori Check active session of the same web application in another tab

提问人:sal 提问时间:11/15/2023 更新时间:11/15/2023 访问量:21

问:

我正在 Neo 环境中开发一个 Sap Fiori 应用程序,用于填写问卷。 问题是,如果用户打开同一应用程序的 2 个选项卡,则答案会错误地存储在问卷数据库中,以及最终结果的计算。 我想要的是,如果用户在同一浏览器的另一个选项卡中打开同一页面(如果可能的话,甚至在另一个浏览器上),以检查是否已经存在活动会话并中断新会话的执行。

考虑到该应用还调用了部署在 Cloud Foundry 上的 nodejs 服务,并且此 Web 应用(部署在 SAP BTP 上)的打开是通过 SuccessFactors 完成的: - 有没有办法控制在 JavaScript 中打开新会话?(我更喜欢的解决方案) -如果不是,有什么选择?

提前感谢您的回答。

我尝试检查cookie,其他打开的选项卡的URL,尝试检查历史记录,但没有任何具体结果,它们似乎仍然不是好的解决方案。

sapui5 会话 cookie sap-fiori

评论


答:

0赞 fabiopagoti 11/15/2023 #1

任何跨浏览器工作的解决方案都将依赖于在此锁定系统的后端实现的东西。您需要在调查问卷的开头和结尾检查后端以做出相应的行为。

如果这不是一个选项,我建议使用浏览器的本地存储作为此锁定存储库。您可以为浏览器窗口指定一个随机名称,并将一些元数据与它一起存储。在这种情况下,在其他浏览器或专用选项卡中打开应用将绕过锁定。

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/util/Storage",
    "sap/m/MessageBox"
], (Controller, Storage, MessageBox) => {
    "use strict";

    return Controller.extend("yourapp.controller.App", {

        onInit: function() {

            // Give the current tab a random name
            window.name = +((Math.random() * 10).toFixed(2))

            // Initialize local storage with app namesapce
            var oMyStorage = new Storage(Storage.Type.local, "app");

            // Read current local storage 
            var sCurrentData = oMyStorage.get("activeTab") || "{}";
            var oCurrentData = JSON.parse(sCurrentData)

            // Other validations can be done like checking how recent the other window was opened or
            // add an option to unlock/refresh the local storage
            if (sCurrentData && window.name != oCurrentData.activeTab) {
                MessageBox.error(`There is an active tab opened at ${oCurrentData.openedAt}`)
                return;
            }

            // in case no data is found, add data on local storage
            var oStorageData = {
                activeTab: window.name,
                openedAt: new Date()
            }
            oMyStorage.put("activeTab", JSON.stringify(oStorageData));


        },

    });

});