lrf 2 anni fa
parent
commit
eff65cc0b4

+ 9 - 3
app/controller/trade/config/.order.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['total', 'customer', 'address', 'total_detail', 'buy_time', 'pay_time', 'pay_id', 'pay_no', 'status'],
+    requestBody: ['customer', 'address', 'goods', 'total_detail', 'buy_time', 'pay_time', 'pay_id', 'no', 'status'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['total', 'customer', 'address', 'total_detail', 'buy_time', 'pay_time', 'pay_id', 'pay_no', 'status'],
+    requestBody: ['customer', 'address', 'goods', 'total_detail', 'buy_time', 'pay_time', 'pay_id', 'no', 'status'],
   },
   show: {
     parameters: {
@@ -24,7 +24,7 @@ module.exports = {
         customer: 'customer',
         buy_time: 'buy_time',
         pay_id: 'pay_id',
-        pay_no: 'pay_no',
+        no: 'no',
       },
       // options: {
       //   "meta.state": 0 // 默认条件
@@ -38,4 +38,10 @@ module.exports = {
       count: true,
     },
   },
+  fromCartToOrder: {
+    requestBody: ['!cart_ids'],
+  },
+  fromGoodsToOrder: {
+    requestBody: ['!goods_id', '!goodsSpec_id', '!num'],
+  },
 };

+ 22 - 0
app/controller/util.js

@@ -0,0 +1,22 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 业务工具
+class UtilController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.queryObject = this.ctx.query;
+    this.bodyObject = this.ctx.request.body;
+    this.service = this.ctx.service.util;
+    this.tradeService = this.ctx.service.util.trade;
+  }
+  /**
+   * 查询是否可以购买
+   */
+  async checkCanBuy() {
+    const data = await this.tradeService.checkCanBuy(this.bodyObject);
+    this.ctx.ok({ data });
+  }
+}
+module.exports = CrudController(UtilController, {});

+ 9 - 0
app/middleware/checkLogin.js

@@ -0,0 +1,9 @@
+'use strict';
+const _ = require('lodash');
+const whiteList = [];
+module.exports = options => {
+  return async function checklogin(ctx, next) {
+    console.log(ctx.request.url);
+    await next();
+  };
+};

+ 5 - 7
app/model/trade/order.js

@@ -2,17 +2,16 @@
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 
-const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
-
 // 订单
 const order = {
-  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
   address: { type: String, required: false, zh: '邮寄地址', ref: 'User.address' }, //
-  total_detail: { type: Array, required: false, zh: '总金额明细' }, // 运费,优惠,活动,商品原费用的和
+  goods: { type: Array, required: false, zh: '商品' }, // 按店铺分组,快照过来
+  total_detail: { type: Object, required: false, zh: '总金额明细' }, // 优惠,活动;商品总额和运费由商品而来
   buy_time: { type: String, required: false, zh: '下单时间' }, //
   pay_time: { type: String, required: false, zh: '支付时间' }, //
   pay_id: { type: String, required: false, zh: '支付id' }, //
-  pay_no: { type: String, required: false, zh: '支付订单号' }, //
+  no: { type: String, required: false, zh: '订单号' }, //
   status: { type: String, required: false, zh: '订单状态' }, // 字典:order_process
 };
 const schema = new Schema(order, { toJSON: { getters: true, virtuals: true } });
@@ -21,10 +20,9 @@ schema.index({ 'meta.createdAt': 1 });
 schema.index({ customer: 1 });
 schema.index({ buy_time: 1 });
 schema.index({ pay_id: 1 });
-schema.index({ pay_no: 1 });
+schema.index({ no: 1 });
 
 schema.plugin(metaPlugin);
