企业微信移动端项目调用扫码定位功能实现,获取code采用vue来搭建本项目,如果需要调用企业微信js-sdk就需要给登录接口传code,code在index.html获取
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
window.localStorage.setItem('code', unescape(r[2]))
if (r != null) return unescape(r[2]);
return null;
}
getUrlParam('code')
获取完code后就要调用登录接口,把获取的code传进去后台会返回当前用户信息和token,存起来就好了
let code = window.localStorage.getItem("code");
singleSignOn({ code })
在登录接口返回值里需要注册企业微信sdk权限,也就是我们需要调用的api
getSdk({
token: token,
url: location.href.split("#")[0],
}).then((res) => {
wx.config({
beta: true,
debug: false,
appId: res.appId,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: [
"checkJsApi",
"startRecord",
"stopRecord",
"playVoice",
"pauseVoice",
"stopVoice",
"chooseImage",
"translateVoice",
"uploadVoice",
"invoke",
"getLocation",
"openLocation",
],
});
wx.ready(() => {
wx.checkJsApi({
jsApiList: [
"checkJsApi",
"startRecord",
"stopRecord",
"playVoice",
"pauseVoice",
"stopVoice",
"chooseImage",
"translateVoice",
"uploadVoice",
"invoke",
"getLocation",
"openLocation",
],
success: function (res) {
},
});
});
wx.error(function (res) {
console.log(res,'invokeres')
Notify({ type: "warning", message:'获取微信权限失败,请重新登陆' });
});
});
接下来就可以实现一个扫码功能
wx.scanQRCode({
desc: "scanQRCode desc",
needResult: 1,
scanType: ["qrCode"],
success: (res) => {
let resultStr = res.resultStr.split("=")[1]
},
error: (res) => {
if (res.errMsg.indexOf("function_not_exist") > 0) {
alert("版本过低请升级");
}
},
});