生成小程序码
接口:wxacode.get
通过该接口生成的小程序码,永久有效,有数量限制
HTTPS 调用:POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
提交桌号到数据库
实现思路:
获取请求内容(桌号)
验证桌号是否已经存在
通过 HTTPS 调用接口生成该桌号的小程序码
// router/qr-code/code.js
// 添加桌号
// 省略部分代码
router.post('/qrcode', new Auth().m, async ctx => {
let {table} = ctx.request.body
new postcode(ctx, table).start()
let time = moment().utcOffset(8).format('YYYY-MM-DD HH:mm:ss')
try {
const query = `db.collection('table_qr_code').where({table:'${table}'}).get()`
const res = await new getToken().posteve(TripUrl, query)
if (res.data.length > 0) {
new result(ctx, '该桌号已存在', 202).answer()
} else {
let res_code = await new getToken().qrcode(table)
const res_img = await buffer(Code(), res_code.data)
let code_image = 'https://' + res_img
let table_data = `db.collection('table_qr_code').add({data:{time:'${time}',table:'${table}',code:'${code_image}'}})`
await new getToken().posteve(AddUrl, table_data)
new result(ctx, '添加成功').answer()
}
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})
// config/code-img.js
// 给二进制图片重新命名
let Code = function () {
var orderCode = ''
for (var i = 0; i < 6; i++) {
orderCode += Math.floor(Math.random() * 10)
}
orderCode = new Date().getTime() + orderCode
return orderCode + '.jpg'
}
module.exports = {Code}
// config/checking.js
// 桌号为空校验
// 省略部分代码
class postcode extends checking {
start() {
let arr = ['桌号不能为空']
super.Errunder()
super.Parameter(arr)
super.Blank(arr)
}
}
// config/databaseapi.js
// 生成小程序码
class getToken {
// 省略部分代码
async qrcode(number) {
let token = await this.gettoken()
let OBJ = JSON.stringify({path: 'pages/index/index?number=' + number})
try {
let colldata = await axios.post(Qrcode + token, OBJ, {responseType: 'arraybuffer'})
return colldata
} catch (e) {
throw new result(e, 500)
}
}
}
// cos/cos.js
// 二进制上传
// 省略部分代码
let buffer = function (filename, path) {
return new Promise((resolve, reject) => {
cos.putObject({
Bucket,
Region,
Key: filename,
Body: Buffer.from(path)
}).then(res => {
resolve(res.Location)
}).catch(err => {
reject(err)
})
})
}
请求桌号数据
// router/qr-code/code.js
// 获取桌号
// 省略部分代码
router.get('/getqrcode', new Auth().m, async ctx => {
let {page} = ctx.query
let sk = Number(page) * 10
const query = `db.collection('table_qr_code').orderBy('time', 'desc').limit(10).skip(${sk}).get()`
try {
const res = await new getToken().posteve(TripUrl, query)
const data = res.data.map(item => JSON.parse(item))
const total = {total: res.pager.Total}
const array = {...{result: data}, ...total}
new result(ctx, 'SUCCESS', 200, array).answer()
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})
查询前7天数据
// router/qr-code/code.js
// 获取前7天数据
// 省略部分代码
router.get('/salesvolume', new Auth().m, async ctx => {
try {
let arr = [6, 5, 4, 3, 2, 1, 0]
let catedays = arr.map((item)=>{
return moment().utcOffset(8).subtract(item,'days').format('YYYY-MM-DD')
})
let str = JSON.stringify(catedays)
const query = `db.collection('seven_day_sales').where({time:db.command.in(${str})}).orderBy('time','asc').field({time:true,sales_value:true}).get()`
const res = await new getToken().posteve(TripUrl, query)
const data = res.data.map(item => {
return {
sales_value: JSON.parse(item).sales_value,
time: JSON.parse(item).time,
unix: moment(JSON.parse(item).item).unix()
}
})
let days = catedays.map(item => {
return {
sales_value: 0,
time: item,
unix: moment(item).unix()
}
})
let ab = {}
let obj ={}
let removal = [...data, ...days].reduce((prev, item) => {
if (!obj[item.time]) {
prev.push(item)
obj[item.time] = true
}
return prev
}, [])
let res_sort = removal.sort((A, B) => {
return (A.unix - B.unix)
})
new result(ctx, 'SUCCESS', 200, res_sort).answer()
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})