菜品类目页
前端接口
添加菜品类目:addcategor
获取菜品类目:obtaincate
添加菜品类目
- 校验函数校验是否为空
class catecheck extends checking {
start() {
let arr = ['请输入菜品类目']
super.Errunder()
super.Parameter(arr)
super.Blank(arr)
}
}
- 菜品类目数据库集合设计
[{
cid:'a+时间戳',//分类id
count:number,//该分类下有多少商品
value: '菜品类目',
label: '菜品类目',
sele_quantity:0,//小程序端用户在该分类下添加了多少商品
}]
3.数据库操作语句
const query = `db.collection('dishes-category').where({label:'${category}'}).get()`// 获取数据库指定类目的记录,若有记录则该类目已存在
const cate = `db.collection('dishes-category').add({data:{value:'${category}',label:'${category}',cid:'${cid}',count:0,sele_quantity:0}})`// 向数据库添加菜品类目记录
- 使用前面封装过的
getToken().posteve(dataUrl, query)
接口查询是否已存在,若不存在则向数据库添加记录,反之返回客户端已存在响应
try {
const res = await new getToken().posteve(TripUrl, query)
console.log(res)
if (res.data.length > 0) {
new result(ctx, '该类目已存在', 202).answer()
} else {
await new getToken().posteve(AddUrl, cate)
new result(ctx, '添加成功').answer()
}
} catch (e) {
new result(ctx, '添加失败,服务器发生错误', 500).answer()
}
获取菜品类目
router.get('/obtaincate', new Auth().m, async ctx => {
let {page} = ctx.query
let sk = page * 10
const query = `db.collection('dishes-category').orderBy('cid','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()
}
})
一些补充
ctx.query
获取解析的查询字符串, 当没有查询字符串时,返回一个空对象Collection.orderBy(fieldPath: string, string: order)
指定查询排序条件Collection.limit(value: number)
指定查询结果集数量上限Collection.skip(offset: number)
指定查询返回结果时从指定序列后的结果开始返回,常用于分页
菜品管理页
前端接口
获取菜品单位:obtainunit
添加菜品单位:dishunit
获取菜品:obtaindishes
上架菜品:uploaddishes
下架菜品:fromsale
修改上架的菜品:modifydishes
获取菜品单位
router.get('/obtainunit', new Auth().m, async ctx => {
const query = `db.collection('dishunit').get()`
try {
const res = await new getToken().posteve(TripUrl, query)
const data = res.data.map(item => JSON.parse(item))
new result(ctx, 'SUCCESS', 200, data).answer()
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})
添加菜品单位
router.post('/dishunit', new Auth().m, async ctx => {
const unid = new Date().getTime()
const {unit} = ctx.request.body
new unitcheck(ctx, unit).start()
const query = `db.collection('dishunit').where({label:'${unit}'}).get()`
const cate = `db.collection('dishunit').add({data:{value:'${unit}',label:'${unit}',unid:'${unid}'}})`
try {
const res = await new getToken().posteve(TripUrl, query)
if (res.data.length > 0) {
new result(ctx, '该菜品单位已存在', 202).answer()
} else {
await new getToken().posteve(AddUrl, cate)
new result(ctx, '添加成功').answer()
}
} catch (e) {
new result(ctx, '添加失败,服务器发生错误', 500).answer()
}
})
获取菜品
router.get('/obtaindishes', new Auth().m, async ctx => {
let {page} = ctx.query
let sk = page * 10
const query = `db.collection('dishes-data').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)
}
})
上架菜品
router.post('/uploaddishes', new Auth().m, async ctx => {
const {id, category, name, unitprice, unit, image, value} = ctx.request.body
new putoncheck(ctx, category, name, unitprice, unit, image, value).start()
let time = moment().utcOffset(8).format('YYYY-MM-DD HH:mm:ss')
let query = `db.collection('dishes-data').add({data:{
category:'${category}',name:'${name}',unitprice:${unitprice},unit:'${unit}',image:${image},quantity:0,onsale:true,cid:'${value}',time:'${time}',monthlysale:0
}})`
let count = `db.collection('dishes-category').where({cid:'${value}'}).update({data:{count:db.command.inc(1)}})`
try {
await new getToken().posteve(AddUrl, query)
await new getToken().posteve(UpdateUrl, count)
new result(ctx,'提交成功').answer()
} catch (e) {
new result(ctx, '提交失败,服务器发生错误', 500).answer()
}
})
下架菜品
router.get('/fromsale', new Auth().m, async ctx => {
const {id, value} = ctx.query
const query = `db.collection('dishes-data').doc('${id}').update({data:{onsale:false}})`
let count = `db.collection('dishes-category').where({cid:'${value}'}).update({data:{count:db.command.inc(-1)}})`
try {
await new getToken().posteve(UpdateUrl, query)
await new getToken().posteve(UpdateUrl, count)
new result(ctx, '下架成功').answer()
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})
修改上架的菜品
router.post('/modifydishes', new Auth().m, async ctx => {
const {id, category, name, unitprice, unit, image, value, valueBefore} = ctx.request.body
new putoncheck(ctx, category, name, unitprice, unit, image, value).start()
let time = moment().utcOffset(8).format('YYYY-MM-DD HH:mm:ss')
let query = `db.collection('dishes-data').doc('${id}').update({data:{
category:'${category}',name:'${name}',unitprice:${unitprice},unit:'${unit}',image:${image},quantity:0,onsale:true,cid:'${value}',time:'${time}'
}})`
let countBefore = `db.collection('dishes-category').where({cid:'${valueBefore}'}).update({data:{count:db.command.inc(-1)}})`
let countAfter = `db.collection('dishes-category').where({cid:'${value}'}).update({data:{count:db.command.inc(1)}})`
try {
await new getToken().posteve(UpdateUrl, countBefore)
await new getToken().posteve(UpdateUrl, query)
await new getToken().posteve(UpdateUrl, countAfter)
new result(ctx, '修改成功').answer()
} catch (e) {
new result(ctx, '服务器发生错误', 500).answer()
}
})