uniapp app,小程序,公眾號h5調用掃一掃


(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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM