On this page
批量删除文件
控制器:app/controller/file.js
js
// 删除
async delete() {
const { ctx, app } = this;
const user_id = ctx.authUser.id;
ctx.validate({
ids: {
required: true,
type: "string",
desc: "记录"
},
});
let { ids } = ctx.request.body;
ids = ids.split(',');
// 计算删除文件内存
let files = await app.model.File.findAll({
where: {
id: ids,
user_id
}
});
let size = 0;
files.forEach(item => {
size = size + item.size
});
let res = await app.model.File.destroy({
where: {
id: ids,
user_id
}
});
if (res) {
// 减去使用内存
size = ctx.authUser.used_size - size;
ctx.authUser.used_size = size > 0 ? size : 0;
ctx.authUser.save();
}
ctx.apiSuccess(res);
}
模型:app/model/file.js
js
// 监听批量删除
File.afterBulkDestroy(async (data, option) => {
console.log("批量删除后", data.where.id);
if (data.where.id) {
app.model.File.destroy({
where: {
file_id: data.where.id,
user_id: data.where.user_id,
},
});
}
});
路由:app/router.js
js
router.post("/file/delete", controller.file.delete);