-
前后端在工作中相互为搭档,又相互鄙视(手动狗头)。
-
后台总觉得前端代码简单,切切图就好了,不用写什么逻辑,切图仔。由于安全性考虑,很多复杂的逻辑都是后台来完成。这个时候我们前端就会反击:后台只会crud,一个sql玩一天,查询出结果还要几十秒,渣渣。
-
当然,这只是一个玩笑,这里就不深究了。今天主要记录一下封装的 crud 公共方法。
这里只是封装了几个逻辑比较简单的 增 删 改 查:
创建:routes/common.js
const { formatDate } = require('./../utils')
/**
* @name: 新增方法
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
const saveMsg = (Model, res, params) => {
new Model(params).save((err) => {
const datas = err ? { isSuccess: false, message: '保存失败', err } : { isSuccess: true, message: '保存成功' }
res.send(datas);
})
}
/**
* @name: 通过id删除
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
const deleteMsg = (Model, req, res) => {
Model.findByIdAndRemove({ _id: req.body._id }, (err) => {
const datas = err ? { isSuccess: false, message: '删除失败', err } : { isSuccess: true, message: '删除成功' }
res.send(datas);
})
}
/**
* @name: 通过id修改
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
const editMsg = (Model, req, res) => {
const { _id } = req.body
const params = req.body
params.updateDate = formatDate()
delete params._id
Model.findByIdAndUpdate({ _id }, params, (err) => {
const datas = err ? { isSuccess: false, message: '修改失败', err } : { isSuccess: true, message: '修改成功' }
res.send(datas);
})
}
/**
* @name: 不带参数的分页查询
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
const queryMsg = (Model, req, res) => {
const { pageSize, pageNo } = req.body
let size = pageSize || 10
let num = pageNo - 1 || 0
Model.find({}, (err, doc) => {
if (err) {
res.send({ isSuccess: false, message: '查询失败' });
} else {
const total = doc.length
Model.find().sort({ _id: -1 }).skip(num * size).limit(size).exec((errs, docs) => {
if (errs) {
res.send({ isSuccess: false, message: '查询失败' });
} else {
res.send({ isSuccess: true, data: docs, total });
}
})
}
})
}
module.exports = {
saveMsg,
deleteMsg,
editMsg,
queryMsg
}
使用 : 通用方法,分别传入引入的Model 和 req , res即可
const expres = require('express')
const router = expres.Router()
const Product = require('./../model/product')
const { saveMsg, deleteMsg, editMsg, queryMsg } = require('./common')
/**
* @name: 增加商品
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
router.post('/addProduct', (req, res, next) => {
const { types, cate, title, description, price, details, images, isSale, salePrice, isHot, isOn, isTui } = req.body
Product.find({ title, types }).exec((err, doc) => {
if (err) {
res.send({ isSuccess: false, message: '添加失败' });
} else {
if (doc.length > 0) {
res.send({ isSuccess: false, message: '请不要重复添加商品' });
} else {
saveMsg(Product, res, { types, cate, title, description, price, details, images, isSale, salePrice, isHot, isOn, isTui })
}
}
})
})
/**
* @name: 删除商品
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
router.post('/deleteProduct', (req, res, next) => {
deleteMsg(Product, req, res)
})
/**
* @name: 编辑商品
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
router.post('/editProduct', (req, res, next) => {
editMsg(Product, req, res)
})
/**
* @name: 查询商品
* @param {type}
* @Author: luoyong/471826078@qq.com
*/
router.get('/queryProduct', (req, res, next) => {
queryMsg(Product, req, res)
})
module.exports = router
参杂其他业务的接口需要另外写,但是也能减少很多粘贴复制的时间。
欢迎评论、点赞、留言