【系】微信小程序云开发实战坚果商城-云开发之分类数据实现

第 3-7 课:云开发之分类数据实现

目录

1 集合处理

fields 文件夹新建 productCategoryField.js

//  指定返回结果中记录需返回的字段
module.exports = {
    PRODUCT_CATEGORY_FIELD: {
      category_name: true,
      category_type: true,
      _id:false
    }
  }

在前面的章节,我也提到过,_id ,不需要返回咱们的手动写,否则会返回给前台

2 业务层实现

service/productService.js

// 在原来的上面增加 PRODUCT_CATEGORY
const { PRODUCT ,PRODUCT_CATEGORY } = require('../config/tableConfig.js')
// 新增分类字段过滤
const { PRODUCT_CATEGORY_FIELD } = require('../fields/productCategoryField.js')
/**
 * 获取商品分类
 * @return 
 */
const getCategoryMenu = () =>{
  return model.query(PRODUCT_CATEGORY,PRODUCT_CATEGORY_FIELD)
}

/**
 * 根据商品分类获取商品
 * @param {*} options 
 */
const getCategoryProduct = (options) => {
  options.product_status = 0 
  return model.query(PRODUCT, PRODUCTFIELD, options)
}

三、入口文件实现

/***************************    分类   *****************************************/
  // 获取分类
  app.router('getCategoryMenu', async (ctx,next) =>{
    ctx.data = await product.getCategoryMenu()
    ctx.body = await returnUtil.success(ctx)
    await next()
  })
  // 获取分类商品
  app.router('getCategoryProduct', async (ctx , next) => {
    let options = {}
    // ctx.data 前台传过来的category_type
    options.category_type = event.data
    ctx.data = await _productItem(product.getCategoryProduct(options))
    ctx.body = await returnUtil.success(ctx)
    await next()
  })

源码地址

在搭建项目前,根据自己需要下载本系列文章的源代码

本项目源码地址:https://gitee.com/mtcarpenter/nux-shop

发布了38 篇原创文章 · 获赞 67 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/havendream/article/details/103022713
今日推荐