如圖:
需要達到這樣效果,數字轉成條形碼或者二維碼,步驟如下:
1、$ npm install wxbarcode或者從Github上下載包https://github.com/alsey/wxbarcode
目錄結構如下:
注意下載下來之後,需要修改code/index.js文件
utils/code/index.js文件如下:
var barcode = require('./barcode');
var qrcode = require('./qrcode');
function convert_length(length) {
//改爲支付寶小程序的方法my.getSystemInfoSync()
return Math.round(my.getSystemInfoSync().windowWidth * length / 750);
}
function barc(id, code, width, height) {
// 改爲支付寶小程序的方法my.createCanvasContext(id)
barcode.code128(my.createCanvasContext(id), code, convert_length(width), convert_length(height))
}
function qrc(id, code, width, height) {
// 改爲支付寶小程序的方法my.createCanvasContext(id)
qrcode.api.draw(code, {
ctx: my.createCanvasContext(id),
width: convert_length(width),
height: convert_length(height)
})
}
module.exports = {
barcode: barc,
qrcode: qrc
}
2、頁面所在的js文件中引入:import {barcode, qrcode} from '../../utils/code/index';
3、調用:
條形碼:
a、調用:頁面所在的js文件需要轉換的地方調用 barcode('barcode', code , 600, 220);參數:第一個參數爲canvas的id,第二個參數爲需要轉碼的字符串,第三個參數爲條形碼的寬度,第四個參數爲條形碼設置的高度
b、所在頁面的axml文件中設置 <canvas id="barcode"></canvas> 此id對應爲barcode的第一個參數
例如:code='19281234123412341'
二維碼:
a、調用:頁面所在的js文件需要轉換的地方調用 qrcode('qrcode', code , 600, 220);參數:第一個參數爲canvas的id,第二個參數爲需要轉碼的字符串,第三個參數爲條形碼的寬度,第四個參數爲條形碼設置的高度
b、所在頁面的axml文件中設置 <canvas id="qrcode"></canvas> 此id對應爲barcode的第一個參數