lrf 2 jaren geleden
bovenliggende
commit
07604cb559

+ 2 - 2
app/controller/trade/config/.order.js

@@ -38,8 +38,8 @@ module.exports = {
       count: true,
     },
   },
-  fromCartToOrder: {
-    requestBody: ['!cart_ids'],
+  toMakeOrder: {
+    requestBody: ['!key'],
   },
   fromGoodsToOrder: {
     requestBody: ['!shop', '!goods', '!goodsSpec', '!num'],

+ 29 - 20
app/service/trade/order.js

@@ -8,43 +8,52 @@ const assert = require('assert');
 class OrderService extends CrudService {
   constructor(ctx) {
     super(ctx, 'order');
+    this.redis = this.app.redis;
+    this.redisKey = this.app.config.redisKey;
     this.model = this.ctx.model.Trade.Order;
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
     this.addressModel = this.ctx.model.User.Address;
   }
   /**
-   * 从购物车页进入下单页面
+   * 进入下单页面
    * @param {Object} body 请求参数
+   * @param body.key 缓存key
    */
-  async fromCartToOrder(body) {}
+  async toMakeOrder({ key }) {
+    key = `${this.redisKey.orderKeyPrefix}${key}`;
+    let data = await this.redis.get(key);
+    if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
+    data = JSON.parse(data);
+    const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
+    if (_.isArray(data)) {
+      // 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
+      console.log('line 31 in function:cart');
+    } else if (_.isObject(data)) {
+      // 商品页单独买: 1.校验规格商品; 2:按店铺分组
+      const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
+      if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
+      const list = await this.goodsSpecModel.find({ _id: data.goodsSpec }).populate(populate);
+      console.log(list);
 
-  /**
-   * 从商品页直接购买进入下单页
-   * @param {Object} body 请求参数
-   * @param body.shop 店铺id
-   * @param body.goods 商品id
-   * @param body.goodsSpec 商品规格id
-   * @param body.num 购买数量
-   */
-  async fromGoodsToOrder({ shop, goods, goodsSpec, num }) {
-    const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num });
-    if (!result) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
+    } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
     // 组装页面的数据
     const user = this.ctx.user;
     const customer = _.get(user, '_id');
     if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
-    const data = {};
+    const pageData = {};
     // 找到默认地址
     const address = await this.addressModel.findOne({ customer, is_default: '1' });
-    data.address = address;
-    // 组装商品数据
-    const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
-    const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec).populate(populate);
-    // TODO 优惠券列表 及 其他优惠
+    pageData.address = address;
   }
 
-  setOrderGoodsData() {}
+  /**
+   * 整理数据,需要商品与规格的快照,剩下的可以简略
+   * @param {Array} list 规格数组
+   */
+  setOrderGoodsData(list) {
+    // 按店铺分组,精简字段
+  }
 }
 
 module.exports = OrderService;

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

@@ -10,6 +10,7 @@ class TradeService extends CrudService {
     super(ctx, 'trade');
     this.redis = this.app.redis;
     this.redisKey = this.app.config.redisKey;
+    this.redisTimeout = this.app.config.redisTimeout;
     this.shopModel = this.ctx.model.Shop.Shop;
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
@@ -21,8 +22,9 @@ class TradeService extends CrudService {
    * @param param.goods 商品id
    * @param param.goodsSpec 商品规格id
    * @param param.num 购买数量
+   * @param makeCache 生成缓存
    */
-  async checkCanBuy({ shop, goods, goodsSpec, num }) {
+  async checkCanBuy({ shop, goods, goodsSpec, num }, makeCache = true) {
     assert(shop, '缺少店铺信息');
     assert(goods, '缺少商品信息');
     assert(goodsSpec, '缺少商品规格信息');
@@ -69,11 +71,14 @@ class TradeService extends CrudService {
       result.result = false;
       return result;
     }
-    const str = this.createNonceStr();
-    const key = `${this.redisKey.orderKeyPrefix}${str}`;
-    const value = JSON.stringify({ shop, goods, goodsSpec, num });
-    await this.redis.set(key, value, 'EX', 180);
-    result.key = str;
+    if (makeCache) {
+      const str = this.createNonceStr();
+      const key = `${this.redisKey.orderKeyPrefix}${str}`;
+      const value = JSON.stringify({ shop, goods, goodsSpec, num });
+      await this.redis.set(key, value, 'EX', this.redisTimeout);
+      result.key = str;
+    }
+
     return result;
   }
   // 随机字符串产生函数

+ 1 - 1
app/z_router/trade/order.js

@@ -7,7 +7,7 @@ const rkey = 'order';
 const ckey = 'trade.order';
 const keyZh = '总订单';
 const routes = [
-  { method: 'post', path: `${rkey}/fromCartToOrder`, controller: `${ckey}.fromCartToOrder`, name: `${ckey}fromCartToOrder`, zh: '购物车去下单' },
+  { method: 'post', path: `${rkey}/toMakeOrder`, controller: `${ckey}.toMakeOrder`, name: `${ckey}toMakeOrder`, zh: '组织下单页数据' },
   { method: 'post', path: `${rkey}/fromGoodsToOrder`, controller: `${ckey}.fromGoodsToOrder`, name: `${ckey}fromGoodsToOrder`, zh: '直接购买' },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },

+ 1 - 0
config/config.default.js

@@ -70,6 +70,7 @@ module.exports = appInfo => {
   config.redisKey = {
     orderKeyPrefix: 'orderKey:',
   };
+  config.redisTimeout = 300;
 
   return {
     ...config,