博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

企业微信注入agentconfig失败的问题

Posted on 2021-07-27 10:42  悠子快跑  阅读(2153)  评论(0编辑  收藏  举报

微信js-sdk调用一些接口,比如企业微信集成选人接口,需要注入agentconfig,官方说法是需要加载 https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js

但是这个文件里面只有agentconfig函数,可以通过控制台Object.getOwnPropertyNames(wx)查看,而jssdk调用其它内容,必须要引用https://res.wx.qq.com/open/js/jweixin-1.2.0.js,而这个js文件里没有agentconfig函数。

这两个文件有先后加载顺序,必须得先加载https://res.wx.qq.com/open/js/jweixin-1.2.0.js,等下载完成后,再加载https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js,否则wx这个对象会被覆盖掉。

完整的是两个文件合并,合并后的结果是:

[
"config",
"ready",
"error",
"checkJsApi",
"onMenuShareTimeline",
"onMenuShareAppMessage",
"onMenuShareQQ",
"onMenuShareWeibo",
"onMenuShareQZone",
"startRecord",
"stopRecord",
"onVoiceRecordEnd",
"playVoice",
"pauseVoice",
"stopVoice",
"onVoicePlayEnd",
"uploadVoice",
"downloadVoice",
"translateVoice",
"chooseImage",
"getLocation",
"previewImage",
"uploadImage",
"downloadImage",
"getLocalImgData",
"getNetworkType",
"openLocation",
"hideOptionMenu",
"showOptionMenu",
"closeWindow",
"hideMenuItems",
"showMenuItems",
"hideAllNonBaseMenuItem",
"showAllNonBaseMenuItem",
"scanQRCode",
"openAddress",
"openProductSpecificView",
"addCard",
"chooseCard",
"openCard",
"consumeAndShareCard",
"chooseWXPay",
"openEnterpriseRedPacket",
"startSearchBeacons",
"stopSearchBeacons",
"onSearchBeacons",
"openEnterpriseChat",
"agentConfig"
]

 

页面写入顺序如下:

<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript"></script>
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js" type="text/javascript"></script>

 

这样是不行的,虽然jweixin写在上面,但是文件大,可能加载在jwxwork后面,导致wx对象仍没有agentconfig函数。晚上有个说法是调用1.0.0文件,https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js,这个文件里有agentconfig函数,但是是旧版的。

综上,解决办法是手动加载js文件

参考:https://www.cnblogs.com/xiaoniaohhl/p/11254272.html

页面中js加载完全的方法

 

function loadScript( url, callback) {

    var script = document.createElement("script");

    script.type = "text/javascript";

    if (script.readyState) {

        script.onreadystatechange = function() {

            if (script.readyState == "loaded" || script.readyState == "complete") {

                script.onreadystatechange = null;

                callback();

            }

        }

    } else {

        script.onload = function() {

            callback();

        }

    }

    script.src = url;

    document.getElementsByName("head")[0].appendChild(script);

}

如何让脚本的执行顺序按照你设定的顺序执行,使用嵌套的方式:

loadScript("file1.js", function() {

    loadScript("file2.js", function() {

        loadScript("file3.js", function() {

            alert("All files are loaded");

        });

    });

});