首页 > 编程语言 >小程序自定义扫码界面

小程序自定义扫码界面

时间:2022-12-05 11:24:22浏览次数:60  
标签:function 扫码 界面 自定义 flag var false canScan wx

小程序扫一扫wx.scanCode   这个api扫描不能自定义

这个时候就需要用camera 自定义扫描界面; camera文档传送门

 

 

 

效果图:(需要获取camera授权权限)

代码:

<view wx:if="{{!canScan}}">
    <view class="scanBtn" bindtap="richscan">扫一扫</view>
</view>

<!-- 处理问题 1 -->
<view class="scanBox" wx:if="{{canScan}}" bindtap="cancelScan">
    <camera class="camera" binderror="error" mode="scanCode" bindscancode="scancode">
        <cover-image class="coverImg" src="QHImage/iconScanBg.png"></cover-image>
        <cover-view class="moveLine" animation="{{animation}}"></cover-view>
    </camera>
    <view class="scanTip {{animation}}">请扫描二维码</view>
</view>
var animation = wx.createAnimation({});

Page({

    /**
     * 页面的初始数据
     */
    data: {
        canScan: false, 
        scanResult: "" // 处理问题 3
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onl oad: function (options) {
    },

    // 触发扫一扫
    richscan: function() {
        var that = this;
        that.scanAnimation();
        that.setData({
            canScan: !that.data.canScan
        });
        wx.setNavigationBarColor({
          backgroundColor: '#333333',
          frontColor: '#ffffff',
        });
    },

    // 扫一扫 
    scancode: function(e) {
        var that = this;
        var result = e.detail.result;
        if(result) {
            that.setData({
                scanResult: result,
                canScan: false
            });
            wx.setNavigationBarColor({
                backgroundColor: '#ffffff',
                frontColor: '#000000'
            });
        }
    },

    // 扫描动画
    scanAnimation: function() {
        var that = this;
        var flag = true;
        setInterval(function() {
            if(flag) {
                animation.translateY(250).step({duration: 3000});
                flag = !flag;
            } else {
                animation.translateY(10).step({duration: 3000});
                flag = !flag;
            }
            that.setData({
                animation: animation.export()
            })
        }.bind(this), 3000)
    },

    // 处理问题 2
    // 用户拒绝授权相机后,会提示这个弹框,可以去授权
    // 设置允许后,无法重新调用摄像头,因此在 onShow 中 getSetting 查看 scope.camara 是否授权
    error: function(e) {
        wx.showModal({
            title: "提示",
            showCancel: false,
            confirmText: "授权",
            content: "请确认授权使用相机进行扫码功能",
            success: function(tip) {
                if(tip.confirm) {
                    wx.openSetting(); // 打开设置
                }
            }
        })
    },

    // 取消扫描
    cancelScan: function(){ 
        this.setData({
            canScan: false
        });
        wx.setNavigationBarColor({
          backgroundColor: '#ffffff',
          frontColor: '#000000',
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        wx.getSetting({
            success(res) {
                if(!res.authSetting['scope.camera']) {
                    setTimeout(() => {
                        that.setData({
                            canScan: false
                        }, 300);
                    });
                } else {
                    that.setData({
                        canScan: false
                    });
                }
            }
        });
    },    
})
.scanBtn {
    width: 300rpx;
    height: 80rpx;
    margin: 350rpx auto;
    line-height: 80rpx;
    text-align: center;
    background-color: rgb(149, 193, 206);
}

/* 扫码框 */
.scanBox {
    position: fixed;
    top: 0;
    width: 100%;
    height: 100vh;
    background-color: #333;
}

.camera {
    position: relative;
    overflow: hidden;
    width: 70vw;
    height: 70vw;
    margin: 20vh auto 0;
}

.scanTip {
    padding: 30rpx 0;
    font-size: 30rpx;
    text-align: center;
    color: #fff;
}

.moveLine {
    position: absolute;
    top: -10rpx;
    width: 80%;
    height: 6rpx;
    margin-left: 10%;
    border-radius: 30rpx;
    background:#1D79FF;
}

参考链接: https://www.cnblogs.com/L-xjco/p/15649832.html

标签:function,扫码,界面,自定义,flag,var,false,canScan,wx
From: https://www.cnblogs.com/520BigBear/p/16951792.html

相关文章

  • Navicat中MySQL命令列界面操作及基础常用命令
    https://blog.csdn.net/weixin_71712767/article/details/126863842如何使用navicat进入MySQL命令行界面?如何通过命令对数据库/表进行操作? 如何在Navicat中MySQL链接打......
  • MeterSphere BeanShell 前后置脚本如何引用自定义 Jar 包
    需求接口测试中需要对post请求的body参数进行排序后,进行md5加密,生成sign字符串,放入请求体中提交。思路:在前置脚本中自动获取请求参数中的值,进行sign加密,生......
  • RDLC后台自定义报表模板
    首先封装一个公共类,统一来操作RDLC报表usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Xml;usingSystem.Data;usingMi......
  • 小程序自定义底部导航 custom-tab-bar
    1.app.json     2.将custom-tab-bar放到根目录下(pages同级)3. custom-tab-bar 代码{"component":true}Component({data:{USERTYPE:'cu......
  • spring源码 自定义beanDefinition的添加逻辑
      首先我自定义了两个beandefinition,第一个会产生第二个,并且都是特殊的实现,能够避开前面的循环读取,所以,spring在最后用了一个while循环,一个flag就将,beandefinition新增......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有一......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ 在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有......
  • 【C语言】自定义一个模仿strlen()函数功能的函数
    自定义一个模仿strlen()函数功能的函数//my_strlen()函数声明,返回字符数组中字符的个数//参数为字符数组的地址intmy_strlen(char*str);intmain(void){ charch[......
  • Springboot之additional-spring-configuration-metadata.json自定义提示
    【3】@ConfigurationProperties注入属性https://blog.csdn.net/qq_25614773/article/details/124788923 https://docs.spring.io/spring-boot/docs/2.4.7/reference/ht......
  • 15.【C语言进阶】自定义类型
    结构体的声明常规的结构的声明太过简单常见,不再过多阐述。特殊声明在声明结构体的时候可以不完全的声明struct{ inti; doubled; charc;}x;struct{ inti;......