[Record more data]

This commit is contained in:
CPunisher 2020-05-09 00:34:31 +08:00
parent 1764949de4
commit f584e47952
3 changed files with 65 additions and 16 deletions

View File

@ -15,28 +15,42 @@ login();
const db = app.database(); const db = app.database();
const _ = db.command const _ = db.command
const counter = db.collection('QRCounter');
export function update(value) { export function increaseDownloadData(value, date) {
counter.where({ db.collection('QRCounter').where({
value: _.eq(value) value: _.eq(value)
}).get().then(res => { }).get().then(res => {
if (res.data.length > 0) { if (res.data.length > 0) {
counter.where({ db.collection('QRCounter').where({
value: _.eq(value) value: _.eq(value)
}).update({ }).update({
count: _.inc(1) count: _.inc(1),
date: date
}).then(res => { }).then(res => {
console.log(res) console.log(res)
}) })
} }
else { else {
counter.add({ db.collection('QRCounter').add({
value: value, value: value,
count: 1 count: 1,
date: date
}).then(res => { }).then(res => {
console.log(res) console.log(res)
}) })
} }
}) })
} }
export function recordDownloadDetail({text, value, type, params, history}) {
db.collection('QRDownloadData').add({
date: new Date().toString(),
text: text,
value: value,
type: type,
params: params,
history: history
}).then(res => {
console.log(res)
})
}

View File

@ -18,6 +18,7 @@ import QrRendererRandRect from "./QrRendererRandRect";
import QrRendererDSJ from "./QrRendererDSJ"; import QrRendererDSJ from "./QrRendererDSJ";
import QrRenderer25D from "./QrRenderer25D"; import QrRenderer25D from "./QrRenderer25D";
import QrRendererImage from "./QrRendererImage"; import QrRendererImage from "./QrRendererImage";
import {recordDownloadDetail} from "../api/db";
const logoStyle = { const logoStyle = {
background: `url(${logo})`, background: `url(${logo})`,
@ -49,7 +50,8 @@ class Qrcode extends React.Component {
qrcode: null, qrcode: null,
paramInfo: [], paramInfo: [],
paramValue: [], paramValue: [],
correctLevel: 0 correctLevel: 0,
history: []
}; };
this.paramInfoBuffer = new Array(16).fill(new Array(16)); this.paramInfoBuffer = new Array(16).fill(new Array(16));
this.paramValueBuffer = new Array(16).fill(new Array(16)); this.paramValueBuffer = new Array(16).fill(new Array(16));
@ -90,15 +92,41 @@ class Qrcode extends React.Component {
} }
downloadSvg = (e) => { downloadSvg = (e) => {
const style = styleList[this.state.selectedIndex] const selected = this.state.selectedIndex
const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[this.state.selectedIndex]}) const style = styleList[selected]
const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[selected]})
saveSvg(style.value, ReactDOMServer.renderToString(el)) saveSvg(style.value, ReactDOMServer.renderToString(el))
recordDownloadDetail({
text: this.state.text,
value: styleList[selected],
type: 'svg',
params: this.state.paramInfo[selected].map((item, index) => {
return {
key: item.key,
value: item.choices ? item.choices[this.state.paramValue[selected][index]] : this.state.paramValue[selected][index]
}
}),
history: this.state.history
});
} }
downloadImg = (e) => { downloadImg = (e) => {
const style = styleList[this.state.selectedIndex] const selected = this.state.selectedIndex
const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[this.state.selectedIndex]}) const style = styleList[selected]
const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[selected]})
saveImg(style.value, ReactDOMServer.renderToString(el), 1500, 1500) saveImg(style.value, ReactDOMServer.renderToString(el), 1500, 1500)
recordDownloadDetail({
text: this.state.text,
value: styleList[selected],
type: 'jpg',
params: this.state.paramInfo[selected].map((item, index) => {
return {
key: item.key,
value: item.choices ? item.choices[this.state.paramValue[selected][index]] : this.state.paramValue[selected][index]
}
}),
history: this.state.history
});
} }
renderParamEditor = (info, index) => { renderParamEditor = (info, index) => {
@ -150,6 +178,12 @@ class Qrcode extends React.Component {
} }
} }
changeStyle = (index) => {
const newHistory = this.state.history.slice();
newHistory.push(styleList[index].value);
this.setState({selectedIndex: index, history: newHistory})
}
render() { render() {
return ( return (
<div className="Qr-outer"> <div className="Qr-outer">
@ -187,7 +221,7 @@ class Qrcode extends React.Component {
})} })}
text={this.state.text} text={this.state.text}
selected={index == this.state.selectedIndex} selected={index == this.state.selectedIndex}
onSelected={() => this.setState({selectedIndex: index})} onSelected={this.changeStyle}
/> />
}) })
} }

View File

@ -1,4 +1,4 @@
import {insert, update} from "../api/db"; import {increaseDownloadData} from "../api/db";
const svgHead = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n " + const svgHead = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n " +
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n" "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
@ -18,7 +18,7 @@ export function saveSvg(value, content) {
a.hidden = true a.hidden = true
a.click() a.click()
update(value) increaseDownloadData(value, new Date().toString())
} }
export function saveImg(value, content, width, height) { export function saveImg(value, content, width, height) {
@ -60,8 +60,9 @@ export function saveImg(value, content, width, height) {
a.setAttribute('target', 'download') a.setAttribute('target', 'download')
a.setAttribute('download', filename); a.setAttribute('download', filename);
a.click(); a.click();
update(value)
}; };
img.setAttribute('src', 'data:image/svg+xml;base64,' + btoa(svgData)); img.setAttribute('src', 'data:image/svg+xml;base64,' + btoa(svgData));
increaseDownloadData(value, new Date().toString())
} }