小程序生成二维码

简单粗暴

一、效果

 二、具体步骤、代码

1、小程序wxml文件

<view class="container">
  <view class="form-field">
    <text>优惠码:</text>
    <input type="text" placeholder="请输入优惠码" bindinput="onCodeInput" />
  </view>
  <view class="form-field">
    <text>优惠金额:</text>
    <input type="number" placeholder="请输入优惠金额" bindinput="onValueInput" />
  </view>
  <view class="form-field">
    <text>优惠标题:</text>
    <input type="text" placeholder="请输入优惠标题" bindinput="onTitleInput" />
  </view>

  <button class="btn-generate" type="primary" bindtap="generateQRCode">生成优惠券二维码</button>

  <view class="qrcode-container">
    <canvas canvas-id="myCanvas" style="width: 100px; height: 100px;margin:0 auto"></canvas>
  </view>
</view>

2、小程序wxss代码


.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx;
}

.form-field {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}
  text {
    width: 100rpx;
  }

  input {
    flex: 1;
    border: none;
    border-bottom: 1px solid #ccc;
    padding: 5rpx 10rpx;
    text-align: center;
    font-size: 30rpx;
  }

.btn-generate {
  width: 100%;
  height: 80rpx;
  background-color: #007aff;
  color: #fff;
  border-radius: 10rpx;
  font-size: 32rpx;
  line-height: 80rpx;
  text-align: center;
  margin-bottom: 30rpx;
}

.qrcode-container {
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #ccc;
}

2、小程序js代码


Page({
  data: {
    couponCode: '',
    couponValue: 0,
    couponTitle: ''
  },

  onCodeInput(e) {
    this.setData({
      couponCode: e.detail.value
    })
  },

  onValueInput(e) {
    this.setData({
      couponValue: e.detail.value
    })
  },

  onTitleInput(e) {
    this.setData({
      couponTitle: e.detail.value
    })
  },
  generateQRCode() {
    const { couponCode, couponValue, couponTitle } = this.data;
  
    // 构造二维码链接
    const qrCodeUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(`优惠码:${couponCode},优惠金额:${couponValue},优惠标题:${couponTitle}`)}`;
  
    // 下载二维码图片资源
    wx.downloadFile({
      url: qrCodeUrl,
      success: (res) => {
        // 下载成功后获取图片本地路径
        const filePath = res.tempFilePath;
        // 在canvas中生成二维码图片
        const context = wx.createCanvasContext('myCanvas');
        context.drawImage(filePath, 0, 0, 100, 100);
        context.draw();
      },
      fail: (err) => {
        console.error(err);
      }
    });
  }


})

直接cv就行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值