时间: 2025-09-23 【学无止境】 阅读量:共3人围观
简介 使用微信二维码扫码核销,第一次进入页面后提示config:invalid signature,点击扫码提示没有权限。文章介绍如何避免这个问题,有效解决。
第一步引入wx.js的文件
import wx from "weixin-js-sdk";
// https://res.wx.qq.com/open/js/jweixin-1.6.0.js
第二步代码实现
let params = ref({ appId: '', timestamp: 0, nonceStr: '', signature: '', });
const getfindSign = async () => { const currentUrl = window.location.href.split("#")[0]; try { let res = await findSign({ url: currentUrl, }); if (res.status == 0) { // 给全局变量赋值 params.value = res.data; } } catch (error) {} };
const startScan = async () => { if (!isWxBrowser()) { uni.showToast({ title: "请在微信中使用此功能", icon: "none" }); return; } // 异步从后台拉取新的签名,避免用户停留在页面的时间过长,导致签名过期失效 getfindSign(); try { wx.config({ debug: false, appId: params.value.appId, timestamp: params.value.timestamp, nonceStr: params.value.nonceStr, signature: params.value.signature, jsApiList: ["scanQRCode"], }); wx.ready(async () => { wx.scanQRCode({ debug: false, needResult: 1, scanType: ["qrCode"],// 默认是配置了二维码和条形码,如果代码没有问题,但是还是拉取不了扫码,请先去掉条形码后,再试试。 success: (res) => { // 处理扫码成功的业务 }, fail: (res) => { uni.showToast({ title: res.errMsg, icon: "none" }); console.log("startScan ~ res:", res); }, }); }); } catch (error) { console.log("startScan ~ error:", error); } };