lrf 2 years ago
parent
commit
9a9d1cb5c4
3 changed files with 53 additions and 8 deletions
  1. 46 5
      app/service/trade/order.js
  2. 5 1
      app/service/user/user.js
  3. 2 2
      config/config.default.js

+ 46 - 5
app/service/trade/order.js

@@ -26,16 +26,15 @@ class OrderService extends CrudService {
     if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
     data = JSON.parse(data);
     const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
+    let specsData = [];
     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);
-
+      specsData = this.setGoodsToPageData(list);
     } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
     // 组装页面的数据
     const user = this.ctx.user;
@@ -45,14 +44,56 @@ class OrderService extends CrudService {
     // 找到默认地址
     const address = await this.addressModel.findOne({ customer, is_default: '1' });
     pageData.address = address;
+    // 商品总价,各店铺的价格明细
+    pageData.goodsData = specsData;
+    return pageData;
   }
 
   /**
-   * 整理数据,需要商品与规格的快照,剩下的可以简略
+   * 单商品整理数据,需要商品与规格的快照,剩下的可以简略
    * @param {Array} list 规格数组
    */
-  setOrderGoodsData(list) {
+  setGoodsToPageData(list) {
     // 按店铺分组,精简字段
+    const arr = [];
+    for (const i of list) {
+      const obj = {};
+      obj.shop_name = _.get(i.goods, 'shop.name');
+      obj.shop = _.get(i.goods, 'shop._id');
+      const goods = {};
+      goods.goods_id = _.get(i.goods, '_id');
+      goods.goods_name = _.get(i.goods, 'name');
+      goods.goodsSpec_id = _.get(i, '_id');
+      goods.goodsSpec_name = _.get(i, 'name');
+      goods.freight = _.get(i, 'freight');
+      goods.money = _.get(i, 'sell_money');
+      goods.num = _.get(i, 'num');
+      goods.file = _.get(i.goods, 'file');
+      obj.goods = goods;
+      arr.push(obj);
+    }
+    return arr;
+  }
+
+  /**
+   * 重组购物车商品
+   * @param {Object} goods 商品
+   * @param {Object} goodsSpec 商品规格
+   * @param {Object} data 购物车数据
+   */
+  setCartGoodsData(data) {
+    const { goods, goodsSpec, _id, num = 1 } = data;
+    const obj = {};
+    obj.cart_id = _id;
+    obj.goods_id = _.get(goods, '_id');
+    obj.goods_name = _.get(goods, 'name');
+    obj.goodsSpec_id = _.get(goodsSpec, '_id');
+    obj.goodsSpec_name = _.get(goodsSpec, 'name');
+    obj.freight = _.get(goodsSpec, 'freight');
+    obj.money = _.get(goodsSpec, 'sell_money');
+    obj.num = num;
+    obj.file = _.get(goods, 'file');
+    return obj;
   }
 }
 

+ 5 - 1
app/service/user/user.js

@@ -11,10 +11,14 @@ class UserService extends CrudService {
     this.model = this.ctx.model.User.User;
   }
   async beforeCreate(data) {
+    const openid = _.get(data, 'openid');
     const phone = _.get(data, 'phone');
-    if (phone) {
+    if (!openid && phone) {
       const num = await this.model.count({ phone });
       if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该手机号已注册');
+    } else if (openid) {
+      const num = await this.model.count({ openid });
+      if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该微信号已注册');
     }
     return data;
   }

+ 2 - 2
config/config.default.js

@@ -17,7 +17,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1664237342649_2194';
 
   // add your middleware config here
-  config.middleware = [ 'setUserFromToken', 'checkLogin' ];
+  config.middleware = [ 'setUserFromToken' ]; // , 'checkLogin'
 
   // add your user config here
   const userConfig = {
@@ -70,7 +70,7 @@ module.exports = appInfo => {
   config.redisKey = {
     orderKeyPrefix: 'orderKey:',
   };
-  config.redisTimeout = 300;
+  config.redisTimeout = 3600;
 
   return {
     ...config,