Browse Source

Merge branch 'dev'

lrf 2 years ago
parent
commit
65490971fa

+ 5 - 0
app/controller/shop/config/.shopNotice.js

@@ -27,6 +27,7 @@ module.exports = {
         time: 'time',
         time: 'time',
         type: 'type',
         type: 'type',
         status: 'status',
         status: 'status',
+        source_type: 'source_type',
       },
       },
       // options: {
       // options: {
       //   "meta.state": 0 // 默认条件
       //   "meta.state": 0 // 默认条件
@@ -48,4 +49,8 @@ module.exports = {
     requestBody: ['source_id'],
     requestBody: ['source_id'],
     service: 'remindToAfterSale',
     service: 'remindToAfterSale',
   },
   },
+  cnr: {
+    params: ['shop'],
+    service: 'countNotRead',
+  },
 };
 };

+ 2 - 2
app/controller/util.js

@@ -14,8 +14,8 @@ class UtilController extends Controller {
     this.tradeService = this.ctx.service.util.trade;
     this.tradeService = this.ctx.service.util.trade;
   }
   }
   async util() {
   async util() {
-    const res = this.ctx.multiply(this.ctx.minus(1, this.ctx.divide(0, 100)), 200);
-    this.ctx.ok({ data: res });
+    // await this.ctx.service.util.rabbitMq.shopMsg('6333d71d32c5f69745f9bd32');
+    this.ctx.ok({ data: 'ok' });
   }
   }
 
 
   async crk() {
   async crk() {

+ 16 - 4
app/service/shop/shopNotice.js

@@ -26,18 +26,22 @@ class ShopNoticeService extends CrudService {
       const od = await this.orderDetailModel.findById(source_id);
       const od = await this.orderDetailModel.findById(source_id);
       if (!od) return;
       if (!od) return;
       const content = this.toSendMsg(_.get(od, 'no'));
       const content = this.toSendMsg(_.get(od, 'no'));
-      const obj = { shop: _.get(od, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type };
+      const obj = { shop: _.get(od, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '0' };
       list.push(obj);
       list.push(obj);
     } else if (_.isArray(source_id)) {
     } else if (_.isArray(source_id)) {
       for (const id of source_id) {
       for (const id of source_id) {
         const od = await this.orderDetailModel.findById(id);
         const od = await this.orderDetailModel.findById(id);
         if (!od) continue;
         if (!od) continue;
         const content = this.toSendMsg(_.get(od, 'no'));
         const content = this.toSendMsg(_.get(od, 'no'));
-        const obj = { shop: _.get(od, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type };
+        const obj = { shop: _.get(od, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '0' };
         list.push(obj);
         list.push(obj);
       }
       }
     }
     }
     await this.model.insertMany(list);
     await this.model.insertMany(list);
+    // 发mq消息
+    for (const i of list) {
+      await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
+    }
 
 
   }
   }
   /**
   /**
@@ -60,18 +64,22 @@ class ShopNoticeService extends CrudService {
       const data = await this.afterSaleModel.findById(source_id);
       const data = await this.afterSaleModel.findById(source_id);
       const od = await this.orderDetailModel.findById(data.order_detail);
       const od = await this.orderDetailModel.findById(data.order_detail);
       const content = this.toAfterSaleMsg(_.get(od, 'no'));
       const content = this.toAfterSaleMsg(_.get(od, 'no'));
-      const obj = { shop: _.get(data, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type };
+      const obj = { shop: _.get(data, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '1' };
       list.push(obj);
       list.push(obj);
     } else if (_.isArray(source_id)) {
     } else if (_.isArray(source_id)) {
       for (const id of source_id) {
       for (const id of source_id) {
         const data = await this.afterSaleModel.findById(id);
         const data = await this.afterSaleModel.findById(id);
         const od = await this.orderDetailModel.findById(data.order_detail);
         const od = await this.orderDetailModel.findById(data.order_detail);
         const content = this.toAfterSaleMsg(_.get(od, 'no'));
         const content = this.toAfterSaleMsg(_.get(od, 'no'));
-        const obj = { shop: _.get(data, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type };
+        const obj = { shop: _.get(data, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '1' };
         list.push(obj);
         list.push(obj);
       }
       }
     }
     }
     await this.model.insertMany(list);
     await this.model.insertMany(list);
+    // 发mq消息
+    for (const i of list) {
+      await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
+    }
   }
   }
   /**
   /**
    * 提示售后消息
    * 提示售后消息
@@ -80,6 +88,10 @@ class ShopNoticeService extends CrudService {
   toAfterSaleMsg(no) {
   toAfterSaleMsg(no) {
     return `您的订单号为: ${no} 的订单有售后未处理,请您及时处理`;
     return `您的订单号为: ${no} 的订单有售后未处理,请您及时处理`;
   }
   }
+
+  async countNotRead({ shop }) {
+    return this.model.count({ shop, status: '0' });
+  }
 }
 }
 
 
 module.exports = ShopNoticeService;
 module.exports = ShopNoticeService;

+ 11 - 8
app/service/trade/orderDetail.js

@@ -26,10 +26,10 @@ class OrderDetailService extends CrudService {
     if (goods_id) {
     if (goods_id) {
       // 传来指定商品,查有该商品的订单,但是如果没有,就查每一项中是否有 shop_transport_no 和 shop_transport_type
       // 传来指定商品,查有该商品的订单,但是如果没有,就查每一项中是否有 shop_transport_no 和 shop_transport_type
       // 如果有这俩属性,说明也有单子,也查出来
       // 如果有这俩属性,说明也有单子,也查出来
-      toSearch = transport.filter((f) => _.isArray(f.goods) && f.goods.find((fg) => fg.goods_id === goods_id));
+      toSearch = transport.filter(f => _.isArray(f.goods) && f.goods.find(fg => fg.goods_id === goods_id));
       console.log(toSearch);
       console.log(toSearch);
       if (toSearch.length <= 0) {
       if (toSearch.length <= 0) {
-        toSearch = transport.filter((f) => f.shop_transport_no && f.shop_transport_type);
+        toSearch = transport.filter(f => f.shop_transport_no && f.shop_transport_type);
       }
       }
     } else {
     } else {
       toSearch = transport;
       toSearch = transport;
@@ -62,6 +62,7 @@ class OrderDetailService extends CrudService {
     // 分订单计数器
     // 分订单计数器
     let noTimes = 1;
     let noTimes = 1;
     // const list = [];
     // const list = [];
+    const ids = [];
     for (const s of shopGoods) {
     for (const s of shopGoods) {
       const shop = _.get(s, 'shop');
       const shop = _.get(s, 'shop');
       const remarks = _.get(s, 'remarks');
       const remarks = _.get(s, 'remarks');
@@ -79,10 +80,12 @@ class OrderDetailService extends CrudService {
       // 测试用
       // 测试用
       obj.status = '1';
       obj.status = '1';
       // list.push(obj);
       // list.push(obj);
-      tran.insert('OrderDetail', obj);
+      const od_id = tran.insert('OrderDetail', obj);
+      ids.push(od_id);
       // 添加该商品是否和平台活动有关,做记录
       // 添加该商品是否和平台活动有关,做记录
       // await this.ctx.service.trade.actOrder.create(orderDetail_id, obj, tran);
       // await this.ctx.service.trade.actOrder.create(orderDetail_id, obj, tran);
     }
     }
+    return ids;
     // await this.model.insertMany(list);
     // await this.model.insertMany(list);
   }
   }
 
 
@@ -129,10 +132,10 @@ class OrderDetailService extends CrudService {
     const rate = await this.goodsRateModel.find({ orderDetail: res._id });
     const rate = await this.goodsRateModel.find({ orderDetail: res._id });
     const goods = _.get(res, 'goods', []);
     const goods = _.get(res, 'goods', []);
     for (const g of goods) {
     for (const g of goods) {
-      const r = afterSale.find((f) => ObjectId(_.get(f, 'goods._id')).equals(g._id));
+      const r = afterSale.find(f => ObjectId(_.get(f, 'goods._id')).equals(g._id));
       if (r) g.is_afterSale = true;
       if (r) g.is_afterSale = true;
       else g.is_afterSale = false;
       else g.is_afterSale = false;
-      const r2 = rate.find((f) => ObjectId(_.get(f, 'goodsSpec')).equals(g._id));
+      const r2 = rate.find(f => ObjectId(_.get(f, 'goodsSpec')).equals(g._id));
       if (r2) {
       if (r2) {
         g.is_rate = true;
         g.is_rate = true;
         g.rate = r2._id;
         g.rate = r2._id;
@@ -178,7 +181,7 @@ class OrderDetailService extends CrudService {
     if (sort && _.isString(sort)) {
     if (sort && _.isString(sort)) {
       sort = { [sort]: desc ? -1 : 1 };
       sort = { [sort]: desc ? -1 : 1 };
     } else if (sort && _.isArray(sort)) {
     } else if (sort && _.isArray(sort)) {
-      sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
     }
     }
     let condition = _.cloneDeep(filter);
     let condition = _.cloneDeep(filter);
     condition = await this.beforeQuery(condition);
     condition = await this.beforeQuery(condition);
@@ -263,7 +266,7 @@ class OrderDetailService extends CrudService {
       for (const og of obj.goods) {
       for (const og of obj.goods) {
         const { file = [], _id: goodsSpec } = og;
         const { file = [], _id: goodsSpec } = og;
         const gfile = _.get(og, 'goods.file', []);
         const gfile = _.get(og, 'goods.file', []);
-        const nf = [...file, ...gfile];
+        const nf = [ ...file, ...gfile ];
         const url = _.get(_.head(nf), 'url');
         const url = _.get(_.head(nf), 'url');
         og.url = url;
         og.url = url;
         delete og.file;
         delete og.file;
@@ -275,7 +278,7 @@ class OrderDetailService extends CrudService {
         obj.price = _.get(obj, 'price', obj.sell_money);
         obj.price = _.get(obj, 'price', obj.sell_money);
       }
       }
       // 售后
       // 售后
-      const asum = await this.afterSaleModel.count({ order_detail: obj._id, status: { $nin: ['-1', '!1', '-2', '!2', '-3', '!3', '-4', '!4', '-5', '!5'] } });
+      const asum = await this.afterSaleModel.count({ order_detail: obj._id, status: { $nin: [ '-1', '!1', '-2', '!2', '-3', '!3', '-4', '!4', '-5', '!5' ] } });
       obj.is_afterSale = asum > 0;
       obj.is_afterSale = asum > 0;
       list.push(obj);
       list.push(obj);
     }
     }

+ 3 - 2
app/service/trade/pay.js

@@ -127,6 +127,7 @@ class PayService extends CrudService {
     }
     }
     orderData = JSON.parse(JSON.stringify(orderData));
     orderData = JSON.parse(JSON.stringify(orderData));
     const payData = _.get(orderData, 'pay', {});
     const payData = _.get(orderData, 'pay', {});
+    let odIds = [];
     // 支付结果全都存起来
     // 支付结果全都存起来
     payData.result = result;
     payData.result = result;
     // 支付时间
     // 支付时间
@@ -136,7 +137,7 @@ class PayService extends CrudService {
       this.tran.update('Order', orderData._id, { pay: payData, status: '1' });
       this.tran.update('Order', orderData._id, { pay: payData, status: '1' });
       await this.tran.run();
       await this.tran.run();
       // 拆订单
       // 拆订单
-      await this.ctx.service.trade.orderDetail.create({ order_id: orderData._id }, this.tran);
+      odIds = await this.ctx.service.trade.orderDetail.create({ order_id: orderData._id }, this.tran);
       // 加销量
       // 加销量
       await this.addSell(orderData, this.tran);
       await this.addSell(orderData, this.tran);
       await this.tran.run();
       await this.tran.run();
@@ -160,7 +161,7 @@ class PayService extends CrudService {
       this.tran.clean();
       this.tran.clean();
     }
     }
     // 发送系统消息,让对应的店铺接收消息
     // 发送系统消息,让对应的店铺接收消息
-    const msgData = { source_id: orderData._id, type: '0' };
+    const msgData = { source_id: odIds, type: '0' };
     await this.ctx.service.shop.shopNotice.remindToSend(msgData);
     await this.ctx.service.shop.shopNotice.remindToSend(msgData);
   }
   }
   /**
   /**

+ 19 - 0
app/service/util/rabbitMq.js

@@ -12,6 +12,25 @@ class RabbitmqService extends Service {
     this.task = this.app.config.taskMqConfig;
     this.task = this.app.config.taskMqConfig;
   }
   }
 
 
+  /**
+   * 店铺系统消息
+   * @param shop_id 店铺id
+   */
+  async shopMsg(shop_id) {
+    const { mq } = this.ctx;
+    const ex = 'shopMsg';
+    try {
+      const ch = await mq.conn.createChannel();
+      await ch.assertExchange(ex, 'direct', { durable: true });
+      await ch.publish(ex, shop_id, Buffer.from('check msg'));
+      await ch.close();
+    } catch (error) {
+      console.error('mq--店铺系统消息--任务队列生成失败');
+      console.error(error);
+    }
+  }
+
+
   // 初始化死信机制
   // 初始化死信机制
   async initDeadProcess() {
   async initDeadProcess() {
     try {
     try {

+ 1 - 0
app/z_router/shop/shopNotice.js

@@ -8,6 +8,7 @@ const ckey = 'shop.shopNotice';
 const keyZh = '店铺系统消息';
 const keyZh = '店铺系统消息';
 const routes = [
 const routes = [
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/cnr/:shop`, controller: `${ckey}.cnr`, name: `${ckey}cnr`, zh: `${keyZh}查询未读` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}/rts`, controller: `${ckey}.rts`, name: `${ckey}rts`, zh: `发送-处理发货-${keyZh}` },
   { method: 'post', path: `${rkey}/rts`, controller: `${ckey}.rts`, name: `${ckey}rts`, zh: `发送-处理发货-${keyZh}` },
   { method: 'post', path: `${rkey}/rtas`, controller: `${ckey}.rtas`, name: `${ckey}rtas`, zh: `发送-处理售后-${keyZh}` },
   { method: 'post', path: `${rkey}/rtas`, controller: `${ckey}.rtas`, name: `${ckey}rtas`, zh: `发送-处理售后-${keyZh}` },