lrf 2 年之前
父节点
当前提交
21c5d47620
共有 3 个文件被更改,包括 15 次插入7 次删除
  1. 1 0
      app/service/trade/order.js
  2. 11 0
      app/service/util/trade.js
  3. 3 7
      config/plugin.js

+ 1 - 0
app/service/trade/order.js

@@ -40,6 +40,7 @@ class OrderService extends CrudService {
     data.address = address;
     // 组装商品数据
     const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
+    const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec).populate(populate);
     // TODO 优惠券列表 及 其他优惠
   }
 

+ 11 - 0
app/service/util/trade.js

@@ -8,9 +8,12 @@ const assert = require('assert');
 class TradeService extends CrudService {
   constructor(ctx) {
     super(ctx, 'trade');
+    this.redis = this.app.redis;
     this.shopModel = this.ctx.model.Shop.Shop;
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
+
+    this.orderKeyPrefix = 'orderKey:';
   }
   /**
    * 检测是否可以购买该物品
@@ -67,8 +70,16 @@ class TradeService extends CrudService {
       result.result = false;
       return result;
     }
+    const key = `${this.orderKeyPrefix}${this.createNonceStr()}`;
+    const value = JSON.stringify({ shop, goods, goodsSpec, num });
+    await this.redis.set(key, value, 'EX', 180);
+    result.key = key;
     return result;
   }
+  // 随机字符串产生函数
+  createNonceStr() {
+    return Math.random().toString(36).substr(2, 15);
+  }
 }
 
 module.exports = TradeService;

+ 3 - 7
config/plugin.js

@@ -1,9 +1,5 @@
 'use strict';
-
-/** @type Egg.EggPlugin */
-module.exports = {
-  // had enabled by egg
-  // static: {
-  //   enable: true,
-  // }
+exports.redis = {
+  enable: true,
+  package: 'egg-redis',
 };