原文:https://blog.csdn.net/xbw12138/article/details/75213274
前端
二维码生成
二维码要求:每分钟刷新一次,模拟了个鸡肋,添加了个按分钟显示的时间加在二维码中,起到刷新的作用,在onshow中每次打开界面就会刷新显示
var QR = require("../../utils/qrcode.js");
var util = require("../../utils/util.js");
data: {
qrMsg: '',
recognizeMsg: '',
isShowMsg: false,
isShowResult: false,
showClear: true,
qrlogin :false,
},
onShow: function () {
var that=this;
this.setData({
isShowMsg: false,
isShowResult: true,
})
// 页面显示,用户已注册
if (getApp().globalData.userSign == 1) {
that.setData({
qrlogin: true,
})
//存储openid
wx.getStorage({
key: 'openid',
success: function (res) {
var openid = res.data;
//加入分钟,每分钟刷新一次二维码
QR.qrApi.draw('wechat@'+openid + '@' + util.formatTimeQrcodeChange(new Date), 'mycanvas', 300, 300)
}
})
} else if (getApp().globalData.userSign == 2){
that.setData({
qrlogin:false,
})
}
},
扫描二维码
recognizeCode: function () {
this.setData({
isShowMsg: true,
isShowResult: false,
recognizeMsg: "",
})
var that = this
//小程序API
wx.scanCode({
//扫描条形码ISBN
success: function (res) {
// success
console.log('https://book.douban.com/subject/' + res.result+'/');
var url = 'https://book.douban.com/subject/' + res.result + '/';
wx.navigateTo({
url: '../desc/desc?url=' + url
})
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
utils.js中添加以下代码
function formatTimeQrcodeChange(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
//按分钟显示,去掉秒
//var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
}
qrcode.js
var QR = (function () {
// alignment pattern
var adelta = [
0, 11, 15, 19, 23, 27, 31,
16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24,
26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28
];
// version block
var vpat = [
0xc94, 0x5bc, 0xa99, 0x4d3, 0xbf6, 0x762, 0x847, 0x60d,
0x928, 0xb78, 0x45d, 0xa17, 0x532, 0x9a6, 0x683, 0x8c9,
0x7ec, 0xec4, 0x1e1, 0xfab, 0x08e, 0xc1a, 0x33f, 0xd75,
0x250, 0x9d5, 0x6f0, 0x8ba, 0x79f, 0xb0b, 0x42e, 0xa64,
0x541, 0xc69
];
// final format bits with mask: level << 3 | mask
var fmtword = [
0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, //L
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, //M
0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed, //Q
0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b //H
];
// 4 per version: number of blocks 1,2; data width; ecc width
var eccblocks = [
1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17,
1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28,
1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22,
1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16,
1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22,
2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28,
2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26,
2, 0, 97, 24, 2, 2, 38, 22, 4,