feat:转账插件
This commit is contained in:
@@ -18,6 +18,7 @@ let QRCode = {};
|
||||
return [c0, c1];
|
||||
// 3 bytes
|
||||
} else {
|
||||
|
||||
c0 = 224 + (code >> 12);
|
||||
c1 = 128 + (code >> 6 & 63);
|
||||
c2 = 128 + (code & 63);
|
||||
@@ -1022,21 +1023,25 @@ let QRCode = {};
|
||||
QRCode = function (opt) {
|
||||
//设置默认参数
|
||||
this.options = {
|
||||
text: '',
|
||||
size: 256,
|
||||
correctLevel: 3,
|
||||
background: '#ffffff',
|
||||
foreground: '#000000',
|
||||
text: '',
|
||||
size: 256,
|
||||
correctLevel: 3,
|
||||
quietZone: 0,
|
||||
background: '#ffffff',
|
||||
foreground: '#000000',
|
||||
pdground: '#000000',
|
||||
image: '',
|
||||
imageSize: 30,
|
||||
canvasId: opt.canvasId,
|
||||
nvueContext: opt.nvueContext,
|
||||
context: opt.context,
|
||||
canvasId: opt.canvasId,
|
||||
ctx: opt.ctx,
|
||||
canvasHost: opt.canvasHost,
|
||||
isNvue: opt.isNvue,
|
||||
vuectx: opt.vuectx,
|
||||
usingComponents: opt.usingComponents,
|
||||
showLoading: opt.showLoading,
|
||||
loadingText: opt.loadingText,
|
||||
};
|
||||
|
||||
if (typeof opt === 'string') { // 只编码ASCII字符串
|
||||
opt = {
|
||||
text: opt
|
||||
@@ -1083,8 +1088,14 @@ let QRCode = {};
|
||||
}
|
||||
return options.foreground;
|
||||
}
|
||||
|
||||
// 创建canvas
|
||||
let createCanvas = function (options) {
|
||||
let createCanvas = async function (options) {
|
||||
let isApp = false;
|
||||
// #ifdef APP
|
||||
isApp = true;
|
||||
// #endif
|
||||
|
||||
if(options.showLoading){
|
||||
uni.showLoading({
|
||||
title: options.loadingText,
|
||||
@@ -1092,48 +1103,58 @@ let QRCode = {};
|
||||
});
|
||||
}
|
||||
var ctx = '';
|
||||
if (options.nvueContext) {
|
||||
ctx = options.nvueContext;
|
||||
} else {
|
||||
ctx = uni.createCanvasContext(options.canvasId, options.context);
|
||||
}
|
||||
var count = qrCodeAlg.getModuleCount();
|
||||
var ratioSize = options.size;
|
||||
var ratioImgSize = options.imageSize;
|
||||
//计算每个点的长宽
|
||||
var tileW = (ratioSize / count).toPrecision(4);
|
||||
var tileH = (ratioSize / count).toPrecision(4);
|
||||
//绘制
|
||||
for (var row = 0; row < count; row++) {
|
||||
for (var col = 0; col < count; col++) {
|
||||
var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
|
||||
var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW));
|
||||
var foreground = getForeGround({
|
||||
row: row,
|
||||
col: col,
|
||||
count: count,
|
||||
options: options
|
||||
});
|
||||
ctx.setFillStyle(qrCodeAlg.modules[row][col] ? foreground : options.background);
|
||||
ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h);
|
||||
}
|
||||
ctx = options.ctx;
|
||||
|
||||
var count = qrCodeAlg.getModuleCount();
|
||||
var quietZone = Math.max(0, Math.floor(options.quietZone || 0));
|
||||
var renderCount = count + quietZone * 2;
|
||||
var ratioSize = options.size;
|
||||
var ratioImgSize = options.imageSize;
|
||||
//计算每个点的长宽
|
||||
var tileW = (ratioSize / renderCount).toPrecision(4);
|
||||
var tileH = (ratioSize / renderCount).toPrecision(4);
|
||||
//绘制
|
||||
for (var row = 0; row < renderCount; row++) {
|
||||
for (var col = 0; col < renderCount; col++) {
|
||||
var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
|
||||
var h = (Math.ceil((row + 1) * tileH) - Math.floor(row * tileH));
|
||||
var qrRow = row - quietZone;
|
||||
var qrCol = col - quietZone;
|
||||
var isDark = qrRow >= 0 && qrCol >= 0 && qrRow < count && qrCol < count && qrCodeAlg.modules[qrRow][qrCol];
|
||||
var foreground = getForeGround({
|
||||
row: qrRow,
|
||||
col: qrCol,
|
||||
count: count,
|
||||
options: options
|
||||
});
|
||||
setFillStyle(ctx, isDark ? foreground : options.background);
|
||||
ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h);
|
||||
}
|
||||
}
|
||||
if (options.image) {
|
||||
var x = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
|
||||
var y = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
|
||||
drawRoundedRect(ctx, x, y, ratioImgSize, ratioImgSize, 2, 6, true, true)
|
||||
ctx.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
|
||||
// 画圆角矩形
|
||||
function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
|
||||
ctxi.setLineWidth(lineWidth);
|
||||
ctxi.setFillStyle(options.background);
|
||||
ctxi.setStrokeStyle(options.background);
|
||||
ctxi.beginPath(); // draw top and top right corner
|
||||
await options.vuectx.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
|
||||
// 画圆角矩形
|
||||
function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
|
||||
setLineWidth(ctxi, lineWidth);
|
||||
setFillStyle(ctxi, options.background);
|
||||
setStrokeStyle(ctxi, options.background);
|
||||
ctxi.beginPath(); // draw top and top right corner
|
||||
ctxi.moveTo(x + r, y);
|
||||
ctxi.arcTo(x + width, y, x + width, y + r, r); // draw right side and bottom right corner
|
||||
ctxi.arcTo(x + width, y + height, x + width - r, y + height, r); // draw bottom and bottom left corner
|
||||
ctxi.arcTo(x, y + height, x, y + height - r, r); // draw left and top left corner
|
||||
ctxi.arcTo(x, y, x + r, y, r);
|
||||
ctxi.lineTo(x + width, y); // move to top-right corner
|
||||
ctxi.arc(x + width - r, y + r, r, -Math.PI / 2, 0); // draw right side and bottom right corner
|
||||
|
||||
ctxi.lineTo(x + width, y + height - r);
|
||||
ctxi.arc(x + width - r, y + height - r, r, 0, Math.PI / 2); // draw bottom and bottom left corner
|
||||
|
||||
ctxi.lineTo(x + r, y + height);
|
||||
ctxi.arc(x + r, y + height - r, r, Math.PI / 2, Math.PI);// draw left and top left corner
|
||||
|
||||
ctxi.lineTo(x, y + r);
|
||||
ctxi.arc(x + r, y + r, r, Math.PI, Math.PI * 3 / 2);
|
||||
|
||||
ctxi.closePath();
|
||||
if (fill) {
|
||||
ctxi.fill();
|
||||
@@ -1144,62 +1165,61 @@ let QRCode = {};
|
||||
}
|
||||
}
|
||||
setTimeout(() => {
|
||||
ctx.draw(true, () => {
|
||||
// 保存到临时区域
|
||||
setTimeout(() => {
|
||||
if (options.nvueContext) {
|
||||
ctx.toTempFilePath(
|
||||
0,
|
||||
0,
|
||||
options.width,
|
||||
options.height,
|
||||
options.width,
|
||||
options.height,
|
||||
"",
|
||||
1,
|
||||
function(res) {
|
||||
if (options.cbResult) {
|
||||
options.cbResult(res.tempFilePath)
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
uni.canvasToTempFilePath({
|
||||
width: options.width,
|
||||
height: options.height,
|
||||
destWidth: options.width,
|
||||
destHeight: options.height,
|
||||
canvasId: options.canvasId,
|
||||
quality: Number(1),
|
||||
success: function (res) {
|
||||
if (options.cbResult) {
|
||||
// 由于官方还没有统一此接口的输出字段,所以先判定下 支付宝为 res.apFilePath
|
||||
if (!empty(res.tempFilePath)) {
|
||||
options.cbResult(res.tempFilePath)
|
||||
} else if (!empty(res.apFilePath)) {
|
||||
options.cbResult(res.apFilePath)
|
||||
} else {
|
||||
options.cbResult(res.tempFilePath)
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
if (options.cbResult) {
|
||||
options.cbResult(res)
|
||||
}
|
||||
},
|
||||
complete: function () {
|
||||
uni.hideLoading();
|
||||
},
|
||||
}, options.context);
|
||||
}
|
||||
}, options.text.length + 100);
|
||||
});
|
||||
}, options.usingComponents ? 0 : 150);
|
||||
// canvas2 绘制是自动的不需要手动绘制
|
||||
if(options.isNvue || isApp){
|
||||
const exportCode = () => {
|
||||
// 保存到临时区域
|
||||
setTimeout(() => {
|
||||
if (options.canvasHost && typeof options.canvasHost.toTempFilePath === 'function') {
|
||||
options.canvasHost.toTempFilePath({
|
||||
width: options.width || options.size,
|
||||
height: options.height || options.size,
|
||||
destWidth: options.width || options.size,
|
||||
destHeight: options.height || options.size,
|
||||
quality: Number(1),
|
||||
success: function (res) {
|
||||
if (options.cbResult) {
|
||||
if (!empty(res.tempFilePath)) {
|
||||
options.cbResult(res.tempFilePath)
|
||||
} else if (!empty(res.apFilePath)) {
|
||||
options.cbResult(res.apFilePath)
|
||||
} else {
|
||||
options.cbResult(res.tempFilePath)
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
console.log('绘制失败', res)
|
||||
if (options.cbResult) {
|
||||
options.cbResult(res)
|
||||
}
|
||||
},
|
||||
complete: function () {
|
||||
uni.hideLoading();
|
||||
},
|
||||
}).catch(function(res) {
|
||||
console.log('绘制失败', res)
|
||||
});
|
||||
}
|
||||
}, options.text.length + 100);
|
||||
};
|
||||
if (options.canvasHost && typeof options.canvasHost.draw === 'function') {
|
||||
options.canvasHost.draw(true, exportCode);
|
||||
} else if (ctx && typeof ctx.draw === 'function') {
|
||||
ctx.draw(true, exportCode);
|
||||
} else {
|
||||
exportCode();
|
||||
}
|
||||
}
|
||||
else{
|
||||
options.cbResult("")
|
||||
}
|
||||
|
||||
}, options.usingComponents ? 100 : 200);
|
||||
}
|
||||
createCanvas(this.options);
|
||||
// 空判定
|
||||
let empty = function (v) {
|
||||
let empty = function (v) {
|
||||
let tp = typeof v,
|
||||
rt = false;
|
||||
if (tp == "number" && String(v) == "") {
|
||||
@@ -1213,17 +1233,38 @@ let QRCode = {};
|
||||
} else if (tp == "function") {
|
||||
rt = false
|
||||
}
|
||||
return rt
|
||||
}
|
||||
};
|
||||
QRCode.prototype.clear = function (fn) {
|
||||
var ctx = '';
|
||||
if (options.nvueContext) {
|
||||
ctx = options.nvueContext;
|
||||
} else {
|
||||
uni.createCanvasContext(this.options.canvasId, this.options.context)
|
||||
}
|
||||
ctx.clearRect(0, 0, this.options.size, this.options.size)
|
||||
return rt
|
||||
}
|
||||
function setFillStyle(ctx, value) {
|
||||
if (ctx && typeof ctx.setFillStyle === 'function') {
|
||||
ctx.setFillStyle(value);
|
||||
} else if (ctx) {
|
||||
ctx.fillStyle = value;
|
||||
}
|
||||
}
|
||||
function setStrokeStyle(ctx, value) {
|
||||
if (ctx && typeof ctx.setStrokeStyle === 'function') {
|
||||
ctx.setStrokeStyle(value);
|
||||
} else if (ctx) {
|
||||
ctx.strokeStyle = value;
|
||||
}
|
||||
}
|
||||
function setLineWidth(ctx, value) {
|
||||
if (ctx && typeof ctx.setLineWidth === 'function') {
|
||||
ctx.setLineWidth(value);
|
||||
} else if (ctx) {
|
||||
ctx.lineWidth = value;
|
||||
}
|
||||
}
|
||||
};
|
||||
QRCode.prototype.clear = function (fn) {
|
||||
if (this.options.canvasHost && typeof this.options.canvasHost.clearCanvas === 'function') {
|
||||
this.options.canvasHost.clearCanvas()
|
||||
if (fn) fn()
|
||||
return
|
||||
}
|
||||
var ctx = this.options.ctx;
|
||||
ctx.clearRect(0, 0, this.options.size, this.options.size)
|
||||
ctx.draw(false, () => {
|
||||
if (fn) {
|
||||
fn()
|
||||
@@ -1232,4 +1273,4 @@ let QRCode = {};
|
||||
};
|
||||
})()
|
||||
|
||||
export default QRCode
|
||||
export default QRCode
|
||||
|
||||
Reference in New Issue
Block a user