微信小程序自定义弹框+生成二维码功能

本文介绍如何在小程序中创建一个自定义的弹窗组件,该组件包含生成二维码的功能。通过设置组件样式和事件处理,实现了弹窗的显示、关闭、以及二维码的生成。同时,提供了复制二维码链接到剪贴板的功能。代码示例详细展示了组件的结构、样式和使用方法。
摘要由CSDN通过智能技术生成

最近在做小程序的功能,需求是弹框生成二维码功能,所以根据需求自定义了一个弹框组件,后续其他地方也可以用到。

最后效果图如下

 

dialog部分

<!--components/dialog/dialog.wxml-->
<view class='dialog-custom' wx:if="{{visible}}">
  <view class='dialog-mask' bindtap="clickMask"></view>
  <view class="dialog-main">
    <view class="dialog-container">
      <view class='dialog-container__title' wx:if="{{title.length>0}}">
        <view class='title-label'>{{ title }}</view>
          <view class='title-icon'>
            <van-icon wx:if="{{showClose}}" bindtap='close' name="cross" />
          </view>
      </view>
      <view class='dialog-container__body'>
        <slot name="dialog-body"></slot>
      </view>
      <view class='dialog-container__footer' wx:if="{{showFooter}}">
        <view class='dialog-container__footer__cancel' bindtap="close">取消</view>
        <view class='dialog-container__footer__confirm' bindtap='confirm'>确定</view>
      </view>
    </view>
  </view>
</view>
Component({
  /**
  * 组件的属性列表
  */
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    width: {
      type: Number,
      value: 85
    },
    position: {
      type: String,
      value: 'center'
    },
    title: {
      type: String,
      value: '提示'
    },
    showClose: {
      type: Boolean,
      value: true
    },
    showFooter: {
      type: Boolean,
      value: false
    },
  },
  
  /**
  * 组件的初始数据
  */
  data: {
   
  },
  options:{
    multipleSlots: true
  },
  /**
  * 组件的方法列表
  */
  methods: {
    clickMask() {
      this.setData({ visible: false });
    },
  close(){
    this.setData({ visible: false });
  },
  cancel() {
    this.setData({ visible: false });
    this.triggerEvent('cancel');
  },
  confirm() {
    this.setData({ visible: false });
    this.triggerEvent('confirm');
  }
  }
})
{
  "component": true,
  "usingComponents":{}
}
.dialog-custom {
  width: 100vw;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9999;
}
  
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10000;
  width: 100vw;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}
  
.dialog-main {
  position: fixed;
  z-index: 10001;
  top: 50%;
  left: 0;
  right: 0;
  width: 85vw;
  height: auto;
  margin: auto;
  transform: translateY(-50%);
}
  
.dialog-container {
  margin: 0 auto;
  background: #fff;
  z-index: 10001;
  border-radius: 3px;
  box-sizing: border-box;
  padding: 40rpx;
}
  
  .dialog-container__title {
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  margin-bottom: 20rpx;
  position: relative;
}
.dialog-container__title .title-label{
  display: inline-block;
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  font-size: 36rpx;
  color: #000;
  text-align: center;
}
  
.dialog-container__title .title-icon{
  width: 34rpx;
  height: 50rpx;
  position: absolute;
  top: 0;
  right: 0;
}
  
.dialog-container__body {
  padding-top: 10rpx;
  font-size: 32rpx;
  line-height: 50rpx;
  padding: 0rpx 0 60rpx;
  text-align: center;
}
  
.dialog-container__footer {
  height: 76rpx;
  line-height: 76rpx;
  font-size: 32rpx;
  text-align: center;
  border-top: 1px solid #f1f1f1;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}
  
.dialog-container__footer .dialog-container__footer__cancel {
  width: 50%;
  color: #999;
  display: inline-block;
}
  
.dialog-container__footer .dialog-container__footer__cancel::after{
  position: absolute;
  right: 50%;
  bottom: 0;
  content: '';
  width: 2rpx;
  height: 76rpx;
  background: #f1f1f1;
}
  
.dialog-container__footer .dialog-container__footer__confirm {
  color: #3B98F7;
  width: 50%;
  display: inline-block;
  text-align: center;
}

引用页面部分

{
  "usingComponents": {
    "dialog": "/components/dialog/dialog"
  }
}
<dialog visible="{{dialogVisible}}" showFooter="{{footerVisible}}" title="{{titleVisible}}">
    <view class='dialog-body' slot="dialog-body">
      <view class='dialog-content'>
        <view class="dialog-url">
          <van-cell-group>
            <van-field
              value="{{ url }}"
              center
              use-button-slot
            >
              <van-button slot="button" size="small" type="info" bindtap="handleCopy">
                复制
              </van-button>
            </van-field>
          </van-cell-group>
        </view>
        <view class='dialog-canvas'>
          <canvas style="width: 400rpx; height: 400rpx;" canvas-id='canvas'></canvas>
        </view>
        <view class="dialog-text">扫码二维码快速加入</view>
      </view>
    </view>
  </dialog>
.dialog-canvas{
  display: flex;
  justify-content: center;
  align-items: center;
}
.dialog-text{
  color:rgb(0, 122, 255);
  padding: 10rpx 0;
}
const app = getApp();
const QRCode = require("../../../utils/weapp-qrcode.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialogVisible:true,
    footerVisible:false,
    titleVisible:'测试',
    url:'https://blog.csdn.net/Fansr_'
  },

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

  handleQRCode(){
    //生成二维码
    let size = {}
    size.w = wx.getSystemInfoSync().windowWidth / 750 *600
    size.h = size.w
    var qrcode = new QRCode('canvas', {
      text: this.data.url,
      width: size.w,
      height: size.h,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },

  //执行复制
  handleCopy(){
    wx.setClipboardData({
      data: this.data.url,
      success: function (res) {
        wx.getClipboardData({
          success (res) {
            wx.showToast({
              title: '复制成功',
            })
          }
        })
      }
    })
  },
})

生成二维码需要用到weapp-qrcode.js,下载https://github.com/tomfriwel/weapp-qrcode/blob/master/utils/weapp-qrcode.js点开链接保存到相应的位置,我放在/utils/weapp-qrcode.js文件里。

其他页面样式根据实际进行相应调试即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值