小程序自定义扫码页面

微信官方文档

index.wxml

<!-- 扫一扫按钮+scanResult扫描结果 -->
<button wx:if="{{!canScan}}" type="primary" bindtap="scanShowClick">自定义扫一扫{{scanResult}}</button>
<!-- 扫描框 -->
<view class="scanBox" wx:if="{{canScan}}">
    <camera class="camera" mode="scanCode" bindscancode="scancode" flash='{{flashBtn}}'>
        <image class="coverImg" src="{{iconScanBgGif}}"></image>
    </camera>
    <view style="text-align: center;margin: 80rpx 0 40rpx 0;">
        <button class="cu-btn scancodePic" bindtap="changeflashBtn">
            <image src="{{flashBtn=='on'?scancodeOpen:scancodeClose}}"></image>
        </button>
    </view>
    <view class="scanTip">请扫描大熊帅照</view>
    <!-- 关闭扫码页面 -->
    <view class="closeScan" bindtap="scanHideClick">关闭</view>
</view>

index.js


import { iconScanBgGif,scancodeOpen,scancodeClose } from "../../../utils/imgUrl/index";
const permisson = require("../../../utils/tools/permisson"); // 权限校验封装
const userCameraName = "scope.camera"; // 摄像头权限
const userCameraZhName = "手机摄像头"; // 摄像头权限对应的中文名称
Page({
    /**
     * 页面的初始数据
     */
    data: {
        iconScanBgGif,scancodeOpen,scancodeClose,
        canScan: false, // 是否显示自定义扫码界面
        flashBtn: 'off', // off关闭   on  打开手电筒
        scanResult: '' // 扫描结果
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
    },   
    // 打开关闭手电筒
    changeflashBtn(){
        this.setData({
            flashBtn:this.data.flashBtn=='off'?'on':'off'
        })
    },
    // 显示扫码界面(扫一扫)
    scanShowClick(){
        // 校验权限, 必须开启摄像头权限
        wx.getSetting({
            success:async(res)=> {
                if(!res.authSetting['scope.camera']) {
                    // 权限封装
                    permisson.permission_request(userCameraName, userCameraZhName);
 
                    // 也可自己写,通过 wx.authorize 打开有关授权=>官方链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/authorize/wx.authorize.html
                } else {
                    this.setData({
                        canScan:true
                    })
                    // 顶部标题栏背景变黑
                    wx.setNavigationBarColor({
                        backgroundColor: '#000000',
                        frontColor: '#ffffff',
                    });
                }
            }
        })
    },
    // 隐藏扫码界面
    scanHideClick(){
        this.setData({canScan:false})
        // 顶部标题栏背景变白
        wx.setNavigationBarColor({
            backgroundColor: '#ffffff',
            frontColor: '#000000',
        });
    },
    // 扫一扫返回数据
    scancode(e) {
        console.log(e.detail)
        const {result:scanResult} = e.detail; 
        if(result) {
            this.setData({scanResult,canScan: false})
            // 顶部标题栏背景变白
            wx.setNavigationBarColor({
                backgroundColor: '#ffffff',
                frontColor: '#000000'
            });
        }
    }
})

index.wxss

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


.camera {
    position: relative;
    width: 70vw;
    height: 70vw;
    margin: 20vh auto 0;
}
.coverImg{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 99999;
}
.scanTip {
    padding: 20rpx 0 0 0;
    font-size: 32rpx;
    text-align: center;
    color: #fff;
}


/* 开关手电筒 */
.scancodePic{
    background: transparent !important;
    width: 100rpx;
    height: 100rpx;
    padding: 0 !important;
}
.scancodePic>image{
    width: 100%;
    height: 100%;
}
/* 关闭按钮 */
.closeScan{
    position: absolute;
    bottom: 0;
    right: 0;
    padding: 20rpx 30rpx;
    z-index: 99999;
    background: #fff;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值