diff --git a/src/api/db.js b/src/api/db.js index d617d55..5479d09 100644 --- a/src/api/db.js +++ b/src/api/db.js @@ -15,28 +15,42 @@ login(); const db = app.database(); const _ = db.command -const counter = db.collection('QRCounter'); -export function update(value) { - counter.where({ +export function increaseDownloadData(value, date) { + db.collection('QRCounter').where({ value: _.eq(value) }).get().then(res => { if (res.data.length > 0) { - counter.where({ + db.collection('QRCounter').where({ value: _.eq(value) }).update({ - count: _.inc(1) + count: _.inc(1), + date: date }).then(res => { console.log(res) }) } else { - counter.add({ + db.collection('QRCounter').add({ value: value, - count: 1 + count: 1, + date: date }).then(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) + }) +} diff --git a/src/components/Qrcode.js b/src/components/Qrcode.js index 9804cdf..0795dd5 100644 --- a/src/components/Qrcode.js +++ b/src/components/Qrcode.js @@ -18,6 +18,7 @@ import QrRendererRandRect from "./QrRendererRandRect"; import QrRendererDSJ from "./QrRendererDSJ"; import QrRenderer25D from "./QrRenderer25D"; import QrRendererImage from "./QrRendererImage"; +import {recordDownloadDetail} from "../api/db"; const logoStyle = { background: `url(${logo})`, @@ -49,7 +50,8 @@ class Qrcode extends React.Component { qrcode: null, paramInfo: [], paramValue: [], - correctLevel: 0 + correctLevel: 0, + history: [] }; this.paramInfoBuffer = 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) => { - const style = styleList[this.state.selectedIndex] - const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[this.state.selectedIndex]}) + const selected = 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)) + 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) => { - const style = styleList[this.state.selectedIndex] - const el = React.createElement(style.renderer, {qrcode: this.state.qrcode, params: this.state.paramValue[this.state.selectedIndex]}) + const selected = 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) + 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) => { @@ -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() { return (
@@ -187,7 +221,7 @@ class Qrcode extends React.Component { })} text={this.state.text} selected={index == this.state.selectedIndex} - onSelected={() => this.setState({selectedIndex: index})} + onSelected={this.changeStyle} /> }) } diff --git a/src/utils/downloader.js b/src/utils/downloader.js index 78af1d1..75a0548 100644 --- a/src/utils/downloader.js +++ b/src/utils/downloader.js @@ -1,4 +1,4 @@ -import {insert, update} from "../api/db"; +import {increaseDownloadData} from "../api/db"; const svgHead = "\n " + "\n" @@ -18,7 +18,7 @@ export function saveSvg(value, content) { a.hidden = true a.click() - update(value) + increaseDownloadData(value, new Date().toString()) } export function saveImg(value, content, width, height) { @@ -60,8 +60,9 @@ export function saveImg(value, content, width, height) { a.setAttribute('target', 'download') a.setAttribute('download', filename); a.click(); - update(value) }; img.setAttribute('src', 'data:image/svg+xml;base64,' + btoa(svgData)); + + increaseDownloadData(value, new Date().toString()) }