-schema.plugin(MoneyPlugin({ zh: '总金额', required: false, key: 'total' }));
 
 module.exports = app => {
   const { mongoose } = app;

+ 1 - 1
app/router.js

@@ -12,6 +12,6 @@ module.exports = app => {
   require('./z_router/shop/index')(app); // 店铺部分
   require('./z_router/trade/index')(app); // 交易部分
 
-
   require('./z_router/view/index')(app); // 视图部分
+  require('./z_router/util')(app); // 工具接口
 };

+ 2 - 1
app/service/trade/cart.js

@@ -92,7 +92,8 @@ class CartService extends CrudService {
   }
 
   /**
-   * 检查商品规格的库存
+   * 检查商品规格的库存 
+   * TODO 检查商品的是否满足销售条件 & 规格是否在销售中
    * @param {String} goodsSpecId 商品规格id
    * @param {Number} num 需要的库存数量
    * @return {Boolean} true:库存满足购物车条件;false:库存不满足条件

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

@@ -4,12 +4,43 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 
-// 
+//
 class OrderService extends CrudService {
   constructor(ctx) {
     super(ctx, 'order');
     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 请求参数
+   */
+  async fromCartToOrder(body) {}
+
+  /**
+   * 从商品页直接购买进入下单页
+   * @param {Object} body 请求参数
+   * @param body.goods_id 商品id
+   * @param body.goodsSpec_id 商品规格id
+   * @param body.num 购买数量
+   */
+  async fromGoodsToOrder({ goods_id, goodsSpec_id, num }) {
+    const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ goods_id, goodsSpec_id, num });
+    if (!result) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
+    // 组装页面的数据
+    const user = this.ctx.user;
+    const customer = _.get(user, '_id');
+    if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
+    // 找到默认地址
+    const address = await this.addressModel.findOne({ customer, is_default: '1' });
+    // 组装商品数据
+
+    // TODO 优惠券列表 及 其他优惠
+  }
+
+
 }
 
 module.exports = OrderService;

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

@@ -0,0 +1,56 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class TradeService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'trade');
+    this.goodsModel = this.ctx.model.Shop.Goods;
+    this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
+  }
+  /**
+   * 检测是否可以购买该物品
+   * @param {Object} param 查询条件
+   * @param param.goods_id 商品id
+   * @param param.goodsSpec_id 商品规格id
+   * @param param.num 购买数量
+   */
+  async checkCanBuy({ goods_id, goodsSpec_id, num }) {
+    const result = { result: true };
+    // 1.检查商品是否可以购买
+    const goods = await this.goodsModel.findById(goods_id);
+    if (!goods) {
+      result.msg = '未找到商品';
+      result.result = false;
+      return result;
+    }
+    if (goods.status === '0') {
+      result.msg = '该商品已下架';
+      result.result = false;
+      return result;
+    }
+    // 2.检验该规格是否可以购买
+    const goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
+    if (!goodsSpec) {
+      result.msg = '未找到商品的指定规格';
+      result.result = false;
+      return result;
+    }
+    if (goods.status !== '0') {
+      result.msg = '该规格的商品已下架';
+      result.result = false;
+      return result;
+    }
+    if (goods.num < num) {
+      result.msg = '库存量不足';
+      result.result = false;
+      return result;
+    }
+    return result;
+  }
+}
+
+module.exports = TradeService;

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

@@ -37,7 +37,7 @@ class GoodsService extends CrudService {
 
   async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
     condition = this.dealFilter(condition);
-    const pipline = [];
+    const pipline = [{ $match: { status: { $ne: '0' } } }];
     const { view_num, sell_num, sell_money, name } = condition;
     const sort = {};
     if (view_num) sort.view_num = view_num;

+ 2 - 0
app/z_router/trade/order.js

@@ -7,6 +7,8 @@ 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}/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}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },

+ 13 - 0
app/z_router/util.js

@@ -0,0 +1,13 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'util';
+const ckey = 'util';
+const keyZh = '工具接口';
+const routes = [{ method: 'post', path: `${rkey}/checkCanBuy`, controller: `${ckey}.checkCanBuy`, zh: `${keyZh}-检查是否可以购买商品` }];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 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 = [ 'setUserFromToken' ];
+  config.middleware = [ 'setUserFromToken', 'checkLogin' ];
 
   // add your user config here
   const userConfig = {