小程序开发二维码扫描自定义页面 自定义扫码页面 扫一扫

<template>
  <view class="custom-scan">
    <!-- 扫码区域 -->
    <camera
      mode="scanCode"
      frame-size="large"
      class="scan-view"
      @scancode="scancode"
    >
      <view class="scan-container-box">
        <view class="scan-wrap">
          <view class="scan-line"></view>
        </view>
      </view>
      <view class="scan-container-main">
        <slot></slot>
      </view>
    </camera>
  </view>
</template>

<script>
// const innerAudioContext = wx.createInnerAudioContext(); // 提示音
// innerAudioContext.src = "https://qdstorage.okii.com/retail-app/common/beep.mp3";

export default {
  data() {
    return {
      isTf: false,
    };
  },
  methods: {
    // 扫码
    scancode(event) {
      if (this.isTf) return;
      this.isTf = true;
      setTimeout(() => {
        // 3秒扫描一次
        this.isTf = false;
        return;
      }, 3000); 
      wx.vibrateShort(); // 触发手机振动
      // innerAudioContext.play(); // 提示音
      const { result } = event.detail; // 获取校验扫描结果
      console.log("result", result);
      wx.showToast({ title: `扫描结果:${result}`, icon: "none" });
    },
  },
};
</script>
<style lang="scss" scoped>
.custom-scan {
  width: 100vw;
  height: 100vh;

  .scan-view {
    width: 100%;
    height: 100%;
  }
  .scan-container-main {
    display: flex;
    width: 100%;
    height: 60vh;
    position: fixed;
    bottom: 0;
    background: #fff;
  }
  .scan-container-box {
    width: 100vw;
    height: 40vh;
    display: flex;
    justify-content: center;
    align-items: center;
    -webkit-background-clip: padding-box;
    -moz-background-clip: padding;
    background-clip: padding-box;

    .scan-wrap {
      position: relative;
      width: 400rpx;
      height: 300rpx;
      .scan-line {
        position: absolute;
        width: 100%;
        height: 1rpx;
        background-color: #0f0;
        box-shadow: 0px -0px 10px 1px #0f0;
        top: 0;
        left: 0;
        animation: scan 2s infinite linear;
      }
    }
  }

  @keyframes scan {
    0% {
      transform: translateY(0);
      opacity: 1;
    }
    80% {
      opacity: 1;
      box-shadow: 0px -0px 10px 1px #0f0;
    }
    100% {
      transform: translateY(20vh);
      opacity: 0;
    }
  }
}
</style>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义扫码页面,你需要先使用微信小程序提供的 wx.scanCode 接口进行二维码扫描功能的实现。然后,你可以在扫码成功后,跳转到自定义页面进行显示。 以下是实现自定义扫码页面的基本步骤: 1. 在 app.json 文件中配置自定义页面的路径。 ```json { "pages": [ "pages/scan/scan", "pages/custom/custom" ] } ``` 2. 创建自定义扫码页面的代码文件。 在 pages 目录下创建一个名为 custom 的目录,并在该目录下创建一个名为 custom.wxml 的文件,用于编写自定义页面的布局结构。然后在 custom 目录下创建一个名为 custom.js 的文件,用于编写自定义页面的逻辑代码。 3. 在扫码成功后,跳转到自定义页面。 在扫码成功后,可以使用 wx.navigateTo 方法跳转到自定义页面。在跳转时,可以通过 URL 参数传递扫码结果或其他需要的信息。 ```javascript wx.scanCode({ success: (res) => { wx.navigateTo({ url: '/pages/custom/custom?qrcode=' + res.result }) } }) ``` 4. 在自定义页面中获取 URL 参数。 在自定义页面中,可以通过 onLoad 生命周期获取传递的 URL 参数。在 onLoad 中使用 options 参数获取 URL 参数。 ```javascript onLoad: function (options) { console.log(options.qrcode) } ``` 5. 在自定义页面中显示扫码结果。 在自定义页面中,可以使用传递的 URL 参数或其他方式获取扫码结果,并在页面中显示。可以使用 setData 方法更新页面数据。 ```javascript onLoad: function (options) { this.setData({ qrcode: options.qrcode }) } ``` 在 custom.wxml 中使用 {{}} 语法绑定数据,显示扫码结果。 ```html <view>{{qrcode}}</view> ``` 这样就可以实现自定义扫码页面的功能了。需要注意的是,在自定义页面中,还可以添加其他功能和样式,使页面更加丰富和美观。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值