業(yè)務需求:點擊預約按鈕即可生成二維碼憑碼入校參觀~
一.創(chuàng)建頁面
如下是博主自己寫的wxml:
<swiper indicator-dots indicator-color="white" indicator-active-color="blue"
autoplay interval="2000" circular
>
<!-- 這部分是實現(xiàn)輪播圖下面的小圓點,可以根據(jù)兩個不同的屬性來分別更改樣式 -->
<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">預約參觀?</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文件全部復制到utils目錄下:
三.完成業(yè)務
如下代碼必須完整的導入再頁面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: '預約成功~',
})
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: '個人二維碼信息',
})
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: '禁止重復預約~',
})
}
else{
wx.showToast({
icon: 'error',
title: '很抱歉,已無預約名額~',
})
}
}
})
點擊預約即可成功生成二維碼~?文章來源:http://www.zghlxwxcb.cn/news/detail-852438.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-852438.html
到了這里,關于微信小程序?qū)崿F(xiàn)預約生成二維碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!