小程序web-view嵌入h5扫码 jssdk方式

扫码方式:使用jssdk方式

1.需要使用微信公众号,在微信开发者平台配置 API IP白名单以及js接口安全域名
2.需要给后端提供 AppId、AppSecret生成jssdk的config参数(appId、timestamp、nonceStr、signature)
3.引入jssdk
js 复制代码
	<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"/>  
        <script>  
            window.jWeixin=window.wx;  
            delete window.wx;  
        </script> 
4.请求后端获取配置接口(参数需要传递当前页面url,不包括hash值)

encodeURIComponent(location.href.split("#")[0])

5.点击按钮扫码
js 复制代码
 wxScan() { 
      jWeixin.config({
        debug: false, // 调试模式:true会弹出调试信息,上线改为false
        appId: this.wxConfig.appId, // 公众号appId
        timestamp: this.wxConfig.timestamp, // 时间戳
        nonceStr: this.wxConfig.nonceStr, // 随机字符串
        signature: this.wxConfig.signature, // 签名
        jsApiList: ["scanQRCode"], // 需使用的API列表(扫码)
      });

      // 配置 ready 回调,确保配置成功后再调用
      jWeixin.ready(() => {
        jWeixin.scanQRCode({
          needResult: 1, // 1=直接返回结果
          scanType: ["qrCode", "barCode"],
          success: (res) => {
            const scanResult = res.resultStr; // 扫码结果
          },
          fail: (err) => {
          },
        });
      });
      // 处理配置失败情况
      jWeixin.error((res) => {
        console.error("JS-SDK 配置失败", res);
        uni.showToast({
          title: "扫码功能初始化失败",
          icon: "none",
        });
      });
    },
相关推荐
软件技术NINI12 分钟前
圣罗兰彩妆html+css 4页
前端·css·html
2601_9516152042 分钟前
网站模板 网站网页模板下载
前端·网络·html
一木 之林1 小时前
微信小程序开发全攻略·文章版|00 总览:74集学习路径与知识地图
人工智能·学习·计算机视觉·微信小程序·小程序
酷炫码神1 小时前
Day 1|HTML5:一个商城首页的骨架,从 DOCTYPE 到语义化
前端·html·html5
AI源码2 小时前
鲜花商城(HTML 版)
前端·html
hiahiahia1232 小时前
Message 和对话
服务器·前端·数据库
溪语流沙2 小时前
【Web全栈进阶】Composition API与组件通信:props深入、emit、插槽
前端·css·vue.js
ZoeLandia2 小时前
TypeScript 03:泛型与工具类型
前端·typescript
code_Bo2 小时前
从策划到上线:我和 AI 一起做了个微信记账小程序
前端·ai编程·cursor
Shirley~~3 小时前
Three.js加载Gltf模型并播放动画
前端·3d