需求: H5页面调用微信扫一扫扫描二维码,获取二维码内容
技术: vue,vuex
参考文档:微信开放文档
问题: Android正常打开扫一扫,ios必须刷新一下才能打开
解决:
npm install weixin-js-sdk
安装完成之后在页面引入(在全局也行)
import wx from "weixin-js-sdk";
sys_click() {
let that = this;
$.ajax({
url: "请求地址",
type: "POST",
dataType: "JSON",
data: {
url:that.wxUrl,//这里的wxUrl是vuex里面的,接着往下看
},
success: res => {
that.resMsg = res;//这里我把拿到的参数赋值了一下,也可以直接用
wx.config({
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
debug: false,
// 必填,公众号的唯一标识
appId: that.resMsg.appId,
// 必填,生成签名的时间戳
timestamp: that.resMsg.timestamp,
// 必填,生成签名的随机串
nonceStr: that.resMsg.nonceStr,
// 必填,签名,见附录1
signature: that.resMsg.signature,
// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
jsApiList: ["scanQRCode"]
});
}
});
}
wx.config
失败会wx.error
,如果成功就wx.ready
wx.error(res => {
alert("出错了:" + res.errMsg);
});
wx.ready(() => {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: res => {
alert("扫描成功", res.resultStr);
that.coding = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
}
});
});
invalid signature
签名错误,那么就需要用到vuex如果从 A 页面跳到 B(扫码) 页面,如果 A 页面地址123,B 页面地址是456,这个时候请求后台的url不可以都传123。因为ios会用 A 页面的地址去生成签名,而Android则会用 B 页面的地址去生成签名,所以传入后台的url参数必须是不一样的
判断是否是ios方法:
let u = navigator.userAgent;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
A页面(从这个页面跳转到扫码页面)
<template>
<div>
<button @click="goCode">跳转扫码页面button>
div>
template>
<script>
import {mapMutations} from 'vuex';
export default {
components: {
mapMutations,
},
created() {
this.isIOS();
},
methods: {
...mapMutations(['saveUrl','judgeModels']),
isIOS(){
// 判断手机机型,如果是ios就把本页面地址存到vuex
let u = navigator.userAgent;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
this.judgeModels(isiOS);
if(isiOS){
this.saveUrl(window.location.href)
}
},
goCode(){
this.$router.push({
name: "serve",
params: {
id: 123456
}
});
},
}
};
script>
B页面(扫码页面)
<template>
<div>
<input type="text" v-model="coding" placeholder="扫描二维码获取" />
<button @click="sys_click">扫一扫button>
div>
template>
<script>
import wx from "weixin-js-sdk";
import {mapState, mapMutations} from 'vuex';
export default {
components: {
mapState,
mapMutations,
},
computed:{
...mapState(['model','wxUrl']),
},
data() {
return {
coding:'',
resMsg: {}
};
},
mounted() {
if (!this.$route.params.id) {
this.$router.go(-1);
return;
}
if(this.model=='android'){
this.saveUrl(window.location.href)
}
},
methods: {
...mapMutations(['saveUrl']),
// 扫一扫
sys_click() {
let that = this;
$.ajax({
url: "请求地址",
type: "POST",
dataType: "JSON",
data: {
url:that.wxUrl,
},
success: res => {
that.resMsg = res;
wx.config({
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
debug: false,
// 必填,公众号的唯一标识
appId: that.resMsg.appId,
// 必填,生成签名的时间戳
timestamp: that.resMsg.timestamp,
// 必填,生成签名的随机串
nonceStr: that.resMsg.nonceStr,
// 必填,签名,见附录1
signature: that.resMsg.signature,
// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
jsApiList: ["scanQRCode"]
});
}
});
wx.error(res => {
alert("出错了:" + res.errMsg);
});
wx.ready(() => {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: res => {
alert("扫描成功", res.resultStr);
that.coding = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
}
});
});
},
},
};
script>
vuex页面
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
wxUrl: '',
model:''
},
mutations: {
// 更改页面地址
saveUrl(state, value) {
state.wxUrl = value;
},
// 判断手机类型
judgeModels(state, value){
if(value){
state.model = 'ios';
}else{
state.model = 'android'
}
}
},
})