1.请求后端接口初始化配置
$(function () {
$.ajax({
url:"/WeChatManage/WeChatManage/GetJSSDKConfig",
type:"get",
dataType:"json",
success: function (res) {
var data = res;
if (data.state==="success") {
wx.config({
debug: false,
appId: data.data.AppId,
timestamp: data.data.Timestamp,
nonceStr: data.data.NonceStr,
signature: data.data.Signature,
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
"scanQRCode"
//"chooseImage",
//"getLocation",
//"onMenuShareTimeline"
]
});
} else {
top.layer.msg(data.message, {
time: 4500
});
}
$("#scancode").on("click", function () {
ScanQrcode()
})
}
});
})
2.如果微信环境打开调用JSSDK,调用APP的ScanQrcode
function isAndroid() {
var u = navigator.userAgent;
if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) {
return true;
}
return false;
}
// 判断设备为 ios
function isIos() {
var u = navigator.userAgent;
if (u.indexOf("iPhone") > -1 || u.indexOf("iOS") > -1) {
return true;
}
return false;
}
var ua = window.navigator.userAgent.toLowerCase();
function isMiniProgram(callback) { //是否为小程序环境
if(ua.match(/MicroMessenger/i) == 'micromessenger'){ //判断是否是微信环境
//微信环境
callback(true)
}else if(isAndroid()||isIos()){
//非微信环境逻辑
callback(false)
}
}
3.扫码调用
function ScanQrcode() {
isMiniProgram(function (res) {
if (res) {
wx.scanQRCode({
needResult: 1,
scanType: ["qrCode", "barCode"],
success: function (res) {
top.QRCallBack(res.resultStr)
},
fail: function (res) {
top.layer.msg(JSON.stringify(res), {
time: 4500
});
//alert(JSON.stringify(res));
}
});
} else if (!res) {
//alert("手机APP")
try {
if (!!window.SWPJSObj) {
window.SWPJSObj.openQrCodeScan("QRCallBack");
return;
}
}
catch (e) {
}
$.modalOpen({
id: "QRScan",
title: "扫描设备",
width: "1000px",
height: "1000px",
url: "/APP/Scan/index",
btn: null,
callBack: function (iframeId) {
}
});
}
})
}