微信小程序?qū)崿F(xiàn)預(yù)約生成二維碼功能
業(yè)務(wù)需求:點(diǎn)擊預(yù)約按鈕即可生成二維碼憑碼入校參觀~

一.創(chuàng)建頁面
如下是博主自己寫的wxml:
<swiper indicator-dots indicator-color="white" indicator-active-color="blue" autoplay interval="2000" circular > <!-- 這部分是實(shí)現(xiàn)輪播圖下面的小圓點(diǎn),可以根據(jù)兩個(gè)不同的屬性來分別更改樣式 --> <swiper-item> <image src="/image/1606976870484.jpg"></image> </swiper-item> <swiper-item> <image src="/image/1606976921531.jpg"></image> </swiper-item> <swiper-item> <image src="/image/1606976936035.png"></image> </swiper-item> <swiper-item> <image src="/image/111.jpg"></image> </swiper-item> <swiper-item> <image src="/image/222.jpg"></image> </swiper-item> </swiper> <button class="mybt" bindtap="yuyue">預(yù)約參觀?</button> <canvas type="2d"id="myQrcode"></canvas>
以及wxss:
/* pages/youke/youke.wxss */
page{
background-color:#f3ffff;
}
swiper{
margin-top: 50rpx;
width: 100%;
height: 430rpx;
border-radius: 30rpx;
}
swiper-item {
width: 100%;
height: 100%;
border-radius: 50rpx;
}
.mybt{
margin-top: 100rpx;
width:300rpx;
background-color: rgb(41, 113, 248);
color: rgb(255, 255, 255);
}
view{
font-size: 45rpx;
text-align: center;
margin-top: 100rpx;
}
canvas{
width: 230rpx;
height: 230rpx;
margin-top: 100rpx;
margin-left: 260rpx;
}二.下載并配置第三方庫
去Gitee下載工具包:
二維碼工具包
http://? https://gitee.com/WeiDoctor/weapp-qrcode-canvas-2d
?下載zip:


將dist文件夾中的js文件全部復(fù)制到utils目錄下:

三.完成業(yè)務(wù)
如下代碼必須完整的導(dǎo)入再頁面JS的最頂部:
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
如下是完整的代碼:
// pages/youke/youke.js
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
Page({
data: {
yynum:500,
randomNum:"0"
},
onLoad() {
},
onReady() {
},
onShow() {
},
onHide() {
},
onUnload() {
},
onPullDownRefresh() {
},
onReachBottom() {
},
onShareAppMessage() {
},
yuyue(msg){
// console.log("lll")
if(this.data.yynum>0&&this.data.randomNum=="0")
{
wx.showToast({
icon: 'success',
title: '預(yù)約成功~',
})
let y=this.data.yynum;
y--;
this.setData({
yynum:y
})
let r=(Math.random()*1).toFixed(4)*10000
this.setData({
randomNum:r
}),
console.log(r);
const query = wx.createSelectorQuery()
query.select('#myQrcode')
.fields({
node: true,
size: true
})
.exec((res) => {
var canvas = res[0].node
drawQrcode({
canvas: canvas,
canvasId: 'myQrcode',
width: 260,
padding: 30,
background: '#4169E1',
foreground: '#ffffff',
text: '個(gè)人二維碼信息',
})
wx.canvasToTempFilePath({
canvasId: 'myQrcode',
canvas: canvas,
x: 0,
y: 0,
width: 260,
height: 260,
destWidth: 260,
destHeight: 260,
})
})
}
else if(this.data.randomNum!="0")
{
wx.showToast({
icon: 'error',
title: '禁止重復(fù)預(yù)約~',
})
}
else{
wx.showToast({
icon: 'error',
title: '很抱歉,已無預(yù)約名額~',
})
}
}
})點(diǎn)擊預(yù)約即可成功生成二維碼~

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)預(yù)約生成二維碼的文章就介紹到這了,更多相關(guān)小程序預(yù)約生成二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法
這篇文章主要介紹了window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法,需要的朋友可以參考下2014-08-08
JavaScript中的各種寬高屬性的實(shí)現(xiàn)
這篇文章主要介紹了JavaScript中的各種寬高屬性的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
javascript學(xué)習(xí)筆記之函數(shù)定義
本文主要給大家介紹了javascript的一些函數(shù)定義方面的基礎(chǔ)知識(shí),包括函數(shù)聲明式、函數(shù)表達(dá)式、Function 構(gòu)造函數(shù)等,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06
JavaScript中promise.all和promise.race的區(qū)別詳解
Promise.all和Promise.race是JavaScript的兩種Promise處理方法,Promise.all要求所有Promise對(duì)象成功完成才返回成功,若有一個(gè)失敗則整體失敗,Promise.race返回第一個(gè)完成的Promise結(jié)果,需要的朋友可以參考下2024-09-09
基于JS實(shí)現(xiàn)父組件的請(qǐng)求服務(wù)過程解析
這篇文章主要介紹了基于JS實(shí)現(xiàn)父組件的請(qǐng)求服務(wù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

