Enterprise WeChat Official API
Step 1: Import JS file
<script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
Step 2: Inject permission verification configuration through the config interface
// 使用微信内置API都需要进入校验操作
wx.config({
beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '', // 必填,企业微信的corpID
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名,见 附录-JS-SDK使用权限签名算法
jsApiList: [] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
});
Calling the config API to obtain an encrypted signature requires encryption and encryption rules, see the official document for details
url (the URL of the current web page, excluding # and its following parts) only removes the last part of the content after # / cannot be removed, the following is an error example, and the removal of post-validation failed
const link = location.href.split('#')[0]
const url = link.substring(0, link.length - 1)
The jsapi_ticket is valid for 7200 seconds and is obtained through access_token. Since the number of API calls to obtain jsapi_ticket is very limited (one enterprise can obtain up to 400 calls within one hour, and a single application cannot exceed 100 calls), frequent refreshing of jsapi_ticket will lead to limited API calls and affect their own business. Developers must The service caches jsapi_ticket globally.
Step 3: Handle successful verification through the ready interface
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客 户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
Step 4: Handle failed verification through the error interface
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
Error information can be queried through the official error query tool
Step 5: Call WeChat API
wx.scanQRCode({
desc: 'scanQRCode desc',
needResult: 0, // 默认为0,扫描结果由企业微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码(一维码),默认二者都有
success: function(res) {
// resultStr为JOSN类型
var result = res.resultStr;//当needResult为1时返回处理结果
},
error: function(res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本过低请升级')
}
}
});