1.获取所有轮播图、
// 处理轮播图
const handleDB = require('../handleDB/index')
// 获取所有轮播图
exports.allCarousel = (req, res) => {
(async function () {
let results = await handleDB(res, "book_carousel", "find", "查询数据出错!", { where: `status=0` });
if (results.length < 1) {
res.send({ code: 0, message: '轮播图列表为空' })
}
const total = results.length;
res.send({ code: 200, message: '获取轮播图成功', data: results, total: total })
})()
}
2.添加或者更新轮播图
(用cdn修改图片)先不写
// 导入数据库操作模块
const db = require('../db/index')
//引入七牛文件
const upload_file=require('../utils/qn')
const constant=require('../utils/constant');
//添加轮播图
exports.addCarousel = (req, res) => {
const { id } = req.body;
if (id) {
const sql = `update info_carousel set ? where id=${id}`
db.query(sql, [req.body, req.body.id], (err, results) => {
// 执行 SQL 语句失败
if (err) return res.cc(err)
// SQL 语句执行成功,但是影响行数不等于 1
if (results.affectedRows !== 1) return res.cc('更新轮播图失败!')
// 更新轮播图成功
res.cc('更新轮播图成功!', 0)
})
} else {
const sql = `insert into info_carousel set ?`
db.query(sql, req.body, (err, results) => {
// SQL 语句执行失败
if (err) return res.cc(err)
// SQL 语句执行成功,但是影响行数不等于 1
if (results.affectedRows !== 1) return res.cc('新增轮播图失败!')
// 新增轮播图成功
res.cc('新增轮播图成功!', 0)
})
}
}
3.根据id删除轮播图
// 根据id删除轮播图
exports.delCarousel = (req, res) => {
(async function () {
// console.log(11);
const id = req.params.id;
if (!id) {
res.send({ code: 0, message: '获取参数为空' })
}
// 3根据id查询数据库是否存在该轮播图
let result = await handleDB(res, "book_carousel", "find", "book_carousel查询出错", {
where: `id = ${id} AND status = 0`
});
console.log(result);
// 4 检查指定 id 的轮播图是否存在
if (result.length !== 1) return res.send({ code: 0, message: '该轮播图不存在!' })
let results = await handleDB(res, "book_carousel", "update", "查询数据出错!", `id= ${id}`, { status: 1 });
// 6 SQL 语句执行成功,但是影响行数不等于 1
if (results.affectedRows !== 1) return res.send({ code: 0, message: '删除轮播图失败!' })
res.send({ code: 200, message: '删除轮播图成功', });
})()
}
Model.prototype.find = function (options, callback) {
if (!isConnect) {
console.log(options.constructor);
this.connect(err => {
isConnect = true;
var str = '';
if (!callback) {
str = `select * from ${this.name}`;
callback = options;
} else if (options.constructor == Array) {
str = `select ${options.join()} from ${this.name}`;
console.log(str, '000');
} else {
str = `select * from ${this.name} where ${options.where}`;
console.log(str, '111');
}
console.log(str);
connection.query(str, (error, results, fields) => {
callback(error, results, fields);
});
return this;
})
} else {
var str = '';
if (!callback) {
str = `select * from ${this.name}`;
callback = options;
} else if (options.constructor == Array) {
str = `select ${options.join()} from ${this.name}`;
} else {
str = `select * from ${this.name} where ${options.where}`;
console.log(str, '333');
};
//console.log(str);
connection.query(str, (error, results, fields) => {
callback(error, results, fields);
});
return this;
}
};
修该了这两个地方(传入对象)这种格式
{
where: `id = ${id} AND status = 0`
}