lrf 2 年 前
コミット
575ccce161

+ 7 - 0
app/controller/trade/config/.cart.js

@@ -38,4 +38,11 @@ module.exports = {
       count: true,
     },
   },
+  selfCart: {
+    parameters: {
+      query: {
+        customer: 'customer',
+      },
+    },
+  },
 };

+ 10 - 0
app/middleware/setColumnForQuery.js

@@ -0,0 +1,10 @@
+'use strict';
+const _ = require('lodash');
+module.exports = options => {
+  return async function setcolumnforquery(ctx, next) {
+    const query = ctx.request.query;
+    const customer = _.get(ctx.user, '_id');
+    if (!query.customer) query.customer = customer;
+    await next();
+  };
+};

+ 12 - 0
app/middleware/setUserFromToken.js

@@ -0,0 +1,12 @@
+'use strict';
+const _ = require('lodash');
+module.exports = options => {
+  return async function setuserfromtoken(ctx, next) {
+    const token = _.get(ctx.request, 'header.token');
+    if (token) {
+      const data = ctx.service.util.jwt.decode(token);
+      if (data) ctx.user = data;
+    }
+    await next();
+  };
+};

+ 49 - 0
app/service/trade/cart.js

@@ -12,6 +12,55 @@ class CartService extends CrudService {
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
   }
 
+  /**
+   * 返回购物车需要的数据格式
+   * @param {Object} query 查询条件
+   * @param query.customer 顾客id
+   * @return {Array}
+   */
+  async selfCart({ customer }) {
+    assert(customer, '缺少顾客信息');
+    const { populate } = this.getRefMods();
+    let list = await this.model.find({ customer }).populate(populate);
+    list = JSON.parse(JSON.stringify(list));
+    list = list.map(i => {
+      const obj = {};
+      obj.cart_id = _.get(i, '_id');
+      obj.shop_name = _.get(i.shop, 'name');
+      obj.shop = _.get(i.shop, '_id');
+      obj.goods = this.setCartGoodsData(i.goods, i.goodsSpec);
+      return obj;
+    });
+    list = _.groupBy(list, 'shop');
+    const arr = [];
+    for (const key in list) {
+      const shopGroup = list[key];
+      const goods = shopGroup.reduce((p, n) => {
+        p.push(n.goods);
+        return p;
+      }, []);
+      const shop = _.omit(_.head(shopGroup), [ 'goods' ]);
+      arr.push({ ...shop, goods });
+    }
+    return arr;
+  }
+
+  /**
+   * 重组购物车商品
+   * @param {Object} goods 商品
+   * @param {Object} goodsSpec 商品规格
+   */
+  setCartGoodsData(goods, goodsSpec) {
+    const obj = {};
+    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.money = _.get(goodsSpec, 'sell_money');
+    obj.num = _.get(goodsSpec, 'num');
+    return obj;
+  }
+
   /**
    * 创建购物车信息,若找到该店的该商品规格.进行库存校验
    * 数量进行合并;

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

@@ -34,13 +34,14 @@ class UserService extends CrudService {
    */
   async login({ phone, password }) {
     const { populate } = this.getRefMods();
-    const user = await this.model.findOne({ phone }, '+password').populate(populate);
+    let user = await this.model.findOne({ phone }, '+password').populate(populate);
     if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
     const { password: upwd, status } = user;
     if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
     if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
     // // 使用redis存储,后续的任何操作进行token的校验
     // await this.setUserInRedis(user);
+    user = JSON.parse(JSON.stringify(user));
     delete user.password;
     delete user.meta;
     delete user.__v;

+ 4 - 0
app/service/util/jwt.js

@@ -13,6 +13,10 @@ class JwtService extends CrudService {
     const token = jwt.sign(JSON.stringify(data), secret);
     return token;
   }
+  decode(token) {
+    const data = jwt.decode(token);
+    return data;
+  }
 }
 
 module.exports = JwtService;

+ 1 - 0
app/z_router/trade/cart.js

@@ -7,6 +7,7 @@ const rkey = 'cart';
 const ckey = 'trade.cart';
 const keyZh = '购物车';
 const routes = [
+  { method: 'get', path: `${rkey}/selfCart`, controller: `${ckey}.selfCart`, middleware: [ 'setColumnForQuery' ], name: `${ckey}SelfCart`, zh: `用户自己的${keyZh}` },
   { 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}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },

+ 1 - 1
config/config.default.js

@@ -17,7 +17,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1664237342649_2194';
 
   // add your middleware config here
-  config.middleware = [];
+  config.middleware = [ 'setUserFromToken' ];
 
   // add your user config here
   const userConfig = {