소스 검색

商品浏览量

lrf 2 년 전
부모
커밋
ac9b3c1961
3개의 변경된 파일7개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 0
      app/controller/shop/config/.goods.js
  2. 2 0
      app/model/shop/goods.js
  3. 4 1
      app/service/view/goods.js

+ 1 - 0
app/controller/shop/config/.goods.js

@@ -24,6 +24,7 @@ module.exports = {
         shop: 'shop',
         name: 'name',
         status: 'status',
+        view_num: 'view_num',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 2 - 0
app/model/shop/goods.js

@@ -11,6 +11,7 @@ const goods = {
   brief: { type: String, required: false, zh: '商品介绍' }, //
   file: { type: Array, required: false, zh: '商品图片' }, //
   tags: { type: Array, required: false, zh: '商品标签' }, //
+  view_num: { type: Number, required: false, default: 0, zh: '浏览量' }, //
   status: { type: String, required: false, default: '0', zh: '商品状态' }, // 字典:goods_status
 };
 const schema = new Schema(goods, { toJSON: { getters: true, virtuals: true } });
@@ -19,6 +20,7 @@ schema.index({ 'meta.createdAt': 1 });
 schema.index({ shop: 1 });
 schema.index({ name: 1 });
 schema.index({ status: 1 });
+schema.index({ view_num: 1 });
 
 schema.plugin(metaPlugin);
 

+ 4 - 1
app/service/view/goods.js

@@ -18,7 +18,7 @@ class GoodsService extends CrudService {
    */
   async goodsDetail({ id }) {
     const { populate } = this.ctx.service.shop.goods.getRefMods();
-    let goods = await this.goodsModel.findById(id, { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1 }).populate(populate);
+    let goods = await this.goodsModel.findById(id, { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1, view_num: 1 }).populate(populate);
     if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品数据');
     goods = JSON.parse(JSON.stringify(goods));
     let specs = await this.goodsSpecModel.find({ goods: id, status: '0' }, { sell_money: 1, flow_money: 1, freight: 1, name: 1, num: 1 });
@@ -30,6 +30,9 @@ class GoodsService extends CrudService {
     // goods: 商品信息; specs:商品规格信息; shop:店铺信息
     const returnData = { goods, specs, shop };
 
+
+    // 添加浏览次数
+    await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
     return returnData;
   }
 }