企业微信开发经验合集

1.userId=null

如果企业id、应用id等都配置正确,
如果你是java项目
用的RestTemplate

替换为HttpClient!

RestTemplate真坑,估计是urlconnection问题

2.企业自建应用默认打开系统浏览器

1.引入企业微信jssdk
2.应用配置回调域名LOGIN_URL
3.验证完用户信息后,跳转到jssdk嵌入页并在路径上拼写token
4.截取路径token在下方url中拼写自建应用地址,js自动打开系统默认浏览器

整体流程:

  1. 用户访问企业微信应用 企业微信打开应用首页(在企业微信管理平台进行配置的地址)
  2. 打开应用首页的同时拼接code,建议直接拼接https://open.weixin.qq.com/connect/oauth2/authorize微信认证地址
  3. 根据配置的redirect_uri参数,跳转到目标地址URL&code=XXXX 根据code请求企业微信解析该用户
  4. 解析成功,后端重定向跳转到嵌入企业微信的jssdk地址,这里页面可以写上即将打开默认浏览器,并拼接应用系统token
  5. sdk.openDefaultBrowser方法执行打开系统默认浏览器并打开配置的url,然后截取地址栏上的token=,完成应用系统认证。
    如果在openDefaultBrowser中直接拼接认证,该地址将不是在企业微信浏览器中打开,认证地址回提示请在微信中打开。
<!DOCTYPE html>
<html>
<head>
    <title>跳转等待页面</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
        }

        .message {
            font-size: 24px;
            margin-bottom: 20px;
        }

        .spinner {
            width: 60px;
            height: 60px;
            border: 6px solid #333;
            border-top-color: transparent;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            100% {
                transform: rotate(360deg);
            }
        }
    </style>
</head>
<body>
    <h1 class="message">即将在系统浏览器打开XXXXX</h1>
    <div class="spinner"></div>
</body>
</html>
<script>
	function getJumpUrl(url){
		return url.split('jumpUrl=')[1];
	}
	
    console.log(getJumpUrl(window.location.href));
    fetch('http://zxsj.fdine.net/yhy/api/v1/openBrow')
        .then(response => response.json())
        .then(data => {
            wx.config({
                beta: true,// 调用wx.invoke形式的接口值时,该值必须为true。
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: data.appId, // 必填,交建通的cropID
                timestamp: data.timestamp, // 必填,生成签名的时间戳
                nonceStr: data.randomStr, // 必填,生成签名的随机串
                signature: data.sign,// 必填,签名,见附录1
                jsApiList: ['openUrl','openDefaultBrowser'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });
            console.log('wx.config配置完成')
            wx.ready(function () {
		console.log('wx.ready执行')
                wx.invoke('openUrl', {
                    "type": 1, //0或不填表示使用内部浏览器新窗口打开,1表示用系统浏览器打开
                    "url": getJumpUrl(window.location.href) //url地址
                }, function (res) {
			console.log(res)
                    if (res.err_msg != "openUrl:ok") {
                        //错误处理
                        console.log(res)
                    }
                });
            })
            wx.error(function (res) {
                // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
                console.log(res);
            })
        })
        .catch(error => {
            // 处理请求错误
            console.log(error);
        });

</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值