(app,小程式)
uni.scanCode({
onlyFromCamera: true, //隻能通過相機掃碼
success: function (res) {
if(res.result.indexOf("%")!=-1){
var str = res.result.substr(50);
let strs= str.split("-");
that.account = strs[0];
that.content = strs[1];
}
}
});
(公衆号H5)
第一步-> 進入項目目錄
npm install jweixin-module --save
第二步-> 在需要使用的頁面
import jweixin from 'jweixin-module'
scancode(){
var that = this;
// #ifdef H5
let url = window.location.href.split('#')[0]; // 很重要
let urls = url.split('#')[0];
that.$request({
url:'wx-gzh-sign',
methods:'GET'
},{
url:url
}).then(res=>{
let data = res.data.data;
jweixin.config({
debug: false, //測試時候用true 能看見wx.config的狀态是否是config:ok
appId: data.appid, // 必填,公衆号的唯一辨別(公衆号的APPid)
timestamp: data.timestamp, // 必填,生成簽名的時間戳
nonceStr: data.noncestr, // 必填,生成簽名的随機串
signature: data.sign, // 必填,簽名
jsApiList: ['scanQRCode'], // 必填,需要使用的JS接口清單
});
jweixin.ready(function (res) {
jweixin.checkJsApi({
jsApiList: ['scanQRCode'], // 需要檢測的JS接口清單,所有JS接口清單見附錄2,
success: function(res) {
jweixin.scanQRCode({
needResult: 1, // 預設為0,掃描結果由微信處理,1則直接傳回掃描結果,
scanType: ["qrCode","barCode"], // 可以指定掃二維碼還是一維碼,預設二者都有
success: function (res1) {
if(res1.resultStr.indexOf("%")!=-1){
var str = res1.resultStr.substr(50);
let strs= str.split("-");
that.account = strs[0];
that.content = strs[1];
}
// window.location.href = res1.resultStr; //安卓機型跳轉渲染有問題 是以要加這句
}
});
}
});
});
jweixin.error(function (res1) {
console.log("接口調取失敗:"+res1);
});
});
// #endif