首页 > 解决方案 > Office 无法加载仅适用于 IOS 的 Outlook Web 插件

问题描述

在我的插件中,我使用 Outlook 对话框 API 实现了登录功能。它加载带有返回 url 的凭据表单(不在我的控制范围内)。登录响应正确地出现在我的节点服务器上,我看到用户已通过身份验证。在服务器上,准备了一个 ejs 文件并将其发送给客户端。回到客户端我想执行 messageParent,所以我可以关闭对话框。这适用于浏览器(包括 safari)、android、桌面等,它适用于 Outlook for MAC 但不适用于 iOS。示例代码如下:

在nodejs中:

app.post("/auth/:token", (req, res) => {
    console.log('auth return url logic', req.params.token, JSON.stringify(req.body));

    let send_response = function(res, user) {
        try {
            return res.render(
                'send_to_parent.ejs',
                {user : user},
                function (e, r) {
                    try {
                        res.end(r);
                    }
                    catch(e) {}
                } 
            );
        }
        catch (err) {
             console.log(1312312321, err);
        }
    };

    send_response(res, {});
});

在 send_to_parent.ejs

<!DOCTYPE html>
<html>
<head>
     <title>#Sign in</title>

     <script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.debug.js"></script>
     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script type="text/javascript">
         console.log('in ejs');

         function messageParent() {
              console.log('message parent');
              Office.context.ui.messageParent(true);
         }

         function openClick() {
              window.setTimeout(messageParent, 100);
         }

         Office.initialize = function (reason) {
             console.log('office initialize');
             $(document)
                .ready(function () {
                     console.log('jquery ready');
                     openClick();
                 });
          };
     </script>
 </head>

任何想法出了什么问题或如何诊断?

=====EDIT===== 好吧,为了调试,我做了一些代码更改和替换。现在我在 ejs 文件中的代码如下所示:

<head>
   <title>#Sign in</title>

   <script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.debug.js"></script>
   <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>
    <h1 style="text-align: center">Authorization Successful</h1>
    <div id="log">test</div>

    <script type="text/javascript">
       document.getElementById("log").innerText += "before initialize";
       Office.initialize = function () {
          document.getElementById("log").innerText += "initialized";
          $(document)
              .ready(function () {
                  document.getElementById("log").innerText += "jquery";

                  $("#closeButton").click(function () {
                    document.getElementById("log").innerText += "close clicked";
                    Office.context.ui.messageParent(true);
                });

                Office.context.ui.messageParent(true);
            });
      };

   </script>
</body>

成功登录后,我的日志 div 仅使用“初始化之前”进行更新,这意味着 Office.initialize 永远不会发生。我还尝试先调用 jquery.ready,这证明 jquery 工作正常。出于某种原因,Office 不想初始化。为什么?

====第二次编辑==== 深入研究这个问题,我做了一些改变。

function log(text) {
    document.getElementById("log").innerText += text;
}

function messageParent() {
    log('message parent');
    try {
        if ( !Office ) {
            log('no_office');
        }
        else if (!Office.context ) {
            log('no_context')
        }
        else if( !Office.context.ui) {
            log('no_ui');
        }
        else if (!Office.context.ui.messageParent) {
            log('no_message');
        }

        Office.context.ui.messageParent(true);
    }
    catch(e) {
        log("error");
        log(e);
    }
}

log("here");
Office.onReady ( function (reason) {
    log('office initialize');
    $(document)
        .ready(function () {
            log('jquery ready');
            // messageParent();

            $("#closeButton").click(function () {
                log(" close_clicked_out_of_office ");
                debugger
                messageParent();
            });
        });
});    

现在我终于有了 Outlook。我也有 Outlook.context ,但没有 ui。所以我不能执行messageParent。

标签: iosoutlook-web-addins

解决方案


推荐阅读