category.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. import CategoryModel from '../../models/shopping/category'
  3. import BaseComponent from '../../prototype/baseComponent'
  4. import DeliveryModel from '../../models/shopping/delivery'
  5. import ActivityModel from '../../models/shopping/activity'
  6. class Category extends BaseComponent{
  7. constructor(){
  8. super()
  9. }
  10. //获取所有餐馆分类和数量
  11. async getCategories(req, res, next){
  12. try{
  13. const categories = await CategoryModel.find({}, '-_id');
  14. res.send(categories);
  15. }catch(err){
  16. console.log('获取categories失败');
  17. res.send({
  18. status: 0,
  19. type: 'ERROR_DATA',
  20. message: '获取categories失败'
  21. })
  22. }
  23. }
  24. async addCategory(type){
  25. try{
  26. await CategoryModel.addCategory(type)
  27. }catch(err){
  28. console.log('增加category数量失败');
  29. }
  30. }
  31. async findById(id){
  32. try{
  33. const CateEntity = await CategoryModel.findOne({'sub_categories.id': id});
  34. let categoName = CateEntity.name;
  35. CateEntity.sub_categories.forEach(item => {
  36. if (item.id == id) {
  37. categoName += '/' + item.name;
  38. }
  39. })
  40. return categoName
  41. }catch(err){
  42. console.log('通过category id获取数据失败')
  43. throw new Error(err)
  44. }
  45. }
  46. //获取配送方式
  47. async getDelivery(req, res, next){
  48. try{
  49. const deliveries = await DeliveryModel.find({}, '-_id');
  50. res.send(deliveries)
  51. }catch(err){
  52. console.log('获取配送方式数据失败');
  53. res.send({
  54. status: 0,
  55. type: 'ERROR_DATA',
  56. message: '获取配送方式数据失败'
  57. })
  58. }
  59. }
  60. //获取活动列表
  61. async getActivity(req, res, next){
  62. try{
  63. const activities = await ActivityModel.find({}, '-_id');
  64. res.send(activities)
  65. }catch(err){
  66. console.log('获取活动数据失败');
  67. res.send({
  68. status: 0,
  69. type: 'ERROR_DATA',
  70. message: '获取活动数据失败'
  71. })
  72. }
  73. }
  74. }
  75. export default new Category()