微信小程序生成二维码

目前是在开发小程序端 --> 微信小程序。然后接到需求:根据 form 表单填写内容生成二维码(第一版:表单目前需要客户进行自己输入,然后点击生成按钮实时生成二维码,不需要向后端请求,不存如数据库)。然后找了许多大神的笔记,可能是自己第一次开发小程序端,所以一直都不成功。最后找到了下面的一种方法。仅供参考。

首先需要打开终端,然后进行安装 qrcode.js 。

shell 复制代码
// 安装
// h5
npm install qrcode
// 小程序
npm install uqrcodejs
-----------------------
// 若是安装不成,可以先查看一下镜像路径
npm config get registry
// 然后换成淘宝镜像
npm config set registry https://registry.npmmirror.com/
// 然后再安装

安装完成后页面进行导入

js 复制代码
// h5
import QRCode from 'qrcode';
// 小程序
import UQRCode from 'uqrcodejs';

我选择的是点击生成按钮,然后用 popup 弹出框,在弹出框中让最后生成的二维码赋值给 <image :src="qrCodeImage"> 标签。

vue 复制代码
<view class="generate-code">
  <button @click="generateCode" type="primary" class="agree-btn">
    生成二维码
  </button>
  <u-popup v-model="show" mode="center" width="80%" height="60%" border-radius="14">
    <view class="content">
      <view class="item-content">
        <view class="item-content-title"
              >{{ marketUnit }}
        </view>
        <view class="item-content-title"
              >批次号:
        </view>
        
        <!-- 这里需要注意:image 标签的是 h5 的生成方式,但是不能用于小程序端。想要生成小程序端,需要使用 canvas 标签来接收 -->
        <!-- h5 -->
        <image :src="qrCodeImage" class="item-content-title item-content-title-image"></image>
      	<!-- 小程序 -->
        <canvas id="qrcode" canvas-id="qrcode" class="item-content-title item-content-title-image"/>
        
      </view>
      <button @click="saveOrShare" class="item-content-button">长按保存或分享	
      </button>
    </view>
  </u-popup>
  <button @click="showQRCodeRecord" style=" background-color: #f0f2f6; color: #55aaff; font-size: 28rpx;text-align: center; padding-top: 10px; width: 50%; height: 76rpx;" >二维码生成记录</button>
</view>
js 复制代码
data(){
  return{
		show: false,
		marketUnit: '',
		qrCodeImage: '',
	}
},
methods:{
	generateCode() {
		// 打开弹框
		this.show = true;
		this.createCode(); 
	},
	async createCode() {
    // h5
    try {
      // 生成二维码
      const paramsString = JSON.stringify(this.dataForm)
      this.qrCodeImage = await QRCode.toDataURL(paramsString);
    } catch (error) {
    	console.error('生成二维码失败:', error);
    }
    // 小程序
    const qr = new UQRCode();
    qr.data = JSON.stringify(this.dataForm);
    qr.size = 200;
    qr.make();
    const ctx = uni.createCanvasContext('qrcode', this); 
    	// 组件内调用需传this,vue3 中 this 为 getCurrentInstance()?.proxy
    qr.canvasContext = ctx;
    qr.drawCanvas();
    
    this.loading = false;
    this.marketUnit = "营销单位:";
	},
	saveOrShare() {
  	// 长按保存或分享
  },
}
css 复制代码
.generate-code {
  height: 100px;
  margin-top: 20%;
}
.agree-btn {
  color: #fff; 
  border-color:#000; 
  border-radius: 50rpx;
  width: 90%; 
  height: 76rpx; 
  font-size: 28rpx;
}
.content {
  background-color: #fffdb7;
  height: 100%;
  display: flex;
  flex-direction: column; /* 子元素垂直排列 */
  justify-content: center;
  align-items: center;
}
.item-content {
  background-color: #fff;
  width: 80%;
  height: 70%;
}
.item-content-title {
  margin-top: 7px; 
  margin-left: 20px;
}
.item-content-title-image {
  margin-top: 10px;
  margin-left: 20px;
  width: 200px; 
  height: 200px;
}
.item-content-button {
  width: 80%;
  height: 10%;
  background-color: #fff;
  color: #bc7a00;
  text-align: center;
  margin-top: 20px;
  font-size: 16px;
  /* text-shadow: 0.5px 0.5px 0.5px #000000; /* 字体阴影 */ 
}

效果:打码了

相关推荐
汤姆丁111122 分钟前
直播互动小程序端&Web运营端接入指南
小程序
TiAmo zhang23 分钟前
微信小程序开发案例 | 通讯录小程序(上)
微信小程序·小程序·notepad++
imHere·24 分钟前
小程序强制页面使用skyline
小程序·skyline
AI大法师2 小时前
证件照如何制作?方法来了:一寸、二寸标准证件照、旅游签证、职业资格考试都可
微信小程序
2501_915909066 小时前
网络调试工具推荐 Fiddler抓包工具使用教程与代理设置详解(HTTP/HTTPS配置与实战技巧)
网络·http·ios·小程序·fiddler·uni-app·webview
老师可可8 小时前
成绩发布工具使用方法,附成绩分析教程
学习·信息可视化·小程序·excel·学习方法
sen_shan9 小时前
《微信小程序》第八章:“我的“设计
微信小程序·小程序
咸虾米14 小时前
在unicloud的云对象中如何调用同一服务空间内的另外其他云对象
javascript·微信小程序·前端框架
weixin_1772972206914 小时前
盲盒小程序系统开发:助力品牌拓展新市场
小程序·盲盒
一 乐14 小时前
口腔健康系统|口腔医疗|基于java和小程序的口腔健康系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·小程序·毕设