例:微信调用扫一扫:
概述 | 微信开放文档
1. 安装jweixin-module ; 并引入 var wx = require("jweixin-module");
2. 公众号后台配置js接口域名;访问后台接口获取签名,随机串,时间戳;
3. wx.config注入接口验证配置;
4. wx.read配置成功后验证y处理;
5. 调用微信api。
//微信扫一扫
async wxScan() {
let data = await commonRq("api/getWechatTicket", {
url: window.location.href.split("#")[0],
wechatAppId: config.wx_app_id, //"wxdd8f7302cae96488", config.wx_app_id
});
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.result.appid, // 必填,公众号的唯一标识
timestamp: data.result.timestamp, // 必填,生成签名的时间戳
nonceStr: data.result.noncestr, // 必填,生成签名的随机串
signature: data.result.signature, // 必填,签名,见附录1
jsApiList: [
"checkJsApi",
"scanQRCode", // 微信扫一扫接口
], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
this.scanCode();
},
scanCode() {
var $this = this;
//判断当前客户端版本是否支持指定JS接口
wx.checkJsApi({
jsApiList: ["scanQRCode"], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: (res) => {
wx.ready(() => {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: (data) => {
if (data.resultStr) {
$this.checkPatient(data.resultStr); //当needResult 为 1 时,扫码返回的结果
}
},
fail: (data) => {
$this.$toast("打开扫码失败!");
},
});
});
},
fail: function (res) {
$this.$toast("checkJsApi fail");
},
});
},