二维码生成及展示
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

173 lines
4.6 KiB

'use strict';
const Hex = require('crypto-js/enc-hex');
const MD5 = require('crypto-js/md5');
async function getPublicityInfo(ctx, next) {
try {
const models = ctx.fs.dc.models;
const { limit, page, name, label } = ctx.query
let findOption = {
where: {},
order: [['time', 'DESC']],
include: [
{
model: models.Qrcode,
},
{
model: models.QrcodeLabels,
attributes: ["name"],
},
{
model: models.QrcodeFiles,
},
],
distinct: true
}
if (limit) {
findOption.limit = Number(limit)
}
if (page && limit) {
findOption.offset = page * limit
}
// if (startTime && endTime) {
// findOption.where.time = { $between: [startTime, endTime] };
// }
if (name) {
findOption.where.name = { $like: `%${name}%` }
}
const res = await models.PublicityInfo.findAndCountAll(findOption)
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取宣传信息失败"
}
}
}
async function createPublicityInfo(ctx, next) {
const models = ctx.fs.dc.models;
const transaction = await ctx.fs.dc.orm.transaction();
try {
const body = ctx.request.body;
const { type, name, labels, qrcode, files } = body;
const dbQrcode = await models.Qrcode.create({
name, type, url: qrcode.url, key: qrcode.key, logo: qrcode.logo,
}, { transaction });
const dbPublicityInfo = await models.PublicityInfo.create({
...body,
qrcodeId: dbQrcode.id,
time: new Date().getTime(),
}, { transaction });
if (labels && labels.length) {
let labelIds = []
let createLabels = [];
for (const l of labels) {
if (typeof l === 'string') {
createLabels.push({ name: l });
} else {
labelIds.push(l);
}
}
if (createLabels.length) {
const dbLabels = await models.QrcodeLabels.bulkCreate(createLabels, { returning: true, transaction });
labelIds = labelIds.concat(dbLabels.map(l => l.id));
}
if (labelIds.length) {
await models.QrcodeLabelsQrcode.bulkCreate(labelIds.map(l => ({
labelId: l, qrcodeId: dbQrcode.id, publicityInfoId: dbPublicityInfo.id
})), { transaction });
}
}
if (type !== '链接') {
if (!files || !files.length) throw new Error('缺少请求参数: files');
await models.QrcodeFiles.bulkCreate(files.map(f => ({
fileName: f.customFileName ? f.customFileName : f.name.split('.').slice(0, -1).join('.'),
fileSize: f.size,
fileUrl: f.storageUrl,
previewImgUrl: f.previewImgUrl,
qrcodeId: dbQrcode.id,
publicityInfoId: dbPublicityInfo.id,
})), { transaction });
}
ctx.status = 204;
ctx.body = { message: '新建宣传信息成功' };
await transaction.commit();
} catch (error) {
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '新建宣传信息失败' };
}
}
async function updatePublicityInfo(ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const body = ctx.request.body;
await models.Department.update(
body,
{ where: { id: id } }
)
ctx.status = 204;
ctx.body = { message: '修改部门成功' }
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '修改部门失败' }
}
}
async function delPublicityInfo(ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
let errMsg = "删除宣传信息失败";
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const { qrcodeId } = ctx.query;
await models.QrcodeLabelsQrcode.destroy({
where: { publicityInfoId: id },
transaction
});
await models.QrcodeFiles.destroy({
where: { publicityInfoId: id },
transaction
});
await models.PublicityInfo.destroy({
where: { id: id },
transaction: transaction,
});
await models.Qrcode.destroy({
where: { id: qrcodeId },
transaction
});
ctx.status = 204;
ctx.body = { message: '删除宣传信息成功' }
transaction.commit();
} catch (error) {
transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: error }
}
}
module.exports = {
getPublicityInfo,
createPublicityInfo,
updatePublicityInfo,
delPublicityInfo,
}